The word "variable" means "able to vary" or "changable". In programming, a variable is a name given to data that can change while your game is running. It's like a container that holds information, and the name of the container is used to refer to the data inside it.

For example, let's say you want to keep track of the score in a game. You could create a variable called "score" and set it with an initial value of 0. Use a single equal sign (=) to set values to variable names. 

score = 0

You can read this as, "Create a variable named 'score' and set it's value to zero."

Then, as the player earns points, you could update the value of the "score" variable to reflect the new total.

score = score + 1

This code can be read from left to right as, "Take the variable named score and set it to the new value of the current score value plus one."

Variable Scopes

"Scope" is the range of something.

Here, "variable scope" refers to the range within a program where a variable can be accessed. In other words, it defines where a variable is visible and can be used in your code.

In Lua, there are two types of variable scopes: global and local.

Global Scope: 

A global variable is visible throughout the entire program, including inside any functions. It can be accessed from any part of the code. Global variables are declared outside of any function by simply assigning a value to a variable name without using the "local" keyword. However, using global variables can lead to naming conflicts and make your code harder to read and keep track of.

health = 10

function attack()
	health = health - 2

In this code, we create a global variable named "health" and set it to 10. Then inside of the function named "attack" we decrease health by 2. The attack() function can change the health variable because health has a global scope.

Local Scope: 

A local variable is visible only within a limited area of the program, such as inside a function, loop, or a block of code. It can be accessed only within that area and is not visible to the rest of the code outside of that area. Local variables are declared using the "local" keyword followed by the variable name.

For example, to declare a variable called "x", you would write "local x". You can also assign a value to the variable at the same time by writing "local x = 5" (which assigns the value 5 to the variable named "x").

local x = 5

"X" and "Y" are commonly used variable names for keeping track of a position on the screen, so it is a good idea to make them local variables, so they don't get confused. Here is an example:

function move_player()
	local x = x + 1     

function move_enemy()
	local x = x - 1     --different from move_player

print(x) --cannot find x

In this example, we have two functions for moving the player and the enemy. Both functions use a variable named X but they are not confused because they are two different local variables and they are only accessed inside of their functions. If we try to print X outside of the functions, it will not work because it is outside of the scope.

Types of Variables

Variables can hold different types of data.


Lua supports both integer and decimal (also referred to as "floating-point" or "float") numbers, which can be positive or negative. Numbers can be stored in variables like this:

defense = 10  --positive
speed = 1.5   --float
money = -30   --negative

A string is a sequence of characters, such as "hello world" or "you win!". Strings can be stored in variables like this:

text = "hello world"

Booleans are values that can be either true or false. They can be used to represent "conditions" or "states" in your code, such as if something is on or off, flipped or not flipped, alive or dead, etc.

player_is_alive = true

Tables are a fundamental data structure in Lua, used to store collections of values. If you want to group a set of variables together for better organization and easier access, you can use a table like this:

player_table = {
	x = 10,
	y = 20,
	health = 50

In Lua, functions are "first-class values" which means they can be stored in variables and passed around as arguments to other functions.

print("hello world")  --run print function
write = print         --copy print() to variable write
write("hello world")  --run write function

We can treat function names as variables that hold the function's code as its data. Just pay attention to the use of parentheses. We print "hello world" to the screen by calling the print function and using parentheses. Then we create a new variable named "write" and set its value to whatever print's value is. This copies print's code to write. Write is still a variable but not that it is holding code that can be run, it is also a function. So we can call write now just like we did with print, using parentheses and tell it what to write to the screen.


Nil is a special value in Lua that represents the absence of a value. 

potion = nil

Variables can be set to nil to erase their data and indicate that they have no value. Errors will often say that a variable is "nil" which usually means the variable has not been created, or it is out of scope.


21 Aug 2023