Chapter 2: Variables
What is a variable?
By far, the best analogy of a variable I have seen is that of a box. Let's pretend that a variable is like a box. You can put things in the box, you can see what's in the box, and you can change what's in the box. The same is true for variables. A variable is a storage place to put values. Also similar to a box, it's hard to know what's in it unless it's labelled. Every variable needs a name. So what does this look like in Lua?
lua code:
myVariable = 12
Here, we've created a variable with the name myVariable and given it a value of 12.
I'll talk a lot about good programming practice throughout the tutorial. This is one of those times. Unless explicitly declared, a variable is global. We typically want to work with local variables, not global. We'll discuss what this means when we get in to the topic of scope. Anyways, it is good practice to declare variables like this:
lua code:
local myVariable = 12
This does the same thing as the above example, except we've made the variable local.
Getting back to the analogy of the box, boxes can hold many things. They can hold shoes, they can hold computers, they can hold cups, they can even hold other boxes. Variables, once again, are the same way. They can hold many types of data. Such as... numbers, sentences (known as strings), true/false (known as boolean). Here is an example of each:
lua code:
local myNumber = 12
local myNumber2 = 5.15125617
local myString = "Hello, XenoBot!"
local myBoolean = false
A whole number, a decimal number, a string, and a boolean in that order.
At this point, I'm going to introduce two functions which are a part of Lua. Don't worry, we'll go over functions later. The functions I'm going to introduce are print() and type().
print() is the default method of outputting information. If you want your script to have some sort of text output, you will need to use print() to see it.
type() is important to variables. If you provide it with a variable, it will tell you what kind of variable it is. Meaning it will tell you if it's a number, a string, or anything else.
Here is an example using these functions. What the script would output will be marked by a '>' character at the beginning of the line. With XenoBot, any use of the print() function will result in a green message in game.
lua code:
local myNumber = 12
local myType = type(myNumber)
print(myType)
>number
local myString = "Hello, XenoBot!"
local myType = type(myString)
print(myString)
>string
Displaying what kinds of variables we have using type() and print().
Naming Variables
This is important. It has it's own section, even. A big part of understanding what is happening in a script is coherently named variables. For instance, if I wanted a script that healed at a certain health I could put something like:
lua code:
local a = 100
but then when I try to read through it later, I need to remember what a is for. It would be much better if I did something like this:
lua code:
local hpToHeal = 100
This way, I can clearly understand that whatever value I have put in that variable is intended to be for what hp I want to heal at. Variables can not have any spaces in their name. Thinking of good variable names is important. Also, it's important to make sure that they are readable and as short as possible (without, of course, jeopardizing understanding). Here are some examples of what to do, and what not to do:
lua code:
local variableToRememberHPThatIShouldHealAt = 120
local a = 120
local hEAlthTOheaL = 120
local HEALTHTOHEAL = 120
local healthToHeal = 120
local HpToHeal = 120
local HealAtHP = 120
Variable name examples. Top are not a good idea, bottom are more recommended.
As you may have guessed, the top four are bad examples, and the bottom three are good examples. To learn more about how to form variable names some good conventions are Camel case and Pascal case. Some information can be found here (microsoft.com), but for a more in-depth explanation try googling each one and reading up on it.
Accessing Variables
Alright, we now know how to make variables. The important question is now how do we use them? It's actually quite easy. Once we've made a variable that has a value, we can use it's name in place of the value. What I mean is that if you have a variable called myVariable that has a value of 12, you can then use myVariable in your code instead of writing 12.
lua code:
local myVariable = 12
local anotherVariable = myVariable
Above, we've created the variable myVariable and set it's value to 12. Then, we've created the variable anotherVariable and set it's value to whatever myVariable is. That means that anotherVariable is going to equal 12.
An important note is that a variable must exist in order for you to access it. If you try to access a variable that does not yet exist, you will receive an error. An example of this would be:
lua code:
local myVariable = myOtherVariables
This would result in an error. The variable myOtherVariable has not been created and therefore can not be found.
Now that we have the basics of variables understood, let's move on!