Adding buildings to cities by Lua ?

bluefoxxp

Chieftain
Joined
May 8, 2011
Messages
2
Hi,

currently I'm working on my 1st scenario project.

One of the problems that I am trying to handle is to place a specific set of buildings (e.g. barracks, a market and a granary) into every city on my map at the start of the game initially.

Well, I finished my map and placed all cities by WorldBuilder. But how do I place the buildings by lua?
I tried it with a code snippet from the 'New World' scenario, I adapted for my issues:

Code:
function InitMyCities()

	local player = Players[0];
	local capital1 = player:InitCity(2, 27);
	local city_ID = capital1:GetID();
	SetupMyCity(capital1);

end

function SetupMyCity(city)

	local iBuildingID;
	iBuildingID = GameInfoTypes["BUILDING_GRANARY"];
	city:SetNumRealBuilding(iBuildingID, 1);
end

... unfortunately, it doesn't work.
I copied the code into a new mapscript-file and added it as a mapscript in the content-section of my modbuddy-project.
But when I start the game, it seems to be not working.

I would be very grateful for any help
thanks,
bluefoxxp
 
maybe this part of your code shouldn't be in the mapscript but ingame using InGameUIAddin.
 
I followed your advice (executing my code now seems to be no problem for Civ5, thanks!) and additionally I modified my program with a method-call of "GetCityByID", which I found at Firaxis Live Tuner:

Code:
ContextPtr:SetUpdate(function()

		local player = Players[0];
		cb = player:GetCityByID(0);
		SetupMyCity(cb);
		ContextPtr:ClearUpdate(); 
end);
	
function SetupMyCity(city)

		local iBuildingID;
		iBuildingID = GameInfoTypes["BUILDING_GRANARY"];
		city:SetNumRealBuilding(iBuildingID, 1);
end

Nevertheless, it's not working. Live Tuner tells me "Attempt to call field 'GetCityByID' (a nil value)."

Does that mean, that the method GetCityByID does not exist?
Or does it mean, that I did'nt call it correctly?
 
One of the problems that I am trying to handle is to place a specific set of buildings (e.g. barracks, a market and a granary) into every city on my map at the start of the game initially.

If you want them to go into every city, then there's no need for Lua for this. In the Buildings XML table, look at the variable <FreeStartEra>. This variable says that for any game starting in an era later than X, each city begins with that building. So if the Market says
<FreeStartEra>ERA_INDUSTRIAL</FreeStartEra>
then any Industrial, Modern, or Future era starts will give that building for free.

So if you want your scenario to always have a market, then all you need to do is change the FreeStartEra to ERA_ANCIENT.

There's one small caveat: if the building has a prerequisite, that building must ALSO be set to be in that era or an earlier one. So you couldn't set the Bank to be given in an Ancient start while leaving the Market at Industrial, at least if you wanted to start pre-Industrial.
And a second sub-caveat: if any prerequisite building has a higher ID code, then it also won't work. The Colosseum, for instance, has a lower ID than the Temple. You can make the Colosseum require a Temple, and it'll work just fine in normal play. But if you set both of them to be given in an Industrial start, then it'll get to the Colosseum first, see that it doesn't have the prerequisite, and skip it.

Barring those, it'll work just fine. None of the three buildings you mention have prerequisites, and all of them already have FreeStartEra entries in the Buildings table so it should be easy to see what needs changing.

If you still want to do this in Lua, then I can get you the logic for it when I get home tonight; my Crazy Spatz mod does some Lua-based building placement of its own. (I have an "invisible" +1 Happiness building that gets added or removed in the capital as needed, because I have an Empath specialist that gives +1 happiness and this is the only way to make that work.)
 
Back
Top Bottom