Run a lua script when a specific wonder is built

fuzzymonkey

Chieftain
Joined
Jul 3, 2011
Messages
22
Im making a wonder mod and I want to run a script to increase the population of the city when the wonder is built, but I'm not really sure how to do that. I've been looking at the Lua Game events wiki and I think I might need to use the WonderStateChanged event, but again Im not sure how. I havent been able to find any good guides on even handling either :/
 
If all you want the wonder to do is increase the population of the city, that should be possible with just xml. I believe one of the late game wonders, CN tower, adds population.

It is a bit hacky but I believe you can detect when a building is finished by:
1. Create a new unit class
2. Have the building provide one free unit of this class
3. Whenever a new unit is created (requires a bit of lua) test to see if it is that unit class
4. If it is that unit class, delete it and run whatever lua you need to run (use the unit's location to determine the city that built the unit)
 
Then CN tower does a global population increase, i want just a single city populatin increase.

And I think this should be easily doable with lua, I just dont know any guides on lua event handling
 
Code:
fmBoolean = true;

for i, player in pairs(Players) do
	if player:GetBuildingClassCount(GameInfoTypes.YOURBUILDINGCLASS) then
		fmBoolean = false;
	end
end

GameEvents.PlayerDoTurn.Add(function(iPlayer)
	if fmBoolean then
		if Players[iPlayer]:GetBuildingClassCount(GameInfoTypes.YOURBUILDINGCLASS) then
			fmBoolean = false;
			for iCity in Players[iPlayer]:Cities() do
				if iCity:GetNumBuilding(GameInfoTypes.YOURBUILDINGTYPE) then
					iCity:ChangePopulation(1);
				end
			end
		end
	end
end)
 
Thanks for the code, but I still dont know how to 'use' it. That is, how do I get it to run when the wonder is built?
 
Thanks for the code, but I still dont know how to 'use' it. That is, how do I get it to run when the wonder is built?

The code is already set up to trigger at the most useful time to achieve the effect you want. Just include it in your mod (in a lua script file), leave VFS=false for the file, and add an InGameUIAddin for it. If you don't know how to do that, see the modder's guide.
 
I didnt realize this would trigger when needed I guess my question is, when does the top block of code (the for loop) get called then? I guess the second block of code (PlayerDoTurn) is called every turn, so you are just adding a closure to that list, but the first block of code obviously has to be run before hand and Im just not sure when its run.
 
Back
Top Bottom