Allowing a player to build a unit based on belief

Ahhuatl

Chieftain
Joined
Oct 1, 2011
Messages
87
I'm creating a mod where a player can purchase a particular unit when they adopt a certain belief. I've got the unit purchasing part right but setting up the xml tag using lua is proving to be more difficult. The operating factors:

Unit
BeliefType = BELIEF_TEOTEQUITL
UnitType = UNIT_TICOCYAHUACATL
New Tag = RequiresTeotequitlAdopted

The lua I wrote:

Code:
GameEvents.PlayerCanTrain.Add(
function(iPlayer, unitTypeID)
	local RequiresTeotequitlAdopted = GameInfo.Beliefs[BeliefType].RequiresTeotequitlAdopted
	if RequiresTeotequitlAdopted then
		local player = Players[iPlayer]
		local unitID = UNIT_TICOCYAHUACATL
		if not RequiresTeotequitlAdopted then
		end
	end
	return true
end)

What am I missing here? I'd appreciate any help...
 
The function always returns true, regardless of anything else in the code
 
Also it doesn't check anywhere if the player has the belief, and uses a variable (BeliefType) without first assigning a value to it...

Anyone knows what function can be used to check if a belief exists in a player's religion (or in the religion present in a city)?
 
Here is my update, still not working properly. Also, I changed the name of the belief type and tag to BELIEF_CALMECAC and RequiresCalmecac



Code:
GameEvents.PlayerCanTrain.Add(
function(iPlayer, unitTypeID)
	local beliefType = GetBeliefType(belief)
	if local beliefType = BELIEF_CALMECAC then
		local RequiresCalmecac = GameInfo.Beliefs[beliefType].RequiresCalmecac
		if RequiresCalmecac then
			local player = Players[iPlayer]
			local unitID = UNIT_TICOCYAHUACATL
			if not RequiresCalmecac then
			return false
			end
		end
		return true
	if not local beliefType = BELIEF_CALMECAC
	break
	end
	end)
 
Again you reference an unitialized variable (belief), and don't check whether the player actually has the belief or not. Here is my attempt, it's not tested so probably won't work, as I'm not a LUA expert so I could make a mistake (I got the information about functions related to religion in this thread.) If it doesn't work, then perhaps someone who is better at LUA than me can fix it.

Code:
GameEvents.PlayerCanTrain.Add(
function(iPlayer, unitTypeID)
	if GameInfo.Units[unitTypeID].RequiresCalmecac then
		local Player = Players[iPlayer]
		local eReligion = Player:GetReligionCreatedByPlayer()
		if eReligion then
			local hasBelief = false
			for i,v in ipairs(Game.GetBeliefsInReligion(eReligion)) do
				if v == GameInfoTypes.BELIEF_CALMECAC then hasBelief = true end
			end
			return hasBelief
		else
			return false -- the player has no religion founded
		end
	else
		return true -- the unit doesn't require Calmecac
	end
end)

Edit: Added some comments to the code.

Also note that (assuming that it works properly, which I doubt) it checks the religion founded by the player, not the religion present in the city that is supposed to build the unit. If you want to make it dependent on the city's religion, then you should use CityCanTrain instead of PlayerCanTrain, and City:GetFavoredReligion instead of Player:GetReligionCreatedByPlayer, and make other changes to the code so it checks the city, not the player.

Edit2: Is BELIEF_CALMECAC a pantheon belief? Then the code can be simplified, using the Player:GetBeliefInPantheon function (and the current version will probably require the player to found a religion to be able to train the unit, instead of just founding a pantheon).
 
Hmm, that doesn't work either and it looks pretty solid too. Thanks for your help, hopefully someone will spot the issue.... also Calmecac is an enhancer.
 
PawelS's code should work except I think
Code:
local eReligion = Player:GetReligionCreatedByPlayer()
returns -1 rather than nil (functions that return integers usually return -1 if there "isn't any"). That could easily be checked in FireTuner. If I'm right then the following line should be
Code:
if eReligion ~= -1 then

Note that Game.GetBeliefsInReligion(eReligion) and many other religious functions give a CTD if you supply an eReligion that has not yet been founded. Probably does the same with -1.

The OP is not clear on what is wanted. Is it a belief in the religion founded by the player? (that is what PawelS is doing) Belief in city majority religion? Either of these is very doable.
 
Back
Top Bottom