FredMB
Chieftain
Hello Everyone
I'm experimenting with my first somewhat useful mod that includes LUA. This mod includes the unit's level with the unit name in both UnitPanel.lua and UnitFlagManager.lua. I got the code working just fine for the UnitPanel. It adds the unit's level before the unit's name and even references Language_en_US for text. The code for this is below:
XML File:
<GameData>
<Language_en_US>
<Row Tag="TXT_KEY_UPANEL_LEVEL_ADDED_TO_UNIT_NAME">
<Text>Lv</Text>
</Row>
</Language_en_US>
</GameData>
LUA File:
-- Start Modification
--------------------------------------------------------------------------------
-- Refresh unit portrait and name
--------------------------------------------------------------------------------
function UpdateUnitPortrait( unit )
local ileveltext = unit:GetLevel(); -- Returns Unit Level (Same as tool tip below).
local name = unit:GetName(); -- Retrieves the name of the unit (Original file).
local Levelinunitname = Locale.ConvertTextKey("TXT_KEY_UPANEL_LEVEL_ADDED_TO_UNIT_NAME"); -- Converts "Lv" from the Language_en_US in level.xml to text.
name = Locale.ToUpper(name); -- Sets all uppercase for unit name (Original file).
--local name = unit:GetNameKey(); -- Disabled in original file.
local ileveltextconvert = Locale.ConvertTextKey(ileveltext); -- Converts unit level to text.
local convertedKey = Locale.ConvertTextKey(name); -- Converts the uppercase unit name to text (Original file).
convertedKey = Locale.ToUpper(convertedKey); -- Sets all uppercase for "convertedKey". Seems redundant. Not sure if needed. (Original file).
Controls.UnitName:SetText(Levelinunitname..ileveltextconvert.." "..convertedKey); -- Not pretty but it works. Space between Lv1 and Unit added here. Now sure how else to do it.
--Controls.UnitName:SetText(convertedKey); -- Prints "convertedKey" in game. (Original file).
Controls.UnitName:SetFontByName("TwCenMT24");
-- End Modification
This code works great in the UnitPanel. It updates live as the unit gains levels. I've tested this with the Live Tuner.
However it doesn't work so great in the UnitFlagManager.lua. The code is very similar but a bit different:
-- Modified by FredMB
------------------------------------------------------------------
-- constructor
------------------------------------------------------------------
Initialize = function( o, playerID, unitID, fogState, invisible )
o.m_Player = Players[ playerID ];
o.m_PlayerID = playerID;
o.m_UnitID = unitID;
if( g_PrintDebug ) then print( string.format( "Creating UnitFlag for: Player[%i] Unit[%i]", playerID, unitID ) ); end
local pUnit = Players[ playerID ]:GetUnitByID( unitID );
local ileveltext2 = pUnit:GetLevel(); -- Returns Unit Level (Same as UnitPanel.lua).
local ileveltextconvert2 = Locale.ConvertTextKey(ileveltext2); -- Converts unit level to text.
local Levelinunitname2 = Locale.ConvertTextKey("TXT_KEY_UPANEL_LEVEL_ADDED_TO_UNIT_NAME"); -- Converts "Lv" from the Language_en_US in level.xml to text.
if( pUnit == nil )
then
print( string.format( "Unit not found for UnitFlag: Player[%i] Unit[%i]", playerID, unitID ) );
return nil;
end
o.m_IsCivilian = not pUnit:IsCombatUnit();
o.m_IsInvisible = invisible;
-- Technically, we should get a UnitGarrisoned event after the creation event if
-- the unit is garrisoned. So IsGarrisoned should always be false at creation.
-- In the interest of preserving behavior I'm allowing m_IsGarrisoned to be set
-- using IsGarrisoned() on creation. However, in the strategic view this causes
-- a visibility error in some odd cases so there it always starts as false.
if( InStrategicView() )
then
o.m_IsGarrisoned = false;
else
o.m_IsGarrisoned = pUnit:IsGarrisoned();
end
---------------------------------------------------------
-- Hook up the button
local pPlayer = Players[Game.GetActivePlayer()];
local active_team = pPlayer:GetTeam();
local team = o.m_Player:GetTeam();
--local name = pUnit:GetNameKey();
--local localizedName = Locale.ConvertTextKey(name);
local localizedName = pUnit:GetName();
localizedNameWithlevel = (Levelinunitname2..ileveltextconvert2.." "..localizedName); --New line similar to UnitPanel.
local localizedNameWithlevelConvert = Locale.ConvertTextKey(localizedNameWithlevel); --Converts localizedNameWithlevel to text.
if( active_team == team )
then
o.m_Instance.NormalButton:SetVoid1( playerID );
o.m_Instance.NormalButton:SetVoid2( unitID );
o.m_Instance.NormalButton:RegisterCallback( Mouse.eLClick, UnitFlagClicked );
o.m_Instance.NormalButton:RegisterCallback( Mouse.eMouseEnter, UnitFlagEnter );
o.m_Instance.NormalButton:RegisterCallback( Mouse.eMouseExit, UnitFlagExit );
o.m_Instance.NormalButton:SetDisabled( false );
o.m_Instance.NormalButton:SetConsumeMouseOver( true );
if(PreGame.IsMultiplayerGame() and o.m_Player:IsHuman()) then
local unitOwner = Players[playerID];
local string = Locale.ConvertTextKey("TXT_KEY_MULTIPLAYER_UNIT_TT", o.m_Player:GetNickName(), o.m_Player:GetCivilizationAdjectiveKey(), localizedNameWithlevelConvert ); -- localizedNameWithlevel used here.
if( playerID == Game.GetActivePlayer() ) then
string = string .. Locale.ConvertTextKey( "TXT_KEY_UPANEL_CLICK_TO_SELECT" );
end
o.m_Instance.UnitIcon:SetToolTipString( string );
else
o.m_Instance.UnitIcon:SetToolTipString(Locale.ConvertTextKey("TXT_KEY_PLOTROLL_UNIT_DESCRIPTION_CIV", o.m_Player:GetCivilizationAdjectiveKey(), localizedNameWithlevelConvert).. Locale.ConvertTextKey( "TXT_KEY_UPANEL_CLICK_TO_SELECT" )); -- localizedNameWithlevel used here.
end
o.m_Instance.HealthBarButton:SetVoid1( playerID );
o.m_Instance.HealthBarButton:SetVoid2( unitID );
o.m_Instance.HealthBarButton:RegisterCallback( Mouse.eLClick, UnitFlagClicked );
o.m_Instance.HealthBarButton:RegisterCallback( Mouse.eMouseEnter, UnitFlagEnter );
o.m_Instance.HealthBarButton:RegisterCallback( Mouse.eMouseExit, UnitFlagExit );
o.m_Instance.HealthBarButton:SetDisabled( false );
o.m_Instance.HealthBarButton:SetConsumeMouseOver( true );
local pPlot = pUnit:GetPlot();
if( pPlot:IsCity() ) then
o.m_CityFlag = UpdateCityCargo( pPlot );
end
else
if(PreGame.IsMultiplayerGame() and o.m_Player:IsHuman()) then
o.m_Instance.UnitIcon:SetToolTipString(Locale.ConvertTextKey("TXT_KEY_MULTIPLAYER_UNIT_TT", o.m_Player:GetNickName(), o.m_Player:GetCivilizationAdjectiveKey(), localizedNameWithlevelConvert)); -- localizedNameWithlevel used here.
else
o.m_Instance.UnitIcon:SetToolTipString(Locale.ConvertTextKey("TXT_KEY_PLOTROLL_UNIT_DESCRIPTION_CIV", o.m_Player:GetCivilizationAdjectiveKey(), localizedNameWithlevelConvert)); -- localizedNameWithlevel used here.
end
o.m_Instance.NormalButton:SetDisabled( true );
o.m_Instance.NormalButton:SetConsumeMouseOver( false );
o.m_Instance.HealthBarButton:SetDisabled( true );
o.m_Instance.HealthBarButton:SetConsumeMouseOver( false );
end
-- End Modification
---------------------------------------------------------
-- update all the info -- I tried fiddling around with the code below but anything I do crashed the file.
o:SetUnitColor();
o:SetUnitType();
o:UpdateFlagType();
o:UpdateHealth();
o:UpdateSelected();
o:SetFogState( fogState );
o:UpdateFlagOffset();
o:UpdateVisibility();
---------------------------------------------------------
-- Set the world position
local worldPosX, worldPosY, worldPosZ = GridToWorld( pUnit:GetX(), pUnit:GetY() );
worldPosZ = worldPosZ + 35;
o:UnitMove( worldPosX, worldPosY, worldPosZ );
end,
When in game hovering the cursor over a unit gives you the unit's level just like the unit panel. However it's not live. When the unit gains levels the pop up stays the same. The pop up does update with the proper unit level when the unit upgrades though. The pop up also works when hovering the cursor over other civ's units and barbarians, however I haven't done testing to see how it behaves when AI units gain levels or upgrade.
So does anyone have an idea on how to make the pop up in UnitFlagManager.lua update the unit level in real time just like the one UnitPanel.lua?
Any input would be greatly appreciated.
I've included the files with the post for those of you that feel like testing.
I'm experimenting with my first somewhat useful mod that includes LUA. This mod includes the unit's level with the unit name in both UnitPanel.lua and UnitFlagManager.lua. I got the code working just fine for the UnitPanel. It adds the unit's level before the unit's name and even references Language_en_US for text. The code for this is below:
Spoiler :
XML File:
<GameData>
<Language_en_US>
<Row Tag="TXT_KEY_UPANEL_LEVEL_ADDED_TO_UNIT_NAME">
<Text>Lv</Text>
</Row>
</Language_en_US>
</GameData>
LUA File:
-- Start Modification
--------------------------------------------------------------------------------
-- Refresh unit portrait and name
--------------------------------------------------------------------------------
function UpdateUnitPortrait( unit )
local ileveltext = unit:GetLevel(); -- Returns Unit Level (Same as tool tip below).
local name = unit:GetName(); -- Retrieves the name of the unit (Original file).
local Levelinunitname = Locale.ConvertTextKey("TXT_KEY_UPANEL_LEVEL_ADDED_TO_UNIT_NAME"); -- Converts "Lv" from the Language_en_US in level.xml to text.
name = Locale.ToUpper(name); -- Sets all uppercase for unit name (Original file).
--local name = unit:GetNameKey(); -- Disabled in original file.
local ileveltextconvert = Locale.ConvertTextKey(ileveltext); -- Converts unit level to text.
local convertedKey = Locale.ConvertTextKey(name); -- Converts the uppercase unit name to text (Original file).
convertedKey = Locale.ToUpper(convertedKey); -- Sets all uppercase for "convertedKey". Seems redundant. Not sure if needed. (Original file).
Controls.UnitName:SetText(Levelinunitname..ileveltextconvert.." "..convertedKey); -- Not pretty but it works. Space between Lv1 and Unit added here. Now sure how else to do it.
--Controls.UnitName:SetText(convertedKey); -- Prints "convertedKey" in game. (Original file).
Controls.UnitName:SetFontByName("TwCenMT24");
-- End Modification
This code works great in the UnitPanel. It updates live as the unit gains levels. I've tested this with the Live Tuner.
However it doesn't work so great in the UnitFlagManager.lua. The code is very similar but a bit different:
Spoiler :
-- Modified by FredMB
------------------------------------------------------------------
-- constructor
------------------------------------------------------------------
Initialize = function( o, playerID, unitID, fogState, invisible )
o.m_Player = Players[ playerID ];
o.m_PlayerID = playerID;
o.m_UnitID = unitID;
if( g_PrintDebug ) then print( string.format( "Creating UnitFlag for: Player[%i] Unit[%i]", playerID, unitID ) ); end
local pUnit = Players[ playerID ]:GetUnitByID( unitID );
local ileveltext2 = pUnit:GetLevel(); -- Returns Unit Level (Same as UnitPanel.lua).
local ileveltextconvert2 = Locale.ConvertTextKey(ileveltext2); -- Converts unit level to text.
local Levelinunitname2 = Locale.ConvertTextKey("TXT_KEY_UPANEL_LEVEL_ADDED_TO_UNIT_NAME"); -- Converts "Lv" from the Language_en_US in level.xml to text.
if( pUnit == nil )
then
print( string.format( "Unit not found for UnitFlag: Player[%i] Unit[%i]", playerID, unitID ) );
return nil;
end
o.m_IsCivilian = not pUnit:IsCombatUnit();
o.m_IsInvisible = invisible;
-- Technically, we should get a UnitGarrisoned event after the creation event if
-- the unit is garrisoned. So IsGarrisoned should always be false at creation.
-- In the interest of preserving behavior I'm allowing m_IsGarrisoned to be set
-- using IsGarrisoned() on creation. However, in the strategic view this causes
-- a visibility error in some odd cases so there it always starts as false.
if( InStrategicView() )
then
o.m_IsGarrisoned = false;
else
o.m_IsGarrisoned = pUnit:IsGarrisoned();
end
---------------------------------------------------------
-- Hook up the button
local pPlayer = Players[Game.GetActivePlayer()];
local active_team = pPlayer:GetTeam();
local team = o.m_Player:GetTeam();
--local name = pUnit:GetNameKey();
--local localizedName = Locale.ConvertTextKey(name);
local localizedName = pUnit:GetName();
localizedNameWithlevel = (Levelinunitname2..ileveltextconvert2.." "..localizedName); --New line similar to UnitPanel.
local localizedNameWithlevelConvert = Locale.ConvertTextKey(localizedNameWithlevel); --Converts localizedNameWithlevel to text.
if( active_team == team )
then
o.m_Instance.NormalButton:SetVoid1( playerID );
o.m_Instance.NormalButton:SetVoid2( unitID );
o.m_Instance.NormalButton:RegisterCallback( Mouse.eLClick, UnitFlagClicked );
o.m_Instance.NormalButton:RegisterCallback( Mouse.eMouseEnter, UnitFlagEnter );
o.m_Instance.NormalButton:RegisterCallback( Mouse.eMouseExit, UnitFlagExit );
o.m_Instance.NormalButton:SetDisabled( false );
o.m_Instance.NormalButton:SetConsumeMouseOver( true );
if(PreGame.IsMultiplayerGame() and o.m_Player:IsHuman()) then
local unitOwner = Players[playerID];
local string = Locale.ConvertTextKey("TXT_KEY_MULTIPLAYER_UNIT_TT", o.m_Player:GetNickName(), o.m_Player:GetCivilizationAdjectiveKey(), localizedNameWithlevelConvert ); -- localizedNameWithlevel used here.
if( playerID == Game.GetActivePlayer() ) then
string = string .. Locale.ConvertTextKey( "TXT_KEY_UPANEL_CLICK_TO_SELECT" );
end
o.m_Instance.UnitIcon:SetToolTipString( string );
else
o.m_Instance.UnitIcon:SetToolTipString(Locale.ConvertTextKey("TXT_KEY_PLOTROLL_UNIT_DESCRIPTION_CIV", o.m_Player:GetCivilizationAdjectiveKey(), localizedNameWithlevelConvert).. Locale.ConvertTextKey( "TXT_KEY_UPANEL_CLICK_TO_SELECT" )); -- localizedNameWithlevel used here.
end
o.m_Instance.HealthBarButton:SetVoid1( playerID );
o.m_Instance.HealthBarButton:SetVoid2( unitID );
o.m_Instance.HealthBarButton:RegisterCallback( Mouse.eLClick, UnitFlagClicked );
o.m_Instance.HealthBarButton:RegisterCallback( Mouse.eMouseEnter, UnitFlagEnter );
o.m_Instance.HealthBarButton:RegisterCallback( Mouse.eMouseExit, UnitFlagExit );
o.m_Instance.HealthBarButton:SetDisabled( false );
o.m_Instance.HealthBarButton:SetConsumeMouseOver( true );
local pPlot = pUnit:GetPlot();
if( pPlot:IsCity() ) then
o.m_CityFlag = UpdateCityCargo( pPlot );
end
else
if(PreGame.IsMultiplayerGame() and o.m_Player:IsHuman()) then
o.m_Instance.UnitIcon:SetToolTipString(Locale.ConvertTextKey("TXT_KEY_MULTIPLAYER_UNIT_TT", o.m_Player:GetNickName(), o.m_Player:GetCivilizationAdjectiveKey(), localizedNameWithlevelConvert)); -- localizedNameWithlevel used here.
else
o.m_Instance.UnitIcon:SetToolTipString(Locale.ConvertTextKey("TXT_KEY_PLOTROLL_UNIT_DESCRIPTION_CIV", o.m_Player:GetCivilizationAdjectiveKey(), localizedNameWithlevelConvert)); -- localizedNameWithlevel used here.
end
o.m_Instance.NormalButton:SetDisabled( true );
o.m_Instance.NormalButton:SetConsumeMouseOver( false );
o.m_Instance.HealthBarButton:SetDisabled( true );
o.m_Instance.HealthBarButton:SetConsumeMouseOver( false );
end
-- End Modification
---------------------------------------------------------
-- update all the info -- I tried fiddling around with the code below but anything I do crashed the file.
o:SetUnitColor();
o:SetUnitType();
o:UpdateFlagType();
o:UpdateHealth();
o:UpdateSelected();
o:SetFogState( fogState );
o:UpdateFlagOffset();
o:UpdateVisibility();
---------------------------------------------------------
-- Set the world position
local worldPosX, worldPosY, worldPosZ = GridToWorld( pUnit:GetX(), pUnit:GetY() );
worldPosZ = worldPosZ + 35;
o:UnitMove( worldPosX, worldPosY, worldPosZ );
end,
When in game hovering the cursor over a unit gives you the unit's level just like the unit panel. However it's not live. When the unit gains levels the pop up stays the same. The pop up does update with the proper unit level when the unit upgrades though. The pop up also works when hovering the cursor over other civ's units and barbarians, however I haven't done testing to see how it behaves when AI units gain levels or upgrade.
So does anyone have an idea on how to make the pop up in UnitFlagManager.lua update the unit level in real time just like the one UnitPanel.lua?
Any input would be greatly appreciated.
I've included the files with the post for those of you that feel like testing.