LF short LUA script that adds building to city when it's captured

M0RF34S

Chieftain
Joined
Nov 17, 2024
Messages
9
Hi~

I'm looking for a short LUA script that adds a dummy building to a city when it is captured. More specifically, when Austria uses its UA (Diplomatic Marriage) to Annex/Puppet a City-State, I want the building to be immediately constructed in the City-State.

Pseudocode:
- It's gonna hook on CityCaptureComplete
- If "isConquest", then end (since Diplomatic Marriage isn't flagged as Conquest)
- If the new owner is not Austria, then end
- (Probably redundant) If the city wasn't a City-State, then end
- (Probably redundant) If the coordinates don't contain a city, then end
- If we passed the above checks, then, if city:GetNumRealBuilding(BUILDING_DIPLO_MARRIAGE) < 1 then
city:SetNumRealBuilding(BUILDING_DIPLO_MARRIAGE, 1)

The problem is I don't know how to write LUA. Shouldn't be more than 10 lines of code though. I'll write the building code myself in XML.

Feel free to address any problems you see, or add additional checks as you see fit.

Thanks, and happy new year!
 
Code:
local iAustria = GameInfoTypes['CIVILIZATION_AUSTRIA']
local iDummy = GameInfoTypes['BUILDING_DIPLO_MARRIAGE']

function AddDiploMarriageDummy(oldOwnerID, bCapital, iX, iY, cityID, iOldPop, bConquest)
    local pPlot = Map.GetPlot(iX, iY)
    local pCity = pPlot:GetCity()
    local pNewOwner = pCity:GetOwner()

    if not bConquest and pNewOwner:GetID() == iAustria then
        if pCity:GetNumRealBuilding() < 1 then
            pCity:SetNumRealBuilding(iDummy, 1)
        end
    end
end

GameEvents.CityCaptureComplete.Add(AddDiploMarriageDummy)

not sure we need to check if city already has dummy since the method sets the number to 1, it doesn't add it
 
Code:
local iAustria = GameInfoTypes['CIVILIZATION_AUSTRIA']
local iDummy = GameInfoTypes['BUILDING_DIPLO_MARRIAGE']

function AddDiploMarriageDummy(oldOwnerID, bCapital, iX, iY, cityID, iOldPop, bConquest)
    local pPlot = Map.GetPlot(iX, iY)
    local pCity = pPlot:GetCity()
    local pNewOwner = pCity:GetOwner()

    if not bConquest and pNewOwner:GetID() == iAustria then
        if pCity:GetNumRealBuilding() < 1 then
            pCity:SetNumRealBuilding(iDummy, 1)
        end
    end
end

GameEvents.CityCaptureComplete.Add(AddDiploMarriageDummy)

not sure we need to check if city already has dummy since the method sets the number to 1, it doesn't add it
Hello and thanks for the response - this doesn't seem to work unfortunately.
I started by changing GetNumRealBuilding() to GetNumRealBuilding(iDummy), but that didn't fix it.

Then, just in case there was a problem with player ID vs player object, I stripped the code down to:

function AddDiploMarriageDummy(oldOwnerID, bCapital, iX, iY, cityID, iOldPop, bConquest)
local iDummy = GameInfoTypes['BUILDING_DIPLO_MARRIAGE']
local pPlot = Map.GetPlot(iX, iY)
local pCity = pPlot:GetCity()

if not bConquest then
if pCity:GetNumRealBuilding(iDummy) < 1 then
pCity:SetNumRealBuilding(iDummy, 1)
end
end
end
GameEvents.CityCaptureComplete.Add(AddDiploMarriageDummy)

Yet, to my surprise, that didn't work either.
Any ideas on what the issue could be?
 
Last edited:
Just to be clear, you've added the LUA file in Content, yes? That was a mistake I used to make all the time
 
Yes, I made three big bloomers there, sorry

Code:
local iAustria = GameInfoTypes['CIVILIZATION_AUSTRIA']
local iDummy = GameInfoTypes['BUILDING_DIPLO_MARRIAGE']

function AddDiploMarriageDummy(oldOwnerID, bCapital, iX, iY, cityID, iOldPop, bConquest)
    local pPlot = Map.GetPlot(iX, iY)
    local pCity = pPlot:GetCity()
    local newOwnerID = pCity:GetOwner()
    local pNewOwner = Players[newOwnerID]

    if not bConquest and pNewOwner:GetCivilizationType() == iAustria then
        if pCity:GetNumRealBuilding(iDummy) < 1 then
            pCity:SetNumRealBuilding(iDummy, 1)
        end
    end
end

GameEvents.CityCaptureComplete.Add(AddDiploMarriageDummy)
 
Back
Top Bottom