Skip to main content

Variables

A variable is a named “container” that is used to hold data. Variables can hold any type of data including numbers, strings, booleans, tables, functions, and other things. The name of a variable can be any non-reserved string containing letters, digits, and underscores - however, variable names cannot start with a digit.

-- These are all valid variable declarations
MY_VARIABLE
my_variable
myVariable
_myVariable123

-- These are invalid variables
and -- Invalid, because "and" is a reserved keyword
123var -- Invalid, because variable names cannot start with a digit

Reserved strings in Lua are:

and
break
do
else
elseif
end
false
for
function
if
in
local
nil
not
or
repeat
return
then
true
until
while
caution

Like most programming languages, Lua is case sensitive. This means that the correct case must be used for a variable, function, or other component to be recognized by a program. variable, Variable, and VARIABLE are not the same and will be recognized and treated differently by a program.

Declaring Variables

In order for a variable to be used it must first be declared, and can be assigned an initial value. Assigning an initial value when declaring a variable is often called “initializing” the variable. To assign a value to a variable use the = operator.

local count = 0

The above variable count has been declared in the local scope, and is assigned the initial value 0.

All variables are either global or local in scope. Unless a variable is declared as local, it will default to global scope.

count = 0

The above variable count has been declared in the global scope and assigned the initial value 0.

A global variable is visible to all scopes within a script. A local variable is visible only to the block in which is declared. You can read more about scope here.

Assigning Variables

The contents of a variable can be changed at any time within a program by reassigning a new value to it.

local count = 0

function onStart()
count = count + 1
end

In the example above, count is declared as a local variable and initially assigned the value 0. When the program starts, it is then reassigned a value equal to its initial value (0) plus 1. So the new value of count becomes 1.

Naming Convention

In SCS, the PascalCase naming convention is used for modules. For all other components (variables, functions, etc.) the camelCase naming convention is used.

With PascalCase naming, the first letter must be uppercase in addition to any appended word. For example: PostProcess (module name using PascalCase naming.)

With camelCase naming, the first letter is lowercase while the first letter of any appended word is uppercase. For example: setColorAdjustments (function name using camelCase naming.)

When combining modules and functions in an expression, these conventions are maintained. For example: PostProcess.setColorAdjustments(), which accesses the setColorAdjustments() function that is part of the PostProcess module.