Lua Scopes

In Lua, a variable can either be local or global. By default, all variables defined by the user are globally scoped, while function parameters and loop iterators are locally scoped.

Note: The following guidance applies to Lua development for Q-SYS and might not apply to other platforms where Lua is implemented.

TLDR

Only use the local scope for temporary variables used in functions. Assign values you need later to a global variable or table. In summary, only use local when needed; otherwise, leave the variable, function, or object global.

Simplified Description of Lua Garbage Collection

Lua has automatic memory management, meaning that you don't have to explicitly clean items out of memory when not needed anymore. These include:

Note: Read more about Garbage Collection in Q-SYS Help, here.

The Global Table

The global table, defined as _G in Lua, is the topmost variable in Lua where all other variables, functions, and objects live.

Any item defined without the local keyword prefixing it will be assumed to be global to that script.

Global Tables are not shared amongst separate scripts. All Lua scripts run in isolated environments.

When to use the local scope in scripts

It is recommended to use the local scope for variables that are used solely within that function. For example, if you have an iterator or the result of a function call that is only needed in that scope, assign the local tag to it.

For Lua modules, locally scoping variables will keep them private to the module.

When NOT to use the local scope in scripts

Anything at the top level of your script should not have the local scope assigned to it. This includes the runtime code wrapped in the if Controls then ... end within a plugin.

Timers should never be locally scoped and should be stopped – and their event handlers escaped – before setting to nil, if needed.

Examples

Known Issues