Skip to content
Benedict Allen edited this page Nov 15, 2015 · 3 revisions

Callbacks

A callback is a function called when some event happens. An example might be an onClick callback of a clickable object. By using callbacks, there is no need to constantly poll for events, as code is automatically executed after the initial setup.

There are a couple of types of callbacks, and the way they are defined and used is different accordingly.

Instance callbacks

These are the most commonly used callbacks. They are specific to an instance, and are called when something happens to the instance. You define an instance callback like this:

function instance:callback( ... ) -- note the colon notation. `self` will be passed into instance callbacks
    ...
end

Generic callbacks

These are used for libraries and tasks that aren't specific to an instance. Simply put, they're plain old functions, and have to be registered by the thing that uses them. An example would be this:

local function callback( ... )
    ...
end

something.register( callback )

An actual example of this is with Application:registerResourceLoader:

local function loader( content )
    -- do stuff
end

application:registerResourceLoader( "type.x", loader )

Clone this wiki locally