Thalassicus
Bytes and Nibblers
How can I apply a trait promotion to workers?
function SpatzOnUnitCreated(iPlayer,iUnit,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11,arg12,arg13)
mPlayer = Players[iPlayer];
mUnit = mPlayer:GetUnitByID(iUnit);
mType = mUnit:GetUnitType();
if (GameInfo.Units[mType].Combat < 1 and mPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_AMERICA) then
mUnit:SetHasPromotion(GameInfoTypes.PROMOTION_SENTRY,true);
endif
end
Events.SerialEventUnitCreated.Add( SpatzOnUnitCreated );
Want certain unit types to start with additional XP? Lua.
How can I distinguish between these circumstances:
SerialEventUnitCreated fires for #2, but what I need is #1.
- Unit created/trained
- Unit initialized
Well, the only post-creation "initialized" triggers I know of are embarkation and disembarkation. (Debarkation?) What other ones are there?
Units are initialized each time the game is loaded
But actually, in your case, why do you care? You're giving a promotion to the unit.
I give additional XP to AI units when they start.Want certain unit types to start with additional XP? Lua.
GetTurnCreated doesn't work because it would double-apply effects to units that were created the turn a loaded savegame was saved.
For the temporary promotion, do you give it to all units through the XML?
OnUnitCreated()
if (Unit:IsHasPromotion(GameInfoTypes.PROMOTION_GREEN) or (Unit:GetTurnCreated() < Game:GetCurrentTurn)) then
-- you already triggered a creation event for this unit, either this turn or previously
-- In this case, you probably want to do nothing
else
-- It's a newly-created unit!
Unit:SetHasPromotion(GameInfoTypes.PROMOTION_GREEN,true)
Unit:ChangeExperience(10)
end
(end function set to SerialEventUnitCreated)
and then, make a second function:
UnitCleanup()
for index,pPlayer in pairs(Players) do
for Unit in Players:Units() do
if Unit:IsHasPromotion(GameInfoTypes.PROMOTION_GREEN) then
Unit:SetHasPromotion(GameInfoTypes.PROMOTION_GREEN,false)
end
end
end
(end function set to ActivePlayerEndTurn)
1> If the unit has a Unit Combat Class (melee, gunpowder, etc.). Workers, Settlers, and Great People aren't assigned one. But it'd be easy enough to add a new one if this was the problem.