Granting yields when a citizen is born in your capital (can't get it to work!)

Durkle

Chieftain
Joined
Jul 29, 2017
Messages
29
Location
Jerusalem
I'm trying to make my civs unique ability grant a small boost of Faith and Culture upon a new citizen being born in your capital. However, I can't get it to work. This is what I have right now:
Code:
-----------------------------------------------------------------------------
-- Descendants of Nanih Waiya
----------------------------------------------------------------------------
local iChoctaw = GameInfoTypes.CIVILIZATION_CHOCTAW
function NanihWaiyaCitizenBorn(iHexX, iHexY, iPopulation, iUnknown )
    local pPlot = Map.GetPlot(ToGridFromHex(iHexX, iHexY))
    local pCity = pPlot:GetPlotCity()
    local iOwner = pCity:GetOwner()
    local pCapital = iOwner:GetCapitalCity()
    local pCapitalPlot = pCapital:GetPlotCity()
    if iOwner ~= iChoctaw then return end
    if pCity ~= pCapitalPlot then return end
    local choctawFaithBoost = (iPopulation() * 5)
    local choctawCultureBoost = (iPopulation() * 5)
    player:ChangeFaith(choctawFaithBoost)
    player:ChangeJONSCulture(choctawCultureBoost)
    if (player:IsHuman() and choctawFaithBoost > 0) then
        Events.GameplayAlertMessage(Locale.ConvertTextKey("TXT_KEY_NANIH_WAIYA_BORN", choctawFaithBoost)
    end
end

I don't really understand if the function "GetCapitalCity" gives the coordinates of the capital, or the ID, or the name, or whatever else... So I think that might be the source of this not working.

EDIT: I realize now that (player:IsHuman()) should be iOwner instead. However, I still can't get it to work.
 
Last edited:
Code:
local iChoctaw = GameInfoTypes.CIVILIZATION_DURKLE_CHOCTAW -- Always give stuff a unique identifier to avoid conflicts
function NanihWaiyaCitizenBorn(iX, iY, iOldPop, iNewPop) -- Assuming you wanted to use GameEvents.SetPopulation, which you should've
    if iNewPop <= iOldPop then return end -- Only do stuff if the City's growing, not starving...
    local pPlot = Map.GetPlot(iX, iY) -- GetPlot takes the X and Y values; Hex has nothing to do with nada
    local pCity = pPlot:GetPlotCity()
    local iOwner = pCity:GetOwner()
    local pPlayer = Players[iOwner] -- iOwner is the ID of the player: you want the Player object, which you get by indexing the Players table with the ID
    if pPlayer:GetCivilizationType() ~= iChoctaw then return end
    -- if pCity ~= pCapitalPlot then return end -- Removing this as explained below, but wanna explain that this will always be true because pCity is a City object while pCapitalPlot is a Plot object: there was no need to drop down to the Plot (ofc this didn't work anyway because of the Player object stuff previously mentioned but w/e)
    if not pCity:IsCapital() then return end -- This is a City Method which achieves what you were aiming for.
    local iYieldBoost = (iNewPop * 5) -- No need for function arguments (read: brackets) since iPopulation (not iNewPop) was just a number, not a function. Also why two variables for the same value?
    pPlayer:ChangeFaith(iYieldBoost) -- This wouldn't have worked since player would have been nil, since it was never defined.
    pPlayer:ChangeJONSCulture(iYieldBoost)
    if (pPlayer:IsHuman() and iYieldBoost > 0) then -- Gud - you remembered to check the Player was human. You are now better than CL. :)
        Events.GameplayAlertMessage(Locale.ConvertTextKey("TXT_KEY_NANIH_WAIYA_BORN", iYieldBoost)) -- You were missing a bracket, plus I assume you know what this text's meant to look like: "You have received +{1_Num} [ICON_PEACE] Faith and +{1_Num} [ICON_CULTURE] Culture from a [ICON_CITIZEN] Citizen being born in your [ICON_CAPITAL] Capital!"
    end
end

GameEvents.SetPopulation.Add(NanihWaiyaCitizenBorn) -- Have to subscribe the Function to the GameEvent to have it run.
 
Code:
local iChoctaw = GameInfoTypes.CIVILIZATION_DURKLE_CHOCTAW -- Always give stuff a unique identifier to avoid conflicts
function NanihWaiyaCitizenBorn(iX, iY, iOldPop, iNewPop) -- Assuming you wanted to use GameEvents.SetPopulation, which you should've
    if iNewPop <= iOldPop then return end -- Only do stuff if the City's growing, not starving...
    local pPlot = Map.GetPlot(iX, iY) -- GetPlot takes the X and Y values; Hex has nothing to do with nada
    local pCity = pPlot:GetPlotCity()
    local iOwner = pCity:GetOwner()
    local pPlayer = Players[iOwner] -- iOwner is the ID of the player: you want the Player object, which you get by indexing the Players table with the ID
    if pPlayer:GetCivilizationType() ~= iChoctaw then return end
    -- if pCity ~= pCapitalPlot then return end -- Removing this as explained below, but wanna explain that this will always be true because pCity is a City object while pCapitalPlot is a Plot object: there was no need to drop down to the Plot (ofc this didn't work anyway because of the Player object stuff previously mentioned but w/e)
    if not pCity:IsCapital() then return end -- This is a City Method which achieves what you were aiming for.
    local iYieldBoost = (iNewPop * 5) -- No need for function arguments (read: brackets) since iPopulation (not iNewPop) was just a number, not a function. Also why two variables for the same value?
    pPlayer:ChangeFaith(iYieldBoost) -- This wouldn't have worked since player would have been nil, since it was never defined.
    pPlayer:ChangeJONSCulture(iYieldBoost)
    if (pPlayer:IsHuman() and iYieldBoost > 0) then -- Gud - you remembered to check the Player was human. You are now better than CL. :)
        Events.GameplayAlertMessage(Locale.ConvertTextKey("TXT_KEY_NANIH_WAIYA_BORN", iYieldBoost)) -- You were missing a bracket, plus I assume you know what this text's meant to look like: "You have received +{1_Num} [ICON_PEACE] Faith and +{1_Num} [ICON_CULTURE] Culture from a [ICON_CITIZEN] Citizen being born in your [ICON_CAPITAL] Capital!"
    end
end

GameEvents.SetPopulation.Add(NanihWaiyaCitizenBorn) -- Have to subscribe the Function to the GameEvent to have it run.

Wow, that's perfect. You answered every question I had and more. Thanks!
 
Top Bottom