Question about changing building yield with LUA

Daylightend2

Chieftain
Joined
Oct 25, 2014
Messages
4
Heya !

I'm want to be able to change the production yield of a building, I've managed to change the gold maintenance of my own custom building, which is based on a factory. But I haven't figured out how to change the yield.

My code for changing the gold maintenance
Spoiler :

function something()
GameInfo.Buildings[177].GoldMaintenance = 9
end

177 is the ID that I gave my building. I've tested this ingame and the gold maintenance does change.

Since production yield isn't in the building table, but in the Building_YieldChanges table, I don't know how to change it.

Hope someone has experience with this and can help me, Thx in advance :)
 
Perhaps I'm not really understanding the question, but you just want to add production to a building...?
Code:
<Building_YieldChanges>
	<Row>
		<BuildingType>BUILDING_YOURBUILDINGNAMEHERE</BuildingType>
		<YieldType>YIELD_PRODUCTION</YieldType>
		<Yield>*integer*</Yield>
	</Row>
</Building_YieldChanges>
It wouldn't require lua.
As for the gold maintenance - I've have never seen anything like that (most people jus use the GoldMaintenance column in <Buildings>) but if it works, then whatever floats your boat, I guess.
 
Perhaps I'm not really understanding the question, but you just want to add production to a building...?
Code:
<Building_YieldChanges>
	<Row>
		<BuildingType>BUILDING_YOURBUILDINGNAMEHERE</BuildingType>
		<YieldType>YIELD_PRODUCTION</YieldType>
		<Yield>*integer*</Yield>
	</Row>
</Building_YieldChanges>
It wouldn't require lua.
As for the gold maintenance - I've have never seen anything like that (most people jus use the GoldMaintenance column in <Buildings>) but if it works, then whatever floats your boat, I guess.

You're right, for adding production to a building your example is way easier.

My intention is (my fault for not mentioning that in OP) is to constantly change the production (something like "production = 10% of player's science per turn"). I tried this with gold maintenance, for example chance granary's maintenance to 10 gold after turn 10 or so. Kinda as a proof of concept for myself.
 
You're right, for adding production to a building your example is way easier.

My intention is (my fault for not mentioning that in OP) is to constantly change the production (something like "production = 10% of player's science per turn"). I tried this with gold maintenance, for example chance granary's maintenance to 10 gold after turn 10 or so. Kinda as a proof of concept for myself.
If I understand what you are saying you want to:
  1. Change a city's yield (science, gold, production, food) by some amount (X) if a particular building has been constructed in a city ?
  2. And you want to pull the player's total science per turn (as an example) and use 10% of that as the additional yield amount (X) ?
Don't hard-code the building's ID #, as it may be different when different sets of mods are active at the same time.
Code:
local iBuilding = GameInfoTypes.BUILDING_SOMETHING_OR_OTHER  --this will give the Building's ID # after all enabled mods have been loaded into the database
local iResearchPerTurn = pPlayer:GetScience()
local iProductionChange = math.floor(iResearchPerTurn/10) --you'll need to check I haven't botched the lua syntax for the math.floor
for pCity in pPlayer:Cities() do
	if pCity:IsHasBuilding(iBuilding) then
		--commands to add a yield to the city here (i.e., #2)
		pCity:ChangeProduction(iProductionChange)
	end
end
I'm pretty sure without actually having tested it that pCity:ChangeProduction actually functions and changes the city's production by an integer amount, but it probably will not show anywhere when in the city view like you would see if it were being directly added to a building in XML hard-coding. If you want different kinds of yields you'll need different commands, and the two best places to look for them are http://modiki.civfanatics.com/index.php/City_(Civ5_Type) and http://modiki.civfanatics.com/index.php/Player_(Civ5_Type) at least to start with.

You'd generally want to drop the lines of code shown into a function that is hooked into (probably) PlayerDoTurn, so you'd want something like:
Code:
local iBuilding = GameInfoTypes.BUILDING_SOMETHING_OR_OTHER
local RequiredCivilization = GameInfoTypes.CIVILIZATION_AMERICA

function SomethingOrOther(iPlayer)
	local pPlayer = Players[iPlayer]
	if pPlayer:GetCivilizationType() ~= RequiredCivilization then return end
	if not pPlayer:IsAlive() then return end
	local iResearchPerTurn = pPlayer:GetScience()
	local iProductionChange = math.floor(iResearchPerTurn/10)
	for pCity in pPlayer:Cities() do
		if pCity:IsHasBuilding(iBuilding) then
			--commands to add a yield to the city here (i.e., #2)
			pCity:ChangeProduction(iProductionChange)
		end
	end
end
GameEvents.PlayerDoTurn.Add(SomethingOrOther)

I assumed you want the building to be a unique for a particular civilization. If this is not the case, you can get rid of the lines:
Code:
local RequiredCivilization = GameInfoTypes.CIVILIZATION_AMERICA
			&
if pPlayer:GetCivilizationType() ~= RequiredCivilization then return end
 
Back
Top Bottom