Help getting Lua to fire only once a time range

Craig_Sutter

Deity
Joined
Aug 13, 2002
Messages
2,773
Location
Calgary, Canada
I've created the following code meant to spawn units at a specific game year. I have found simply doing Game.GetGameTurnYear() == 501 does not work always so I am using a range of years to cover instances where 1 turn is 3 years. Even if the previously mentioned game year worked, if there are more than one turn in a year, the function fires each turn.

I am not familiar with using a count to fire of Lua functions. I suspect I need to set a count so that if count<= one, the function will fire, and if not, it well end.

I do not know how to do this.

I also foresee a problem if the script does not remember the count and a game is saved, the script will not remember the count and fire as if it were the first go.

The latter problem is a minor one, however, and I'm willing to trade simplicity for probability it does not matter a whole lot if such happens.

Can anyone give me a hint about how to go about limiting the number of times my units will spawn within the given range of years?

BTW, the reason I am using year rather than turn is that I want to use the lua for all game speeds.

Code:
function SpawnAngles()

if Game.GetGameTurnYear() >= 501 and Game.GetGameTurnYear() <= 503 then

-- Set up Angles Player

	for iPlayer=GameDefines.MAX_MAJOR_CIVS, GameDefines.MAX_CIV_PLAYERS-1, 1 do 

	local pAngles = Players[iPlayer]

		if (GameInfo.MinorCivilizations.MINOR_CIV_ANGLES.ID == pAngles:GetMinorCivType()) then
	
		Angles = pAngles	
							
			-- Enumerate cities
			for cityIndex = 0, Angles:GetNumCities() - 1, 1 do

    			local pCity = Angles:GetCityByID(cityIndex);
				local pPlot = pCity:GetCityIndexPlot();
				local Spawnunit;
				local iSpawnX = pPlot:GetX();
				local iSpawnY = pPlot:GetY();
				Spawnunit = Angles:InitUnit(GameInfoTypes["UNIT_SEAXMAN"], iSpawnX, iSpawnY, UNITAI_ATTACK, DIRECTION_NORTHWEST );
			end
		end
	end
end
end

Events.ActivePlayerTurnEnd.Add(SpawnAngles)

Any help would be appreciated.

Regards. Craig
 
Simple "don't care about the saves" solution

Code:
[COLOR="Magenta"]local haveSpawnedAngles = false[/COLOR]

function SpawnAngles()
  [COLOR="magenta"]if (haveSpawnedAngles == false) then[/COLOR]
    if (Game.GetGameTurnYear() >= 501 and Game.GetGameTurnYear() <= 503) then
    -- Set up Angles Player
      [COLOR="magenta"]haveSpawnedAngles = true[/COLOR]

      for iPlayer=GameDefines.MAX_MAJOR_CIVS, GameDefines.MAX_CIV_PLAYERS-1, 1 do 
        local pAngles = Players[iPlayer]

        if (GameInfo.MinorCivilizations.MINOR_CIV_ANGLES.ID == pAngles:GetMinorCivType()) then
          Angles = pAngles  

          -- Enumerate cities
          for cityIndex = 0, Angles:GetNumCities() - 1, 1 do
            local pCity = Angles:GetCityByID(cityIndex);
            local pPlot = pCity:GetCityIndexPlot();
            local Spawnunit;
            local iSpawnX = pPlot:GetX();
            local iSpawnY = pPlot:GetY();
            Spawnunit = Angles:InitUnit(GameInfoTypes["UNIT_SEAXMAN"], iSpawnX, iSpawnY, UNITAI_ATTACK, DIRECTION_NORTHWEST );
          end
        end
      end
    end
  [COLOR="Magenta"]end[/COLOR]
end
Events.ActivePlayerTurnEnd.Add(SpawnAngles)
 
Enhancement to cope with saved games

Code:
[COLOR="Red"]local modData = Modding.OpenSaveData()
local modAnglesKey = "somethingUnique_AnglesHaveSpawned"[/COLOR]

local haveSpawnedAngles = [COLOR="red"](modData.GetValue(modAnglesKey) == 1)[/COLOR]

function SpawnAngles()
  if (haveSpawnedAngles == false) then
    [B][COLOR="Red"]if (Game.GetGameTurnYear() >= 501) then[/COLOR][/B]
    -- Set up Angles Player
      haveSpawnedAngles = true
      [COLOR="red"]modData.SetValue(modAnglesKey, 1)[/COLOR]

      for iPlayer=GameDefines.MAX_MAJOR_CIVS, GameDefines.MAX_CIV_PLAYERS-1, 1 do 
        local pAngles = Players[iPlayer]

        if (GameInfo.MinorCivilizations.MINOR_CIV_ANGLES.ID == pAngles:GetMinorCivType()) then
          Angles = pAngles  

          -- Enumerate cities
          for cityIndex = 0, Angles:GetNumCities() - 1, 1 do
            local pCity = Angles:GetCityByID(cityIndex);
            local pPlot = pCity:GetCityIndexPlot();
            local Spawnunit;
            local iSpawnX = pPlot:GetX();
            local iSpawnY = pPlot:GetY();
            Spawnunit = Angles:InitUnit(GameInfoTypes["UNIT_SEAXMAN"], iSpawnX, iSpawnY, UNITAI_ATTACK, DIRECTION_NORTHWEST );
          end
        end
      end
    end
  end
end
Events.ActivePlayerTurnEnd.Add(SpawnAngles)

You no longer need the "less than 503" condition as that's taken care of by the load/save of the haveSpawnedAngles flag (unless if a player starts a game after the year 503 you don't want the units to spawn, then you'll need slightly different logic)
 
Back
Top Bottom