Skip to content

CustomEvent

CustomEvent enables an object to send an event.

Functions

CustomEvent CustomEvent.New()
Creates a new instance of CustomEvent.
void AddListener(string eventName, function handler)
Adds an event handler for the eventName event.
void RemoveListener(string eventName, function handler)
Removes the event handler from the eventName event.
void Emit(string eventName, ... arguments)
Emits the eventName event with the following arguments.
  • arguments The event arguments.

Code sample

The following code sample registers a handler which deregisters itself and sends an event.

local customEvt = CustomEvent.New()
-- The event arguments should match with the event sended.
local function handler(count, content)
print("received:", count, content)
customEvt:RemoveListener("count-content-event", handler)
end
customEvt:AddListener("count-content-event", handler)
-- Sends count-content-event two arguments (a number and a string).
customEvt:Emit("count-content-event", 42, "the content is number")