Skip to content

Variables

The variables module gives read and write access to project-global variable sets. Prefer the property-style API (Variables.SetName.varName). For variables attached to a scene object, use node.variables instead — see Variable Sets and SceneObject.

Usage

To use this module, add the following require at the top of your script:

local Variables = require 'engine/variables'

Reference

getSet

getSet(setName)

Get a variable set by name

Parameters

  • setName (string): name of the set

Returns

  • VariableSet: variable set. This function will not check for existence of the named set, it will return a new object in any case

get

static get(setName, varName)

Get a variable's value.

Parameters

  • setName (string): name of the variable set to look up
  • varName (string): name of the variable inside the variable set

Returns

  • any: of the variable

set

static set(setName, varName, value)

Set a variable's value Note: This function does not create or initialize the variable set. The set must already be defined in the project (see the Variables documentation) in order to be accesible.

Parameters

  • setName (string): name of the variable set to look up
  • varName (string): name of the variable inside the variable set
  • value (any): value of the variable to set

reset

static reset(setName, varName)

Reset a variable's value to its initial value

Parameters

  • setName (string): name of the variable set to look up
  • varName (string): name of the variable inside the variable set

Returns

  • any: value of the variable

add

static add(setName, varName, change)

Adds a number to a variables value. Only works for numeric types, others will have no effect

Parameters

  • setName (string): name of the variable set to look up
  • varName (string): name of the variable inside the variable set
  • change (number): what to add to the value

Returns

  • any: of the variable

Examples

local Variables = require 'engine/variables'
local Canvas = require 'engine/canvas'

function init()
  Variables.Test.Set = 1
  Variables.Test.Added = Variables.Test.Added + 4
end

function render()
  local set = Variables.Test.Set
  local initial = Variables.Test.Initial
  local added = Variables.Test.Added + 1
  Variables.Test.Added = added

  Canvas.clear()
  Canvas.setFillColor(255, 255, 255, 1)
  Canvas.setFont("20px Arial")
  Canvas.fillText("Initial: " .. tostring(initial), 20, 40)
  Canvas.fillText("Set: " .. tostring(set), 20, 60)
  Canvas.fillText("Added: " .. tostring(added), 20, 80)
end