Lua CanConstruct not working as expected.

Craig_Sutter

Deity
Joined
Aug 13, 2002
Messages
2,773
Location
Calgary, Canada
This lua function should allow me to construct a booster in the affected city...

Code:
--allows building of Procession unit in original capital

local iProcession = GameInfoTypes.UNIT_SS_COCKPIT

function CityTrainProcessionUnit(iPlayer, iCity, iUnit)
	--differentiate Procession units
	if iUnit == iProcession then
		local player = Players[iPlayer]			
		local pCity = player:GetCityByID(iCity)
		
		[COLOR="Red"]if pCity:IsOriginalCapital() and player == pCity:GetOriginalOwner() then[/COLOR]
			
			return true --city is original capital and can construct unit
		else
			return false -- disable the unit if the city is not the original capital
		end
	end
	return true -- default to true if the unit is not an SS_COCKPIT
end

GameEvents.CityCanTrain.Add(CityTrainProcessionUnit)

So, given this, the player should be able to build the unit in his original capital. You may ask why I do it this way, and just allow him to create the unit in his capital... but another bit of code I use makes a different city his capital....

Code:
--resets capital if London owned by player

function ResetCapital(iPlayer)

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

	local pPlayer = Players[iPlayer]
	local pPlayerTeamID = pPlayer:GetTeam();
	local pPlayerTeam = Teams[ pPlayer:GetTeam() ]

	if (pPlayerTeam : GetProjectCount( GameInfoTypes["PROJECT_APOLLO_PROGRAM"]) > 0 ) then 

		-- Enumerate cities
			
		local oldCapital = pPlayer:GetCapitalCity()
		for pCity in pPlayer:Cities() do			
			
			-- City exists and and is London and it is not the current capital?

			if pCity ~= nil and pCity:GetName() == "London" and not pCity:IsCapital() then 
					
				pCity:SetNumRealBuilding(GameInfoTypes["BUILDING_PALACE"], 1);
				oldCapital:SetNumRealBuilding(GameInfoTypes["BUILDING_PALACE"], 0);
    			
				return pCity							
    		end
		end		
	end
end


--Operate functions to loop through all players

local function OnAllPlayersDoTurn(iPlayer)			
		
		ResetCapital(iPlayer)

end
GameEvents.PlayerDoTurn.Add(OnAllPlayersDoTurn)

The upshot is that London will be the player's capital. The idea is that the Royal Procession will be built in the player's original capital and move to London to complete its part of the spaceship.

The code moving the capital to London does work, no problem. But the code that allows building the unit in the players original capital does not. I suspect the entry done in red to be causing the error, but cannot figure out what is wrong with my logic.

Advice, as always, is appreciated.
 
player == pCity:GetOriginalOwner()

will NEVER be true.

player is an object (a good reason to call this pPlayer) whereas pCity:GetOriginalOwner() returns the id (an integer) of the original player, you want iPlayer
 
Back
Top Bottom