Variables

The variables module is responsible for giving read and write access to the variables of the project.

Usage

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

local Variables = require 'engine/variables'

Reference

variables.get(setName, varName)

Get a variable’s value.

Parameters:
  • setName (str) – name of the variable set to look up

  • varName (str) – name of the variable inside the variable set

Returns:

value of the variable

Return type:

any

variables.set(setName, varName, value)

Set a variable’s value

Parameters:
  • setName (str) – name of the variable set to look up

  • varName (str) – name of the variable inside the variable set

  • value (any) – value of the variable to set

variables.reset(setName, varName)

Reset a variable’s value to its initial value

Parameters:
  • setName (str) – name of the variable set to look up

  • varName (str) – name of the variable inside the variable set

Returns:

new value of the variable

Return type:

any

variables.add(setName, varName, change)

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

Parameters:
  • setName (str) – name of the variable set to look up

  • varName (str) – name of the variable inside the variable set

  • change (number) – what to add to the value

Returns:

value of the variable

Return type:

any

Examples

Set, add and retrieve variables and render them
local Variables = require 'engine/variables'
local Canvas = require 'engine/canvas'

function init()
  Variables.set("Test", "Set", 1)
  Variables.add("Test", "Added", 4)
end

function render()
  local set = Variables.get("Test", "Set")
  local initial = Variables.get("Test", "Initial")
  local added = Variables.add("Test", "Added", 1)

  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