61 posts / categories / feed

Basic Lua Programming with Variables

Getting and setting variables in Lua

Note that “;” is not required in Lua, hence regardless of adding it or not, does not really matter much.

Creating a Global Variable

-- Global Variable
foo = "foo";


Creating a Local Variable

-- Local Variable, only accessible within the stored lua context
local bar = "bar";



Concatenation in Lua is done with the use of “..”, while some other languages such as PHP is using “.” and Java using “+”.

-- Concatenation of variable foo and bar
local fooBar = foo .. bar;


Arithmetic addition with variables with “+”

-- Sum up variables
local one = 1;
local two = 2;
local sum = one + two;

-- Concatenation of number with string
local numString = foo .. sum;


Garbage Collection

It is a good practice to free the variable for garbage collection when not requiring it anymore, by simply assigning nil to the variable.

-- Release the variable for garbage collection
foo = nil;