Going from player ID to Trait info

S3rgeus

Emperor
Joined
Apr 10, 2011
Messages
1,270
Location
London, United Kingdom
I'm creating a UA that utilizes new columns I've added to the Traits table. I want to implement these traits using Lua (and I will be including a DLL, but I'd prefer a Lua solution to this particular issue). What I'm unclear on is how to go from a player's in-game ID to the trait info from the database. I should be able to, loosely, navigate along the tables and follow the connections between player and Civ to leader and then to trait. But I'm getting lost in the Lua API (I have been using the wiki).

I get lost fairly fast, here's an example:

Code:
function customTrait(iPlayer)
	local pPlayer = Players[iPlayer]
	local iLeader = pPlayer:GetLeaderType() -- This is the Leader ID in the Leaders table, correct?

	-- and here I'm lost. How can I get to the Trait? I want to be able to do something like:
	local traitInfo = GameInfo.Traits[*something*]
end
 
Maybe something like this?

Code:
local condition = "LeaderType = '" .. GameInfo.Leaders[iLeader].Type .. "'";
for row in GameInfo.Leader_Traits(condition) do
     -- we now are only searching for traits with LeaderType iLeader
     local traitType = row.TraitType;  -- traitType would be like "TRAIT_GATEWAY_AFRICA"

end

may not work if iLeader isn't an ID i.e. 0, 1, 2, 3, 4, 5...I'm not 100% what GetLeaderType() returns.
 
Maybe something like this?

Code:
local condition = "LeaderType = '" .. GameInfo.Leaders[iLeader].Type .. "'";
for row in GameInfo.Leader_Traits(condition) do
     -- we now are only searching for traits with LeaderType iLeader
     local traitType = row.TraitType;  -- traitType would be like "TRAIT_GATEWAY_AFRICA"

end

may not work if iLeader isn't an ID i.e. 0, 1, 2, 3, 4, 5...I'm not 100% what GetLeaderType() returns.

Awesome, this is what I wanted to do. I'm a bit worried about performance since this will be called relatively often. I hoped there would be a way to get to Trait without having to iterate over the Leader_Traits table. (I also wasn't sure on the syntax of how to do that either!) I may go for a DLL-side implementation for this then, or expose some new functions to the Lua API that lets me go straight from the player object to the Trait ID.

Thanks for the help! :)
 
Awesome, this is what I wanted to do. I'm a bit worried about performance since this will be called relatively often. I hoped there would be a way to get to Trait without having to iterate over the Leader_Traits table. (I also wasn't sure on the syntax of how to do that either!) I may go for a DLL-side implementation for this then, or expose some new functions to the Lua API that lets me go straight from the player object to the Trait ID.

Thanks for the help! :)

I don't think you will have too much of a performance issue, because there are only num_leaders rows and no logic is performed on the other leaders.
 
Back
Top Bottom