AttachingToEvents - mattbichay/test GitHub Wiki

Attaching To Events

Statically in RML

The easiest way to attach to events with Lua is to write your code directly into the RML files, using the on* attributes. When the event is fired three local variables are set up, document, event and element.

||[wiki:documentation/LuaManual/Elements]||The element that is currently being processed|| ||[wiki:documentation/LuaManual/Documents]||The document the element that is currently being processed belongs to|| ||[wiki:documentation/LuaManual/Events]||The event that is currently being processed||

Example:

All standard Lua semantics apply to inline event scripts. You can have multiple statements on one line separated by a space or a semicolon.

Example:

Dynamically from Lua Code

The Lua version of AddEventListener is modeled directly on Javascript. This allows you to bind any callable Lua object (free function or method) or string to an event.

Method 1:

Method 2:

The same rules for inline event scripts apply when a string is passed as the second argument.

However, if a function is passed in, then there is one caveat. If the function requires one of the local variables (event,element,document), then that variable must be in the correct position in the parameters list. The order is the order in the previous sentence: event first, element second, document third. If you only need event, then you can have the function only take one argument. If you need only document, then you must have the other two parameter slots before the document parameter. And, because it is in Lua, they may have any name.

⚠️ **GitHub.com Fallback** ⚠️