LUA: Making a action cost a turn

Wolfdog

Unit Butcher
Joined
Jun 29, 2012
Messages
660
Location
Australia
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)
 
unit:SetMoves(0) clears out an individual unit's remaining moves for that turn, regardless of whether the unit had 'full' or 'partial' moves.

maybe I am misunderstanding what is meant by
cost -1 turn
 
I take it to mean the unit loses its turn, but otherwise.. where are those GameEvents coming from? I don't believe I recall seeing any of them for the stock game.
 
I take it to mean the unit loses its turn, but otherwise.. where are those GameEvents coming from? I don't believe I recall seeing any of them for the stock game.
I just assumed it was coming from a custom DLL, but now that you mention it :dunno:
 
Ahhh, so you mean "moves". As Nutty says, you need to multiply it by the move denominator to get it to subtract them properly.
 
A "move" is not a simple integer. You need to multiply it by GameDefines.MOVE_DENOMINATOR (60, by default).

So for example I would put
pUnitCannon:ChangeMoves(GameDefines.MOVE_DENOMINATOR(*0.33))
and that would make it use 1 of 3 turns.

P.S. I am a lua noob so I just taking a stab here.:)
 
So for example I would put
pUnitCannon:ChangeMoves(GameDefines.MOVE_DENOMINATOR(*0.33))
and that would make it use 1 of 3 turns.

P.S. I am a lua noob so I just taking a stab here.:)

Not quite. 1 movement point will be (1 * GameDefines.MOVE_DENOMINATOR). Multiplying it by 0.33 will use up only 0.33 movement points. (Non-whole movement points are typically found when using roads and railroads, if you're curious.)

If you'll be using this frequently, consider localizing the move denominator:

local MOVE_DENOMINATOR = GameDefines.MOVE_DENOMINATOR;
 
Thank you very much everyone for all your help. You guys were spot on and the following code worked in case anyone is interested.:goodjob:

Code:
local MOVE_DENOMINATOR = GameDefines.MOVE_DENOMINATOR;
pUnitCannon:ChangeMoves(-1 * MOVE_DENOMINATOR)

Cheers
 
Back
Top Bottom