Variables
Questions we will be answering:
- What are they?
- What’s the point in using them?
- How do I use them?
Expected knowledge:
- None
What are variables?
Variables are words used to (temporarily) store values in your scripts. I say temporarily as they do not save over different games.
What’s the point in them?
Variables are useful for a number of reasons:
- Storing data (most relevant)
- Stopping repeated code
- Making scripts easier to read / understand
- (In some cases) helping with memory / lag
How do I use them?
Variables have a
name
and avalue
. They key is the name, and the value is the value attached to it. For example, if you were making a gun, you might want a variable for the damage. The name could be called “Damage” and the value could be “100”. The way we make variables is simple - you writename = value
like this:
Damage = 100
It should also be noted that variables can store any piece of data (value) that you want, so for example it can store
strings
(letters / words),numbers
,floats
(numbers with a decimal) and many other types, that we will get into at a future date.
As for the names of variables, you can use numbers, letters and underscores, so long as the name does not start with a number. So for example you couldn’t say:
123variable = 17
To actually use these variables in your scripts is easy; you simply write out the
name
of it, and you’re done! The script will automatically convert where you’ve used the name of your variable into the value.
You do not need to put any speech marks in around the word, just type it out as normal. Example:
text = "Hello world!"
print(text)
Memory usage
Variables tend to not take up a lot of memory, but can sometimes take up more than necessary. The way we counter this is by putting the keyword
local
in front of our variable names. You will not notice any difference at this point, but I will explain it during theif statements
tutorial where we useindentations
. But in short, the way you want to be writing your variables is like this:
local name = "value"
Concatenation
You may have also noticed that I mentioned something called “concatenation” in the previous tutorial. This is a way of joining variables and strings together. For example, when using
print()
you can only put one thing inside the brackets - but what if we wanted to print out a regular string and a variable at the same time? Doing something like this wouldn’t work:
local name = "Jimmy"
print("My name is name")
Because it would just print out “My name is name”. This is where
concatenation
comes in - to seperate the string and the variable. The way we use concatenation is by first ending the string (having both speech marks in place, i.e. “my string” then using two dots (full stops, periods…) aspace
and our variable name. Like this:
local name = "Jimmy"
print("My name is".. name)
And this will print out
My name is Jimmy
as expected.
Useful tip: Name your variables appropriately
A common mistake seen in new programmers is that they don’t name their variables well. By “well” I mean they don’t give them names that make sense… for example, if you were making a variable for the damage of a gun, don’t name it something random like “aaa = 100”. This will just make reading and understanding your scripts a lot tougher.
Now that you’ve learnt about variables, have a go at testing your knowledge on these questions:
Question 1 (Easy)
How would I create a variable called Points
with a value of 100
?
Hint: The way we create variables is by writing local name = value
Answer:
local Points = 100
Question 2 (Medium)
How would I make a variable called food
and print out “I like food name”?
Hint: Make sure you’ve ended the string properly, used two dots and spelled the variable correctly
Answer:
local food = "Pancakes"
print("I like ".. food)
Question 3 (Hard)
How would I store the sum
of two variables (what variable + variable is equal to) in another variable, then print it out?
Hint: We use an arithmetic
- the plus sign (+). - This is what we’ll look at in the next tutorial
Answer:
local num1 = 5
local num2 = 3
local answer = num1 + num2
print(num1.. " add " ..num2.. " = " ..answer)