Get all traits (Leader + Civ) of a Player

Luke Vo

Chieftain
Joined
Apr 29, 2019
Messages
59
This code gets all the traits of a player ID assigned by their civilization and leader. It is recommended to cache the result as it is (maybe?) the same for the whole game:

Code:
function getPlayerTraits(playerId: number)
    local result = {};
    local playerConfig = PlayerConfigurations[playerId];
    
    -- Civ Trait
    local playerCiv = playerConfig:GetCivilizationTypeName();
    for row in GameInfo.CivilizationTraits() do
        if (row.CivilizationType == playerCiv) then
            result[row.TraitType] = true;
        end
    end
    
    -- Leader Trait
    local playerLeader = playerConfig:GetLeaderTypeName();
    for row in GameInfo.LeaderTraits() do
        if (row.LeaderType == playerLeader) then
            result[row.TraitType] = true;
        end
    end
    
    return result;
end

Example usage:

In my mod where I need to check if a player has TRAIT_CIVILIZATION_SATRAPIES (currently gained by being Persian):

Code:
-- Declare a scoped cache
playerTraitsCache = {};

-- When needed:
    -- See if player has TRAIT_CIVILIZATION_SATRAPIES trait
    local playerId = player:GetID();
    if (not playerTraitsCache[playerId]) then
        playerTraitsCache[playerId] = getPlayerTraits(playerId);
    end
    
    if (playerTraitsCache[playerId]["TRAIT_CIVILIZATION_SATRAPIES"]) then
        -- See if there is a road with higher placement value
        for routeType in GameInfo.Routes() do
            if (routeType.PlacementValue == route.PlacementValue + 1) then
                route = routeType;
                break;
            end
        end
    end
 
Top Bottom