Skip to content

EventHelper

EventHelper is a global object that registers event handlers in events.

Functions

void EventHelper.AddListener(any obj, string eventName, function handler)
Adds a handler to the obj object for the eventName event.
Note: only API components and objects with the AddListener function can be called.
void EventHelper.RemoveListener(any obj, string eventName, function handler)
Removes the handler from the obj object for the eventName event , which inverses the call of AddListener.
Note: only API components and objects with the RemoveListener function can be called.

Code sample

The following code sample registers an event handler which deregisters itself in the StartMoving event of YaMoverComponent.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
local AddListener = EventHelper.AddListener
local RemoveListener = EventHelper.RemoveListener

local mover = script:GetYaComponent("YaMoverComponent")

-- StartMoving has no arguments.
local function handler()  
    print('StartMoving received')
    RemoveListener(mover, "StartMoving", handler)
end

AddListener(mover, "StartMoving", handler)