Craig_Sutter
Deity
I am using the following code in my mod. I want to select a group of units randomly based upon their combat type and assign them to an operation. However, I run into trouble when I try to loop through the units. I get a nil value for "unit". I've never looped through a player's units before, and can't seem to find a good example in the scenarios or in this forum.
I run into an error when I try to loop through the units. I'm doing something wrong, but I'm not good at lua and without a template, am having difficulties.
Any advice would be appreciated.
Thank-you.
Code:
function HladirAttackOnBamborough()
if Game.GetGameTurn() == 0 then
--set Hladir as player
local pHladir
for iPlayer=0, GameDefines.MAX_MAJOR_CIVS-1 do
local pHladir = Players[iPlayer]
if (pHladir:IsAlive() and not pHladir:IsHuman()) then
if (GameInfo.Civilizations.CIVILIZATION_SONGHAI.ID == pHladir:GetCivilizationType()) then
-- loop through Hladir units and find units with certain combat types
local unit = unit:GetID()
for unit in pHladir:Units() do
if (unit:UnitCombatType() == GameInfoTypes["UNITCOMBAT_MELEE"]) then
elseif (unit:UnitCombatType() == GameInfoTypes["UNITCOMBAT_NAVALMELEE"]) then
elseif (unit:UnitCombatType() == GameInfoTypes["UNITCOMBAT_ARCHER"]) then
elseif (unit:UnitCombatType() == GameInfoTypes["UNITCOMBAT_SIEGE"]) then
-- select half of those untis
if (Game.Rand(100, "Selecting attack unit") < 50) then
-- add that unit to operation
unit:SetDeployFromOperationTurn(1);
Players[iPlayer]:AddTemporaryDominanceZone (53, 48);
end
end
end
end
end
end
end
end
Events.ActivePlayerTurnEnd.Add(HladirAttackOnBamborough)
I run into an error when I try to loop through the units. I'm doing something wrong, but I'm not good at lua and without a template, am having difficulties.
Any advice would be appreciated.
Thank-you.