Variables & Assignment
tip
Variables and Assignments are very generic terms in programming, if they are not familiar I recommend checking some resources.
Don't overcomplicate things though, variables are names, assignment is the act of point at things with those names.
In Lua we have two types of Variables, Global and Local
Global
Global Variables are declared with a simple assignment statement, no keywords needed.
It is also possible to just reference them without declaration, but you get a nil
return value
You can also assign nil
to a variable which has the effect of deleting the value via de-reference.
De-referenced values are cleaned up by the automatic memory management provided by Lua.
To give an example of this in action lets create two files.
First we create a file with 1 Global variable PI
with the value of 3.14159
assigned to it
If we try to reference it we cannot, we will get nil
as a result, however once we reference it
after
the constants.lua
is imported it works as expected.
Local
Local variables are declared using the local
key word
Local variables are slightly different to global in that they are only accessible from within the scope.
We will bring our two files back but let's add local before the variable PI
and a print
statement after its declaration.
When we now try and access the variable PI
after the import we still get a nil
value
This is because local variable is not accessible outside the file.
If we run this program we get the following output in the console, the first print comes from the
printPi.lua
at line 3, the second comes from constants.lua
at line 2, the third comes from
printPi.lua
at line 9
A Note on Scope
Scope is quite a complex topic, but it's critical to understand at least the fundamental concept before declaring a single variable.
At its core something is scoped when it exists only for a specific block of code.
The first scope you encounter is the file itself. Then any functions/control structures (for loops etc)
Consider this example
The variable localPi
is limited to the scope of the function definePi
, when we leave the function
the variable no longer exists and is not accessible, it is said to be scoped, the Variable pi
however is NOT and we can access it after calling the function.
warning
It may not be obvious but there is a glaring issue here, often languages when defining a variable
like we do with pi = 3.14
limit this to only be accessible inside the function, meaning in
large scripts you don't get random collisions, that's not the case here, if for example we had
defined pi as 3.14159
at the start, then it was replaced inside a function by accident to 3.14
we have just lost a lot of precision!
LESSON: USE local
VARIABLES UNLESS YOU EXPLICITLY WANT global
ONES