LUA Event hook for on unit killed?

Angryr

Chieftain
Joined
Jul 12, 2014
Messages
40
Location
Indiana, USA
1) Is there an event for hooking onto when a unit is killed? If so, what is the function signature? I can't seem to find any examples.
2) What is the event GameEvents.OnUnitRetreated (found in AlexanderScenario.lua) is that when a great person is captured and returns to a city or is that from the one promotion for ranged units where they fall back a tile when attacked to avoid being hit?

In regards to #1, if anyone has any tips on how to find information like this out on my own it's very welcomed. I've been trying to manage with FireTuner but my knowledge of the Civ6 API and Lua is limited (I'm a C# developer).
 
I've learned through FireTuner that GameEvents.OnUnitKilled.Add is available, I just don't know what the signature of the callback is supposed to be.

EDIT: Actually that might not be true, just learned that typing GameEvents.BLARG.Add returns a function too.
 
Last edited:
Events.UnitKilledInCombat passes the following arguments in the following order to any function subscribed to it: iDefeatedPlayerID, iDefeatedUnitID, iVictoriousPlayerID, iVictoriousUnitID

iDefeatedPlayerID and iVictoriousPlayerID are player ID #s from the lua Players table for that game set-up. (The same civilizations will almost certainly have different Player ID #s in another game). Player ID #s are assigned at game set-up for all the major and minor players the game assigns to that game (or that the human user specifies are to be used in that game). The Barbarians are always assigned Player ID # 63. In a single-player game, the human player is always assigned Player ID # 0, regardless of which civilization the human is using for that game.

iDefeatedUnitID and iVictoriousUnitID will be the individual unit ID # for the units involved. If the victorious player did not kill the unit with one of its units, the iVictoriousUnitID would be assigned a value of "-1".

-----------------------------------------------

There is also a Events.UnitRemovedFromMap which from my notes passes arguments iPlayerID and iUnitID in that order to any function subscribed to it. This event literally fires for every unit removed, for whatever reason.

-----------------------------------------------

In Civ6 (and Civ5) lua you can subscribe a function to a hook event in one of two ways:

Code:
Events.UnitRemovedFromMap.Add(function(iPlayerID, iUnitID) 
   ---stuff your function does with the data passed
end)
or
Code:
function OnUnitRemoved(iPlayerID, iUnitID)
   ---stuff your function does with the data passed
end
Events.UnitRemovedFromMap.Add(OnUnitRemoved)
 
Ah thank you very much.

I notice that the VikingScenario.lua uses GameEvents.OnPillage and you are referencing Events.UnitRemovedFromMap.

What is the difference between Events and GameEvents? Just a different context variable they decided to place those particular events? I did notice there's a LuaEvents as well.
 
Last edited:
Just a different context variable they decided to place those particular events?
In civ6, pretty much yes just a different naming system which they used for some things. Most of the stuff they've added as a result of patches seems to have used GameEvents instead of Events.

LuaEvents is a different sort of beast, really. You can create your own "event" and have it available across contexts and files. LuaEvents can also be used to create direct communication between a UI-Context file and a GameplayScripts file, though since UI-Context and Gameplay-Context run on their own game engines in Civ6 there is always potential for lag between UI-Context and the actual current state of the game. GameplayScripts will always reflect the current state of the game at the instant any hook fires any code within a GameplayScripts lua file.

If I do not know what arguments a hook event provides, I use this system to print into the lua log the argument data values when the event fires, and from that I can usually determine what each argument is and what type of data is being sent:
Spoiler :
Code:
function GetArgumentDatas(sOrigin, tTable)
	print("============================================================================================")
	print("[" .. sOrigin .. "]: Dumping Event Hook Argument Data")
	print("............................................................................................")
	for k,v in pairs(tTable) do
		local sKeyType = type(k)
		local sValueType = type(v)
		print("[" .. sOrigin .. "]: Key is of type " .. sKeyType .. " = " .. tostring(k) .. ", Value is of type " .. sValueType .. " = " .. tostring(v))
	end
	print("............................................................................................")
	print("[" .. sOrigin .. "]: dump completed for this firing of the event")
	print("============================================================================================")
end
function OnUnitAddedToMap(...)
	print("Events.UnitAddedToMap fired for function OnUnitAddedToMap")
	GetArgumentDatas("OnUnitAddedToMap", {...})
end
Events.UnitAddedToMap.Add(OnUnitAddedToMap)
I can then set-up as many hook events to use the system as I want to look at, and take in-game actions that would make them fire. When they do I can look at what got spit into the lua log and compare that to what I know I just did to make the event hook fire off the routine(s).
 
Last edited:
Very interesting, I believe this should be all I need to get going. Thank you a ton, you've been extremely helpful.
 
Any way to get the X and Y coordinates of the defeated unit? I think in Civ 5 the "RemovedfromMap" event also had X and Y.. or it was a dll change from whoward..

The game itself sends a notification when an unit dies, which shows the location, so there must be a way

edit:
found out you can get the location eg from the pNotification object with pNotification:GetLocation()
 
Last edited:
Back
Top Bottom