Caffeine

Events

Reference Game Last updated: Lua 3 · Dryad 0

A simple named-event bus: subscribe, emit and unsubscribe.

All namespaces

Namespace: caffeine.events

on#

Lua: caffeine.events.on

text
on(eventName, callback) -> integer

Subscribes a callback to a named event.

ParametersTypeDescription
eventNamestringEvent name.
callbackfunctionCalled with the arguments given to emit.

Returns: integer. Handler handle for off.

Example

lua
local h = caffeine.events.on('hit', function(dmg) end)

emit#

Lua: caffeine.events.emit

text
emit(eventName, ...)

Calls every handler of the event in registration order. A handler error is logged and does not stop the others.

ParametersTypeDescription
eventNamestringEvent name.
...anyArguments passed to each handler.

Returns: none

Example

lua
caffeine.events.emit('hit', 10)

off#

Lua: caffeine.events.off

text
off(handle)

Removes a handler.

ParametersTypeDescription
handleintegerHandle returned by on.

Returns: none

Example

lua
caffeine.events.off(h)