[BNW] UA - Free Settler every Era

Cou

Chieftain
Joined
Jul 21, 2016
Messages
8
Hi, I need some help on my UA which is spawning a free settler in the capital every time you advance to a new era. However my code seems to give a settler everytime a player in the game advances an era, and not just my civ. Any help? Code below...
Code:
function Cou_GetSettlerFromEra(playerID)
    local player = Players[playerID]
    if (player:IsAlive() and player:GetCivilizationType() == civilisationID) then
        for city in player:Cities() do
            if city:IsCapital() then
                local plot = city:Plot()
                local plotX = plot:GetX()
                local plotY = plot:GetY()
                player:InitUnit(GameInfoTypes["UNIT_SETTLER"], plotX, plotY)
                print("grox settler")
            end
        end
    end
end
Events.SerialEventEraChanged.Add(Cou_GetSettlerFromEra)
 
Probably related to you using a SerialEvent (which are unreliable, do not work as expected, or 'have a catch' most of the time).
Assuming that civilisationID is defined somewhere else in your code, a simple fix would be to hook the function to TeamSetEra(iTeam,iEra) instead.
As one of the parameter is a TeamID rather than a PlayerID, you need to retrieve all team members.

I wrote this piece of code a while ago for someone else, but feel free to use it. It retrieves all team members of a given team:
Spoiler :

Code:
--Gets all team members of a given team.
--INPUT: iTeam: An ID of a specific Team
--OUTPUT: a Table consisting of the ID's of all team members (alive or dead);
function GetTeamMembers(iTeam)
    print("Getting the team members of Team "..iTeam);
    local tTeamMembers = {} --A table that will contain all team members
  
    --loop through all the players (including City States and the Barbarians)
    for i = 0, GameDefines.MAX_PLAYERS-1,1 do
        local pPlayer = Players[i];
        if pPlayer ~= nil then
            --check if the current player is part of the team
            if pPlayer:GetTeam() == iTeam then
                --if yes, we add his playerID at the end of the table;
                print("Player "..i.." ("..pPlayer:GetName()..") is a member of Team "..iTeam);
                tTeamMembers[#tTeamMembers+1] = i;
            end
        end
    end
    print("All Team Members of Team "..iTeam.." have been retrieved")
    --return our table
    return tTeamMembers;
end

--===============================================================

--Here's an example of how you can use it in your code:
function MyFunction(iTeam)
    local tTeamMates = GetTeamMembers(iTeam);
  
    --iterate over the table of player IDs we just retrieved:
    --(iPairs is a special Lua iterator (not specifically for civ5), which iterates over a given table (starting at index=1) until it encounters an element that is nil. More info in the iPairs function can be found here: https://www.lua.org/pil/7.3.html)
    for _,iPlayer in ipairs(tTeamMates) do
        --do stuff here
    end
end


EDIT:
Also, this part
Code:
for city in player:Cities() do
           if city:IsCapital() then
                local plot = city:Plot()
                local plotX = plot:GetX()
                local plotY = plot:GetY()
                player:InitUnit(GameInfoTypes["UNIT_SETTLER"], plotX, plotY)
                print("grox settler")
            end
        end
can be condensed into this:
Code:
city = player:GetCapitalCity()
           if city then
                --an extra 'if' in case the player does not have any cities but is still alive (E.g. 'complete kills' is enabled)
                local plot = city:Plot()
                local plotX = plot:GetX()
                local plotY = plot:GetY()
                player:InitUnit(GameInfoTypes["UNIT_SETTLER"], plotX, plotY)
                print("grox settler")
            end
 
Alright, thank you, I will try using TeamEra instead.
And thank you so much for providing code and examples, it helps a lot.

I actually already fixed the thing with player:GetCapitalCity(), but I will add the "if city" as well, as I play with "complete kills" on :)
 
By the way, is TeamSetEra an Event or GameEvent? I can't find it on modiki.

EDIT: nvm, I found it in one of whowards spreadsheets, its a GameEvent.
 
Last edited:
Back
Top Bottom