Lua Events

lemmy101

Emperor
Joined
Apr 10, 2006
Messages
1,064
Sorry if this has been figured out already, but just thought this page may interest some people here. I know Afforess for one was lamenting the lack of gameplay callback hooks to game events:

http://wiki.2kgames.com/civ5/index.php/Lua_Game_Events

ActivePlayerTurnEnd
ActivePlayerTurnStart

etc...

Looks like this is possible, somehow.
 
Already seen them. ActivePlayerTurnEnd and Start are the only two that are marginally useful, but are less useful than there cousin events from Civ4. Most of the rest are useless.

All of the events seem to be focused on updating the screen and graphics, not for actual modding. Take SerialEventCityPopulationChanged for instance. It only works if the HUMAN has discovered the city that has the population changed. Useless for modding. As are many of the rest.

Never thought I would say this, since I was the one who single handedly removed all python callbacks from the game in RevDCM based mods, but I miss them. Infinitely more useful.
 
The 2k List was actually created from the list in the forum thread.
 
Already seen them. ActivePlayerTurnEnd and Start are the only two that are marginally useful, but are less useful than there cousin events from Civ4. Most of the rest are useless.

All of the events seem to be focused on updating the screen and graphics, not for actual modding. Take SerialEventCityPopulationChanged for instance. It only works if the HUMAN has discovered the city that has the population changed. Useless for modding. As are many of the rest.

hi

i was mucking around the events and stuff, and i remembered this post, mainly because it's supposed to state a limitation, but you can try this:

Code:
function popchange()
	print("population changed somewhere....");
end
Events.SerialEventCityPopulationChanged.Add( popchange );

and you can clearly see that any population change, even if the human has not discovered the city, the population change is apparently caught by SerialEventCityPopulationChanged

when observing on the first turn, it prints out the notification when cities are founded, and you can then even go ahead and use something like this

Code:
local player = Players[ num ];

for city in player:Cities() do

	print( "\t\t" .. city:GetName() .. "\t" .. city:GetPopulation() );
	city:ChangePopulation(1, true);
	print( "\t\t" .. city:GetName() .. "\t" .. city:GetPopulation() );
end

and it will register everytime there's a population change.

but tbh i haven't played the 4-5 turns required for natural city growth so maybe it doesnt work that way?
 
okay so to go off on my last post, since i really got into that one event ;) here's some stuff others might want to try

Code:
function popchange(a, b, c, d, e)

	print(a);
	print(b);
	print(c);
	print(d);
	print(e);
	print("population changed somewhere....");
end
Events.SerialEventCityPopulationChanged.Add( popchange );

here it's going to grab whatever default arguments gets passed to 'SerialEventCityPopulationChanged'. running the game on the first turn, it's just a few seconds and it shows that a-b-c-d are defined, while e is nil. so this event gets 4 variables passed to it.

luckily this event has some examples in the vanilla files:

Code:
function SetCitySize()
	local iCitySize = Game.GetVariableCitySizeFromPopulation( g_iPopulation );
	Events.SerialEventCityPopulationChanged( g_iHexX, g_iHexY, g_iPopulation, iCitySize );
end
Controls.CitySize_ButtonApply:RegisterCallback( Mouse.eLClick, SetCitySize );

not hard to guess what they mean, an x and y position, population and city size. problem here is that the x-y doesnt exactly correspond to what's shown on the game map.

this is where the 'ToGridFromHex' function is used. it seems like its an often used function, so again, i'm guessing there's a lot of events that pass grid coords that need to be converted, or vice versa with hex cords (there is a ToHexFromGrid).

then once converted there's other functions to pull plot info to get some more data (Map section) and there's plenty of examples to get yourself started in the vanilla files

in the end, the new code is now looking like this

Code:
function popchange(a, b, c, d)

	--Events.SerialEventCityPopulationChanged( g_iHexX, g_iHexY, g_iPopulation, iCitySize );
	--the example code used for reference

	print(a);
	print(b);
	print(c);
	print(d);

	local hx, hy  = ToGridFromHex( a, b );

	print("hex x: " .. hx .. " hex y: " .. hy);
	-- now the coords should match the new city coords pop change from 0 to 1 
	-- and any other population change

	local plot = Map.GetPlot(hx, hy);
	local city = plot:GetPlotCity();

	print( "Pop change:\t" .. city:GetName() .. "\t" .. city:GetPopulation() );


	--print("population changed somewhere....");
	--commented this noob scripting line as there is plenty of better informed print statements above :)
end
Events.SerialEventCityPopulationChanged.Add( popchange );

so theres an event, a method at how to figure out what default values (i'm sure theres a proper term in lua lingo for those but i don't know lua at all so default arguments is what i put) are being passed to the function, and then a simple way to get extra relevant information on the event, and most of it is just copy / paste

whew that was fun :)
 
Back
Top Bottom