More about: Variables and Functions
In the Grid Editor, variables and functions help you store and use data.
They always have a name, and they live in different places depending on how you create them.
π’ Types of Variablesβ
There are 3 kinds of variables in Grid:
- Local β for one Event only
- Self β for one Control Element
- Global β shared across the whole page
Each one is created differently and used with different naming styles.
π’ Local Variablesβ
- Exist only during a single Event (e.g. Setup, Timer)
- Disappear after the Event ends
- Uses the localprefix before the name when created, but can be referenced without it
Example:
local myvar = 5 --creating the local variable
myvar = myvar + 5 --referencing the local variable
Tip: Define them in the Locals Block of the Event where you use them.
π Self Variablesβ
- Stored inside the Control Element
- Stay in memory and can be reused anytime
- Use self.orelement[x].to reference or create them
Example:
self.counter = 0
Tip: Define in the Self Block of the Control Element's Setup Event.
π΅ Global Variablesβ
- Shared across the whole page
- Just create and reference them without a prefix, but using a Capitalized name for them is good practice
Example:
Gvalue = 100
Tip: Create in the Global Block of the System Setup Event to keep things organized.
βοΈ Functionsβ
Functions have two parts:
- A prefix (who runs it)
- A suffix (what it does)
Prefixesβ
- self:β runs on this Control Element
- element[x]:β runs on Control Element- x
- (none) β global function
Suffixβ
The name of the function, like button_value() or midi_send()
Examples:
self:element_index() --this is a self function, called on that element
element[3]:button_value() --this is also a self function, but referenced from a different element
module_rotation() --this is a global function
β Quick Syntax Rulesβ
| Type | Prefix | Symbol | Ending | 
|---|---|---|---|
| Variable | self. | . | No () | 
| Function | self: | : | Yes () | 
β οΈ Special Casesβ
- LED functions use no prefix. They take the LED number inside ().
- Lua functions like math.random()are from a LUA library, you can find them on the official LUA documentation page.
- Other exceptions are noted in the Reference Manual.