util.event.on

Shared

Registers a handler function for the specified event


Syntax

local status = util.event.on(
    name,
    exec,
    config = nil
)

The thread instance is only available inside async handlers.

  • When async = true — handler runs inside a dedicated thread; thread can be accessed via util.thread.current().
  • When async = false — handler runs inline on the calling thread.

Parameters

TypeNameDescription
stringnameName of the event to subscribe to
functionexecHandler function to invoke when the event fires
tableconfigConfiguration table
Refer Config section

Returns

TypeNameDescription
boolstatustrue on successful registration, false otherwise

Config

TypeKeyDefaultDescription
boolasyncfalse• When true - runs the handler inside a dedicated thread
• When false - runs the handler inline on the calling thread
intsubscription_limitnilMaximum number of times this handler will fire before being automatically removed
• Must be >= 1 if set
• Omit for unlimited invocations
intpriority0Determines fire order relative to other handlers on the same event
• Higher values fire earlier, similar to a z-index
• Handlers with equal priority fire in registration order
• May be negative

Callback: exec

exec(...)
TypeNameDescription
any...Arguments forwarded from the emitting call

Examples

Register a basic event handler
util.event.on("on_player_join", function(player)
    core.engine.print("info", "Player joined:", player)
end)
Register a handler with a subscription limit
util.event.on("on_pickup_collected", function(pickup)
    core.engine.print("info", "Collected:", pickup)
end, { subscription_limit = 1 })
Register an async handler that sleeps
util.event.on("on_round_start", function()
    local thread = util.thread.current()
    core.engine.print("info", "Round started, waiting 5 seconds...")
    thread:sleep(5000)
    core.engine.print("info", "5 seconds elapsed")
end, { async = true })
Register handlers with explicit fire order using priority
util.event.on("sandbox:draw", function()
    core.engine.print("info", "Fires first - highest priority")
end, { priority = 10 })

util.event.on("sandbox:draw", function()
    core.engine.print("info", "Fires second - default priority")
end)

util.event.on("sandbox:draw", function()
    core.engine.print("info", "Fires last - lowest priority")
end, { priority = -10 })

On this page