[LUA] Working as intended?

Craig_Sutter

Deity
Joined
Aug 13, 2002
Messages
2,773
Location
Calgary, Canada
In my mod, several civilizations start with no cities.

When playtesting, I found that they were puppeting their newly acquired capital cities and well as others.

I have added a courthouse effect to the palace, and so, I don't want the capitals to be puppets.

I used the following code to do so:

Code:
function UnPuppetTurnStart ()
	for iPlayer=0, GameDefines.MAX_MAJOR_CIVS-1 do
		local player=Players [iPlayer];
		if (player:IsAlive () and not player:IsHuman()) then
		local pCity = player:GetCapitalCity()
			for pCity in player:Cities () do
				if (pCity:IsPuppet ()) then
					pCity:SetPuppet (false);
				end
			end
		end
	end
end

Events.ActivePlayerTurnStart.Add (UnPuppetTurnStart);

It works in that the capital cities are no longer puppets; however, I suspect that the code is actually doing more and forcing all AI cities that are conquered to be annexed. I not certain this is the case, but it seems to be so as I no longer see puppets in my testing.

Could someone who is familiar with lua look at my code and verify if it is acting as I wish? Or have some unintentional results occured?
 
the "for pCity in player:Cities () do..." is not needed, if you do that, you'll loop all the player cities at each turn.
 
I got rid of the city loop, but then I run into the problem that pCity becomes a "nil" value, and I get an error. I was not skilled enough to fix it though I tried a number of solutions.

In any case, I've found an alternative solution that works (although it is somewhat clunky):

Code:
function UnPuppetYork()

for iPlayer=0, GameDefines.MAX_MAJOR_CIVS-1 do 

	local pNorthumbria = Players[iPlayer]

	if (GameInfo.Civilizations.CIVILIZATION_RUSSIA.ID == pNorthumbria:GetCivilizationType()) then
		if(pNorthumbria:IsAlive() and not pNorthumbria:IsHuman()) then 
			
			-- Enumerate cities
			
			for cityIndex = 0, pNorthumbria:GetNumCities() - 1, 1 do
    			local pCity = pNorthumbria:GetCityByID(cityIndex)

				-- City exists and has the proper name?

    			if pCity ~= nil and pCity:GetName() == "York" then
				pCity:SetPuppet (false);
				end
			end
		end
	end
end
end
									
Events.ActivePlayerTurnStart.Add (UnPuppetYork);

It is not very flexible in its application as it is specific to a particular city, but as it is for a scenario, it is good enough.

(although I would still like to get the previous code working properly, as it would be a more elegant solution).
 
try:

Code:
function UnPuppetTurnStart ()
	for iPlayer=0, GameDefines.MAX_MAJOR_CIVS-1 do
		local player=Players [iPlayer];
		if (player:IsAlive () and not player:IsHuman()) then
			local pCity = player:GetCapitalCity()
			if pCity and pCity:IsPuppet () then
				pCity:SetPuppet (false);
			end
		end
	end
end

Events.ActivePlayerTurnStart.Add (UnPuppetTurnStart);

Lua check "and" starting with the left element, if it's nil it won't try the right element...
 
Back
Top Bottom