I am trying to make a unit change to a different unit and then back again and cost -1 turn every time the mission button is pressed. It works the first time but not the second or 50th time I press the action button and the unit can perform the action an infinite number of times and only costs it 1 turn. I need it to remember the turns remaining when it spawns the new unit.
Code:
function CanDoCannonChange(playerID, unitID, bTestVisible)
if (Players[playerID]:GetUnitByID(unitID):GetUnitType() == GameInfoTypes.UNIT_MOVE_CANNON) then
return true
end
return false
end
GameEvents.CanDoCannonChange.Add(CanDoCannonChange)
function DoCannonChange(playerID, unitID)
local pUnit = Players[playerID]:GetUnitByID(unitID)
local pUnitCannon = Players[playerID]:InitUnit(GameInfoTypes["UNIT_CANNON"], pUnit:GetX(), pUnit:GetY())
pUnitCannon:SetEmbarked(oldUnit:IsEmbarked())
pUnitCannon:SetDamage(pUnit:GetDamage())
pUnitCannon:SetExperience(pUnit:GetExperience())
pUnitCannon:SetLevel(pUnit:GetLevel())
pUnitCannon:SetMoves(pUnit:GetMoves())
pUnitCannon:ChangeMoves(-1)
for unitPromotion in GameInfo.UnitPromotions() do
local iPromotionID = unitPromotion.ID;
if (pUnit:IsHasPromotion(iPromotionID)) then
if (pUnitCannon:IsPromotionValid(iPromotionID)) then
pUnitCannon:SetHasPromotion(iPromotionID, true)
end
end
end
pUnit:Kill(true);
end
GameEvents.DoCannonChange.Add(DoCannonChange)
function CanDoMoveChange(playerID, unitID, bTestVisible)
if (Players[playerID]:GetUnitByID(unitID):GetUnitType() == GameInfoTypes.UNIT_CANNON) then
return true
end
return false
end
GameEvents.CanDoMoveChange.Add(CanDoMoveChange)
function DoMoveChange(playerID, unitID)
local pUnit = Players[playerID]:GetUnitByID(unitID)
local pUnitMove = Players[playerID]:InitUnit(GameInfoTypes["UNIT_MOVE_CANNON"], pUnit:GetX(), pUnit:GetY())
pUnitMove:SetEmbarked(oldUnit:IsEmbarked())
pUnitMove:SetDamage(pUnit:GetDamage())
pUnitMove:SetExperience(pUnit:GetExperience())
pUnitMove:SetLevel(pUnit:GetLevel())
pUnitMove:SetMoves(pUnit:GetMoves())
pUnitMove:ChangeMoves(-1)
for unitPromotion in GameInfo.UnitPromotions() do
local iPromotionID = unitPromotion.ID;
if (pUnit:IsHasPromotion(iPromotionID)) then
if (pUnitMove:IsPromotionValid(iPromotionID)) then
pUnitMove:SetHasPromotion(iPromotionID, true)
end
end
end
pUnit:Kill(true);
end
GameEvents.DoMoveChange.Add(DoMoveChange)