Craig_Sutter
Deity
I am using the following code to generate units in city state cities. It appears to work fine on the first turn of the game (turn 1 in AD 500) and my print statements and looking at the result in game seems to bear this out.
However, a problem arises when a city state city is captured. Many of my city states have more than one city. If I set the year date at AD 501 to 504, for example, I will get a log message of "attempt to index local 'pCity' (a nil value)" in line 45 of the code (Highlighted in red). For some reason the city index gets screwed up by a conquest of one of the city state cities.
I have tried various means to correct this... used different if statements such as "neverconquered" or checking "isowner" and such, but none of them have made a difference. I've shuffled the order a bit by relocating the local variable statements withing the city enumeration code and have not moved forward. Except for occasionally getting no print statements at all (due to the logic of the "if" exclusions), I have failed to move forward to eliminate the error.
I've also tried to figure out alternate means of looping through the cities, thinking perhaps, that might get around problems accessing the database, but I can't figure out any other way to do it and scanning the game files, the method seems pretty standard.
I thought the problem is that for some reason the database is not updating for the conquests. Firetuner shows, though, that the city ownership changes via conquest have been changed in the database.
The only thing I can think of is that there might be some lua function to refresh the data on city ownership so the change circumstances can be accounted for... but non of the update functions I've found in lua seem to do the trick.
Anyhow, here is the code... it works, but only if no cities have been conquered. After conquests, it works until the loop reaches an affected city, then I get the error.
If I can't fix the error, the only way I can come up with is to treat each city state separately, with its own function, and live with the error being contained to only affected city states.
Here is the code:
I hope someone has run into something similar and can lead me in the right direction on this. Thanks.
However, a problem arises when a city state city is captured. Many of my city states have more than one city. If I set the year date at AD 501 to 504, for example, I will get a log message of "attempt to index local 'pCity' (a nil value)" in line 45 of the code (Highlighted in red). For some reason the city index gets screwed up by a conquest of one of the city state cities.
I have tried various means to correct this... used different if statements such as "neverconquered" or checking "isowner" and such, but none of them have made a difference. I've shuffled the order a bit by relocating the local variable statements withing the city enumeration code and have not moved forward. Except for occasionally getting no print statements at all (due to the logic of the "if" exclusions), I have failed to move forward to eliminate the error.
I've also tried to figure out alternate means of looping through the cities, thinking perhaps, that might get around problems accessing the database, but I can't figure out any other way to do it and scanning the game files, the method seems pretty standard.
I thought the problem is that for some reason the database is not updating for the conquests. Firetuner shows, though, that the city ownership changes via conquest have been changed in the database.
The only thing I can think of is that there might be some lua function to refresh the data on city ownership so the change circumstances can be accounted for... but non of the update functions I've found in lua seem to do the trick.
Anyhow, here is the code... it works, but only if no cities have been conquered. After conquests, it works until the loop reaches an affected city, then I get the error.
If I can't fix the error, the only way I can come up with is to treat each city state separately, with its own function, and live with the error being contained to only affected city states.
Here is the code:
Code:
local haveSpawnedMinorA = false
function SpawnMinorA()
if (haveSpawnedMinorA == false) then
if (Game.GetGameTurnYear() >= 500 and Game.GetGameTurnYear() <= 502) then
-- Set up MinorA Player
haveSpawnedMinorA = true
for iPlayer=GameDefines.MAX_MAJOR_CIVS, GameDefines.MAX_CIV_PLAYERS-1, 1 do
local pMinorA = Players[iPlayer]
MinorA = pMinorA
-- Enumerate cities
local cityCount = -1
for cityIndex = 0, MinorA:GetNumCities() - 1, 1 do
cityCount = cityCount + 1
if cityCount < (MinorA:GetNumCities()/2) then
local pCity = MinorA:GetCityByID(cityIndex);
[COLOR="Red"]local pPlot = pCity:GetCityIndexPlot();[/COLOR]
local Spawnunit;
local iSpawnX = pPlot:GetX();
local iSpawnY = pPlot:GetY();
Spawnunit = MinorA:InitUnit(GameInfoTypes["UNIT_SWORDSMAN"], iSpawnX, iSpawnY, UNITAI_ATTACK, DIRECTION_NORTHWEST );
print (pCity:GetName() ,"...is spawning Swordsman... ");
end
end
end
end
end
end
Events.ActivePlayerTurnEnd.Add(SpawnMinorA)
I hope someone has run into something similar and can lead me in the right direction on this. Thanks.