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
- No prefix, just use the name
Example:
myvar = 5
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 access
Example:
self.counter = 0
Tip: Define in the Self Block of the Control Element's Setup Event.
π΅ Global Variablesβ
- Shared across the whole page
- Use just the name like locals, but they last longer
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 Elementelement[x]:
β runs on Control Elementx
- (none) β global function
Suffixβ
The name of the function, like button_value()
or midi_send()
Examples:
self:element_index()
element[3]:button_value()
module_rotation()
β 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 Lua itself. - Other exceptions are noted in the Reference Manual.