Say you want to print a message at three different places:
print("Hello world!")
print("Hello world!")
print("Hello world!")
This works fine but what happens if later on you decide you want to change the message?
You would have to go in and change all three messages.
In this simple example it may seem trivial, but with a much bigger script, searching for and changing every occurrence of some value is tedious and error prone.
We need some sort of symbolic representation of some value and where we can modify it once and have it update automatically throughout a script.
We need a ‘variable.’
As the name implies, a variable is some thing that can vary or change. More accurately a variable is a named storage location for some value or data.
Variables are useful because it allows us to name some container to store data that can then be referenced from any number of places and we only have to keep track of that one container.
To create a variable, “declare” it creating a name.
The name is of your choosing and can consist of letters, numbers, or the underscore character (_) as long as it does not begin with a number and does not conflict with other keywords or reserved names.
Here are a few ways to declare variables:
local var1
local var2 = 3 + 1
local var3, var4, var5
local var6, var7, var8, var9 = "Hello world!", 3.14, 42
print(var6, var7, var8, var9) --Output: Hello world! 3.14 42 nil
‘local‘ is one of Lua’s keywords. It tells Lua to make this a “local” variable. var1, var2, var3, etc. are the variable names.
The single equal sign ‘=’ is called the ‘assignment operator.’
var1 is declared but not assigned a value so Lua automatically assigns it nil. var2 is assigned the sum of 3+1 when it is declared.
The third line declares three local variables, all initialized to nil.
var6, var7, and var8 are initialized to “Hello world!”, 3.14, and 42 respectively in the fourth line. And var9 again to nil
In a script, you might have dozens or more of variables. This eventually gets difficult to keep track of.
Because there is little restriction on variable names and names do not affect the memory or performance of a system, you should seek to use names that clearly indicate what the variable represents.
a, b, c do little to document what each variable is represents.
health, time_left, runSpeed make it immediately obvious what each variable represents.
Variables can be reassigned and unassigned. Since objects and functions and other variables are just memory addresses, variables can be assigned to those as well.
local var1 = "Hello world!"
local var2 = nil
print(var1) --Output: Hello world!
var2 = var1 --var2 == "Hello world!"
var2 = var2 .. var2
local var3 = print
var3(var2) --Output: Hello world!Hello world!
var1 = nil
var2 = nil
var3 = nil
Initially, var1 is assigned to a string and var2 assigned to nil.
In line 5, var2 is assigned var1. And then we append var2 to var2 and assign it to var2 because that somehow makes sense to a computer.
Then we create a new variable var3 and assign it the print function. Because print did not include the parenthesis, Lua will reference the function rather than execute it as a function.
In line 11, we execute var3 as a function and give it var2 as the argument just like we would for the print function.
All three variables are then unassigned in the last three lines