Lua Question: Add Building to City

Joined
May 4, 2010
Messages
346
Does anyone know if Civ6 has a lua function for adding a building to a city? Civ5 had such a function, but I can't find one that works with Civ6.
 
It's a bit hidden...
Code:
pCity:GetBuildQueue():CreateIncompleteBuilding(buildingID, 100)
 
@LeeS wrote some really helpful functions that do the full operation, which I stole a while back. I no longer use them since I no longer add buildings in Lua (long story) but here you go:

Code:
--place a building
function placeBuildingInCityCenter(pCity, iBuilding)
    local iCityPlotIndex = Map.GetPlot(pCity:GetX(), pCity:GetY()):GetIndex()

    if not pCity:GetBuildings():HasBuilding(iBuilding) then
        pCity:GetBuildQueue():CreateIncompleteBuilding(iBuilding, iCityPzlotIndex, 100);
    end
end

-- Remove a building
function removeBuildingFromCityCenter(pCity, iBuilding)
    if pCity:GetBuildings():HasBuilding(iBuilding) then
        pCity:GetBuildings():RemoveBuilding(iBuilding);
    end
end
 
I've updated the function to allow an argument for whether to repair a pillaged building (primarily for dummies). The additional argument bRepairPillaged defaults to false when omitted.
Code:
function PlaceBuildingInCityCenter(pCity, iBuilding, bRepairPillaged)
	local bFixIfPillaged = ((bRepairPillaged ~= nil) and bRepairPillaged or false)
	local iCityPlotIndex = Map.GetPlot(pCity:GetX(), pCity:GetY()):GetIndex()
	if not pCity:GetBuildings():HasBuilding(iBuilding) then
		pCity:GetBuildQueue():CreateIncompleteBuilding(iBuilding, iCityPlotIndex, 100);
	else
		if (bFixIfPillaged == true) then
			if pCity:GetBuildings():IsPillaged(iBuilding) then
				pCity:GetBuildings():SetPillaged(iBuilding, false)
			end
		end
	end
end
------------------------------------------------------------------------------------
function RemoveBuildingFromCityCenter(pCity, iBuilding)
	if pCity:GetBuildings():HasBuilding(iBuilding) then
		pCity:GetBuildings():RemoveBuilding(iBuilding);
	end
end
 
Many thanks to everyone who replied! This has been extremely helpful. The Worldbuilder was not allowing me to place buildings, but with these lua functions I can now finish a proper scenario map. And, of course, it widens my horizons for potentially doing other things with my mod as well.
 
Top Bottom