# Kurdiez Scripting #1 : Variables

#1

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:

1. A string value is created somewhere in your computer memory.
2. 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).
3. 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:

1. Variable name appeared by itself, therefore follow this pointer to the location in memory and retrieve whatever the value is there.
2. 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.

1. name points to “Kurdiez”
2. name2 points to where name is pointing to therefore “Kurdiez”
3. 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.

#2

I like your Tutorials it’s great for new Scripters & Devs

Just something interesting that could be useful to some people

``````myprint = print
myprint('Hi')
> Output Hi

A = 5
B = 'C'

A , B = B , A

print(A,B)

> Output C 5
``````

You should use local for Vars(Variables) When you can
Scripts will access that Var faster with local it’s like a Favorite List

Also check out This Thead Simple Scripts For Beginners
(Not advertising at all )

#3

Just to clarify, there is only one ‘Kurdiez’ existing in memory, but if you changed the second string this would be more accurate. Lua is pretty smart that way.

Same goes for this, there is still only one Kurdiez in memory, but because the basic datatypes do not act as traditional pointers in Lua, the second variable can be changed without changing Kurdiez in memory.

Great work though, you’re doing a great thing, teaching people. Keep it up!

#4

Other languages do this too in the case of immutable values like primitives to be space efficient. I just didn’t have the heart to introduce such concepts yet. But it is misinforming the readers so I will fix it up. Thanks for pointing this out. I am by no means a Lua expert.

#5

Although you should always always ALWAYS use local variables, I’m not sure it’s a great idea to bring that up in tutorials meant for the absolute beginners of scripting, it might be hard enough to figure out where ends go, let alone scope.

#6

There is really no hard rule to say you should always use local variables. In most cases this is true. What needs to be said here is that if you are confused about using local or not always consider the following:

1. What scope am I in?
2. Is the variable only related to the current scope?

If the variable is part of the logic that only pertains to current scope, local keyword should be used to create the variable. I discuss about scopes and invariant functions in the next topic.

#7

No, my rule is to always always always use locals. I never use global variables in my code, ever. Of course, any discussion on this is off topic.