Help with religion check LUA

Damirith

Prince
Joined
Mar 11, 2015
Messages
379
I want to make a religious wonder, but have it that you cant build it unless you have your own religion. While I know I could just use <holycity> in the xml, I want the wonder to have the freedom to be built in any city in your empire and not just your "holy city". I seared the forums and found some code that I thought would work. Does this make sense to you guys?

Code:
function ReligionRestriction(playerID,buildingTypeID)
	local player = Players[playerID];

	-- If this isn't the wonder, allow it
	if(buildingTypeID ~= GameInfoTypes["BUILDING_MYWONDER"]) then
		return true;
	end

	 -- The player has created a religion
	if (pPlayer:HasCreatedReligion()) then
		return true;
	else
		return false;
	end
end
GameEvents.PlayerCanConstruct.Add(ReligionRestriction);
 
Hmm something isnt right. I am still able to build the wonder even though I haven't even founded a religion yet. I dont even have a pantheon and I can still build it.
 
you have an undefined variable. the names of the variables must match throughout the entirety of your function, otherwise the previously undefined variable will be evaluated as having a 'nil' value which will give the same as 'false' when doing a conditional 'if' comparison. So if the nonexistent previously-undefined player is evaluated as 'nil' all the API methods that are associated with such a nonexistent previously-undefined player will also be evaluated as 'nil' or 'false'. That is, if the game does not error-out on the code in the 1st place and refuse to load it because you are trying to index (look at subsidiary-data) for a non-existent pointer/table-name. The line player = Players[playerID]; is creating a local variable that is really just a pointer for the code to find the data asssociated with an individual player in the game. You can't look through the 'index' of all these different sorts of data unless you have a valid pointer pre-defined to tell the lua which player's data to access and look through.
 
Ah well I think I just fixed the problem, I just changed " if (pPlayer:HasCreatedReligion()) then " to " if (player:HasCreatedReligion()) then " and it now seems to be working :)
 
Ah well I think I just fixed the problem, I just changed " if (pPlayer:HasCreatedReligion()) then " to " if (player:HasCreatedReligion()) then " and it now seems to be working :)

Which is exactly the problem LeeS was trying to point you toward, haha.
You tried to use the variable pPlayer when the only thing you defined was player.
 
Back
Top Bottom