Need help with basic lua operation

Craig_Sutter

Deity
Joined
Aug 13, 2002
Messages
2,773
Location
Calgary, Canada
I'm trying to call a function from within another function...

function CitiesConvert(pPlayer) is called from within function MinorConvert(iPlayer, eReligion, iX, iY)

I seem unable to do it although I've tried many variations. The print statements do work if I set don't use the CitiesConvert function, but rather, use the actual statements within the function inside of MinorConvert in place of where CitiesConvert sits..

I know this is a pretty basic thing to do, calling functions from other functions, but in this case I am stumped. I'm missing some local or nonlocal variable, I think.

Code:
function MinorConvert(iPlayer, eReligion, iX, iY)

local plot = Map.GetPlot(iX, iY);
local iPlayer = plot:GetOwner()
local pPlayer = Players[iPlayer]
local civType = pPlayer:GetMinorCivType()

--check if player alive, Catholic and a minor

	if eReligion == GameInfoTypes["RELIGION_CHRISTIANITY"] and pPlayer:IsAlive () then

		if	civType == GameInfo.MinorCivilizations["MINOR_CIV_SUSSEX"].ID
		
		--The next are commented out for testing purposes		
		--[[ or
			civType == GameInfo.MinorCivilizations["MINOR_CIV_ESSEX"].ID or
			 civType == GameInfo.MinorCivilizations["MINOR_CIV_KENT"].ID or
			 civType == GameInfo.MinorCivilizations["MINOR_CIV_EAST_ANGLIA"].ID]] 
			 
		then

		print(pPlayer:GetName(), "...Chosing Player...");
		
			-- the player is set

			if	civType == GameInfo.MinorCivilizations["MINOR_CIV_SUSSEX"].ID then
			
			print(pPlayer:GetName(), "...Confirming Player...");

			CitiesConvert(pPlayer)

			end
		end
	end
end	
	

GameEvents.CityConvertsReligion.Add (MinorConvert);


function CitiesConvert(pPlayer)

	--if only one city stop

	if pPlayer:GetNumCities() == 1 then
	print(pPlayer:GetName(), "...one city...");

		--if 2 cities	

	elseif pPlayer:GetNumCities() == 2 then
	print(pPlayer:GetName(), "...two cities...");
		for pCity in pPlayer:Cities() do

		Building = pCity:GetNumRealBuilding(GameInfoTypes["BUILDING_CHURCH"])

			if Building == 0 then

			pCity:ConvertPercentFollowers(2, -1, 75);
			pCity:ConvertPercentFollowers(2, 1, 75);
			pCity:ConvertPercentFollowers(2, 3, 50);
			pCity:ConvertPercentFollowers(2, 4, 50);
			print(pCity:GetName(), "...is getting Catholic followers...");

			end
		end
								
	--if majority of cities is Catholic	

	elseif (pPlayer:HasReligionInMostCities(eReligion)) then
	print(pPlayer:GetName(), "...more than two cities...");

		for pCity in pPlayer:Cities() do

		Building = pCity:GetNumRealBuilding(GameInfoTypes["BUILDING_CHURCH"])

			if Building == 0 then

			pCity:ConvertPercentFollowers(2, -1, 75);
			pCity:ConvertPercentFollowers(2, 1, 75);
			pCity:ConvertPercentFollowers(2, 3, 50);
			pCity:ConvertPercentFollowers(2, 4, 50);
			print(pCity:GetName(), "...is getting Catholic followers...");

			end
		end
	end
end

Any advice is appreciated. Thanks.
 
Since your function is driven by a GameEvent, I don't think it will report errors correctly (as Pazyryk has discussed elsewhere). Try adding a simple caller function that's hooked onto the event:

Code:
function MinorConvertCaller(iPlayer, eReligion, iX, iY)
    print(pcall(MinorConvert(iPlayer, eReligion, iX, iY)))
end
GameEvents.CityConvertsReligion.Add (MinorConvertCaller)

Using pcall means you should get debugging results in FireTuner (though filepaths will be truncated, chances are you'll recognize the error from variable names).
 
Back
Top Bottom