Issue with Looping through other Players

JFD

Kathigitarkh
Joined
Oct 19, 2010
Messages
9,132
Location
The Kingdom of New Zealand
At the moment I'm having some trouble retrieving the name of other civs. Whenever I meet another civ, it returns the active player's name - as in if I am playing as Harun al-Rashid and I meet Catherine, her name is given as Harun al-Rashid of Arabia.

Below is the code that I have. All of the functions within are working as intended, it's just that they're returning the value of the active player. I can't see what's wrong, but that doesn't mean it's not something simple.

Code:
function GetDiplomaticTitle()
	for iPlayer=0, GameDefines.MAX_MAJOR_CIVS-1, 1 do
		local iPlayer = Players[iPlayer]
		if iPlayer:IsEverAlive() then
			print(iPlayer:GetName())
			local leaderName = iPlayer:GetName()
			if not HasFormalGovernment() then
			print("player has no formal government")
				local ethnicGroup = GetEthnicGroup()
				if HasEpithet() then
				print("player has an epithet")
					local epithet = GetEpithetTitle()
					return Locale.ConvertTextKey("TXT_KEY_LEADER_EPITHET_OF_PEOPLE", leaderName, epithet, ethnicGroup)
				else
				print("player does not have an epithet")
					return Locale.ConvertTextKey("TXT_KEY_LEADER_OF_PEOPLE", leaderName, ethnicGroup)
				end
			end
		end
	end
end
 
If you want the name of one specific civ (player) why are you looping all players?

Because of the loop, you start at iPlayer=0, who will always be a human player, will always be alive (or you've lost) and so your text retrieval is working for that player, find a text and returning it.

If you want the text for a specific player, pass their ID as a parameter (suggest iPlayer as then you can keep the rest of the code) and ditch the loop. Obviously you will then need to change the call to GetDiplomaticTitle(iPlayerJustMet)
 
Just re-reading the code, all your other functions will need to take parameters, ie, GetEthnicGroup(), HasFormalGovernment(), HasEpithet(), GetEpithetTitle()

There is also a cause for confusion (and hence future bugs) in the line local iPlayer = Players[iPlayer] as it redefines (and hides) the iPlayer used in the loop. Also, the iPlayer in that line is not an int but an object, so by convention should be pPlayer, which means you'll also need to change iPlayer:GetName() and iPlayer:IsEverAlive() to both be pPlayer
 
Well, that wasn't a good idea. I managed to get it working, but don't really understand how, and it is only in a limited scope - as in it works specifically for get the name of the relevant civ, but nothing else. Let me ask simply, as an example: how can I pass a civ ID as a parameter in the below function, so that that parameter isn't actually nil? Do I need to tie it to a GameEvent, though I don't want to call the function until it is needed?

Code:
function HasFormalGovernment(playerID)
        local player = Players[playerID]
	if player:HasPolicy(GameInfoTypes["POLICY_TRADITION_OPENER"]) then
		return true
	else
		return
	end
end
 
Code:
function GetDiplomaticTitle()
	for iPlayer[COLOR="Red"]ID[/COLOR]=0, GameDefines.MAX_MAJOR_CIVS-1, 1 do
		local iPlayer = Players[iPlayer[COLOR="Red"]ID[/COLOR]];
		if iPlayer:IsEverAlive() then
			print(iPlayer:GetName())
			local leaderName = iPlayer:GetName()
			if not HasFormalGovernment([COLOR="Red"]iPlayerID[/COLOR]) then
			print("player has no formal government")
				local ethnicGroup = GetEthnicGroup()
				if HasEpithet() then
				print("player has an epithet")
					local epithet = GetEpithetTitle()
					return Locale.ConvertTextKey("TXT_KEY_LEADER_EPITHET_OF_PEOPLE", leaderName, epithet, ethnicGroup)
				else
				print("player does not have an epithet")
					return Locale.ConvertTextKey("TXT_KEY_LEADER_OF_PEOPLE", leaderName, ethnicGroup)
				end
			end
		end
	end
end

Basically, when you create function:

Code:
function Example(x1, x2, x3)

end

You define x1, x2, x3 when you run it:

Code:
Horse = 33;
Example(10, Horse, 32144)
Then x1 = 10, x2 = 33, x3 = 32144.
 
Thank you, LastSword. I'm making progress. I've managed to pass the playerID from the GetDiplomaticTitle() function to the GetEthnicGroup() and HasFormalGovernment() functions. I appreciate your help.
 
Back
Top Bottom