I have been on Twitch streaming the development of my game here. Many people have come by and showed interest and gave me support one way or the other. I have been getting overwhelming requests from people asking if I can teach them programming personally. Instead, I have decided to start a tutorial series to educate people this way so I don’t have to repeat for each person. For this tutorial, I will assume everyone has a good understanding of the Primitive Datatypes in Lua and get right into variables. If you don’t know what they are, you can do a simple Google search to learn about them. Also my tutorial series isn’t about delivering a complete educational package. Its goal is to teach things other resources do not teach well, that I wish I had someone teach me better. If you are a beginner, you would definitely have to fill in the gaps yourself by studying other materials on the Internet.
Variables
As the name suggests, these are things in your code that can “vary”. Let’s take a look at 2 variables that are declared and defined.
local name = "Kurdiez"
local points = 100
Here we created 2 variables: name pointing to the value “Kurdiez” and points pointing to 100 respectively. It is a common misconception to think that variables are some kind of “storage” to temporarily store values. Rather, they indicate memory addresses, and therefore It is more accurate to think of them as pointers. For example, when the first line
local name = "Kurdiez"
is executed, following things happen:
- A string value is created somewhere in your computer memory.
- Then your name variable is created because the keyword “local” is infront of it. This tells Lua interpreter that you want to create a new variable (a new pointer).
- Finally, the equals sign takes the variable on its left side and make it point to a memory address containing the value added in step 1.
This way, you do not need to remember the complicated memory address of the actual value but simply access the data using the variable name . Let’s take a look at how the value is accessed:
name
That’s it. If you type in the name of the variable anywhere in your code without following it by the = equals sign, you are telling the computer to retrieve the data from wherever the variable is pointing to. This is why you are able to do:
print(name)
and have the “Kurdiez” string be printed on the screen. Just from the above simple print statement, two things are happening:
- Variable name appeared by itself, therefore follow this pointer to the location in memory and retrieve whatever the value is there.
- Pass this value to the print() function
There are more subtle details that we must discuss that the Lua interpreter does under-the-hood. So what happens if we have the following lines?
local name = "Kurdiez"
local name2 = name
name = "Kurdi"
print(name)
print(name2)
On the third line, we create “Kurdi” in the memory and make the variable name point to it. Because name2 points to the value in totally different location, we see that its value has not be modified at all. With these 5 lines of code we created 2 strings all in distinct location in the memory.
- name points to “Kurdiez”
- name2 points to where name is pointing to therefore “Kurdiez”
- name points to “Kurdi”
So in the end we are left with name pointing to “Kurdi” and name2 pointing to “Kurdiez”
Conclusion
- A variable can be declared (created) using the keyword “local”.
- A viariable is nothing but a pointer to a certain memory address where the actual value lives.
- When forcing a variable to point to some data, if the data is of a Primitive Datatype, then the data is always created fresh in a new memory location and a new memory address is pointed to by the variable.
Contact
You can find out more about my discord and any other contact information about me on my twitch channel.