Help InitUnit multiple units in a loop (lua)

Tomatekh

Emperor
Joined
Aug 6, 2012
Messages
1,434
So I wrote this bit of code which is supposed to check if you're friends or higher with a city-state. If so, every x turns, initunit a unit in any city owned by a friendly city-state.

Spoiler :
Code:
function TupiCSAllies(iPlayer, iCurrentTurn)
	local pPlayer = Players[iPlayer];
	for i = GameDefines.MAX_MAJOR_CIVS, GameDefines.MAX_CIV_PLAYERS - 2 do 
		local tPlayer = Players[i];
		if (tPlayer:GetMinorCivFriendshipLevelWithMajor(pPlayer:GetID()) >= 1) then
			for cityindex = tPlayer:GetNumCities() - 1, 1 do
				local tCity = tPlayer:GetCityByID(cityindex);
				--Test Unit
				local TestUnit = "UNIT_WARRIOR"
				pUnit = pPlayer:InitUnit(GameInfoTypes[TestUnit], tCity:GetX(), tCity:GetY())
				pUnit:JumpToNearestValidPlot()
			end
		end
	end
end

function TupiGameSpeed(iPlayer, iCurrentTurn)
	local speed = Game.GetGameSpeedType();
	if speed == GameInfo.GameSpeeds['GAMESPEED_QUICK'].ID then
		if ( iCurrentTurn % 6 ) == 0 then
			TupiCSAllies(iPlayer, iCurrentTurn)
		end
	elseif speed == GameInfo.GameSpeeds['GAMESPEED_STANDARD'].ID then
		if ( iCurrentTurn % 10 ) == 0 then
			TupiCSAllies(iPlayer, iCurrentTurn)
		end
	elseif speed == GameInfo.GameSpeeds['GAMESPEED_EPIC'].ID then
		if ( iCurrentTurn % 15 ) == 0 then
			TupiCSAllies(iPlayer, iCurrentTurn)
		end
	elseif speed == GameInfo.GameSpeeds['GAMESPEED_MARATHON'].ID then
		if ( iCurrentTurn % 30 ) == 0 then
			TupiCSAllies(iPlayer, iCurrentTurn)
		end
	end
end

function TupiCityStateScript(iPlayer)
	local iCurrentTurn = Game.GetGameTurn()
	if (Players[iPlayer]:IsAlive()) then
		--Test to fire with a civ, change to check for a trait later
		if (Players[iPlayer]:GetCivilizationType() == GameInfoTypes["CIVILIZATION_AZTEC"]) then
			TupiGameSpeed(iPlayer, iCurrentTurn)
		end
	end
end
GameEvents.PlayerDoTurn.Add(TupiCityStateScript)
The code does work except it will only spawn one unit and in the first friendly city-state of the list regardless of if you are friendly with multiple city states. Any advice as to how to get it to spawn a unit for every friendly city-state?
 
Code:
for cityindex = tPlayer:GetNumCities() - 1, 1 do
  local tCity = tPlayer:GetCityByID(cityindex);

  ...
end
is wrong. The ID of a city is (effectively) a random number and not a sequential index. So tCity is almost certainly going to be nil which, thanks to the complete lack of error reporting in GameEvents, will cause the entire code to fail silently

use

Code:
for tCity in tPlayer:Cities() do
  ...
end

instead, as that directly iterates each city owned by the player
 
Thanks that fixed it perfectly.

Instead of starting a new thread, I figure I'll just ask as its related to my posted code.

If I wanted to spawn the highest level of a unit from a list that the civ can train instead of a warrior every time, I assume I could set up something like:

Code:
local LandMelee = { 
	GameInfoTypes.UNIT_PIKEMAN, 
	GameInfoTypes.UNIT_SPEARMAN, 
	GameInfoTypes.UNIT_WARRIOR
}

for _, iUnit in pairs(LandMelee) do

And also check if the capital of the pPlayer, pCity:CanTrain( iUnit ) to see if the player can train the units, but how can I make it only spawn the highest unit and not all the ones that city can train? Sorry if that's confusing.
 
Use break to exit the loop once you've found the first one that can be trained

(pseudo-lua)
Code:
-- Note that the first entry MUST be the most powerful, then the next, etc
units = {"Longswordman", "swordman", "warrior"}

for _, unit in ipairs(units) do
  if canTrain(unit) then
    spawn(unit)
    [COLOR="Red"]break[/COLOR]
  end
end
 
Back
Top Bottom