Event Bus 🔗
SIGNAL : LISTEN : CONDITIONAL 🔗
These functions are for signalling and listening for event triggers. Basically it's a way of your code being able to tell the whole game what just happened, such as if a player did some action, or some other event happened. Note that this is more for games than for general modding, but is a helpful API regardless.
This is a global set of functions that do what they say on the tin mostly. The exception is CONDITIONAL which is a bit more involved (see below). Signals and listeners not only allow you to sometimes save on constant checks by using one-off events instead, but you can also allow extending, avoid some dependencies, and generally make things more reactive and less constant polling. Instead of checking constantly whether the player has just died and then come back to life, we use core.register_on_respawnplayer. This is no different except it is generalised and you can signal whatever thing you want.
Example Use 🔗
LISTEN("on_player_damage", function(player, damage, source)
core.chat_send_all(
"Player "..player:get_player_name()..
" was hit for "..damage.." HP"
)
end)
LISTEN("can_player_take_damage", function(player, damage, source)
if player:get_meta():get_string("invulnerable") == "true" then
return false
else
return true
end
end)
if CONDITIONAL("can_player_take_damage", player, damage, source) then
SIGNAL("on_player_damage", player, damage, source)
end
You can also get type and autocomplete hints using the below:
---@param a number
---@param b string
---@diagnostic disable-next-line: duplicate-set-field
function SIGNAL.on_something_happen:signal(a, b) end -- doesn't actually change the function, just for hints/autocomplete
SIGNAL.on_something_happen:signal(a: number, b: string) -- now gives type hints
Any more details can be found within the code itself, as it is documented in-line so it shows examples in the autocomplete.