Coding - Variables
Variables let your script hold information. Using a variable, you can store some piece of data once and then reference it later whenever you want. Lua doesn't require you to declare a variable as strictly being a certain type, so they can hold absolutely anything, including nothing (
From now on, it is assumed you don't need explanations about opening notepad, saving, and loading a script in Garry's Mod. Refer to the first page if you forgot.
Open up your editor. We'll be making a new script, with variables!
In the last tutorial, we learned how to print a simple
string to the console:
This was simple enough: whenever our script ran, it said "Hello World!" in the console:
print() is a function. A function is a command that does something when you call it. Many functions can take arguments, which is data you give the function to change exactly what it does. In this case, print() takes one argument, which is a string (a series of letters, numbers, spaces, and so on), and when print() is called, it puts that string into the console.
Let's dissect the code piece by piece so we can understand it.
( and the
) (these symbols are called parentheses) are where you put the text you want
"Hello World!" is the text we chose to print; it is a
string - Notice the
"" (quotation marks). The quotation marks tell Lua that everything in between them is a string, and not the name of a variable. Doing this:
won't work, because we're missing the quotation marks! If we do this, Lua will think that
World! are the names of two variables, instead of a string literal. Remember: any time we want Lua to know that the information we're giving it is some form of text, we put the text in between quotation marks to represent it as a string.
Whew! Are you still with me? These are the words you need to remember so far:
- Variable (holds a value)
- Function (does something, may take arguments)
- Arguments, a.k.a. Parameters (functions take these and do things with them)
- String (a type of variable that holds text data)
- Quotation Marks
Now that we know about how functions work, how strings are represented, and how to call functions, let's move on to how to define, or declare, variables and assign values to them.
A variable can be created by doing this:
The name of the variable (
myName) is on the left. The equal sign says you want to set this variable's value to what's on the right. If you remember, "Jeff" is a string, because it has quotation marks around it.
myName is the name of a variable, because it doesn't have quotation marks around it! Getting the hang of it?
So now you're probably thinking, what's the point of this? Well, that's what we're here to see.
But first, let's see how to use print() with a variable instead of text. As you know by now, the first way we used message was like so.
Whenever this runs, print() prints
Jeff into the console, because
Jeff. Heh, see?
You can change variables the same way you make them. Like so:
The example above prints
Jeff first, and
Hopefully you're not too confused at this point. Luckily, if you've done math before, especially algebra, you've already seen variables and functions, and even arguments! For example, think back to when an algebra problem said
This means that
x, the name of the variable, holds the value
4, which can also be said like "4 is assigned to x", or "x is 4".
In algebra, we may have also seen a function named
f defined as
If we "plugged in", or "passed", the value of our variable
x to the function
f(x), the function would use it to calculate
9, because it would multiply
2 times 4 and add
1. In this case,
f is the function,
x is the argument, and whatever gets calculated inside the function (
2x + 1) is entirely dependent on the value of the argument
x that was passed to it. For example, if we passed
3 as the argument instead, then the function would calculate
Knowing how that works, we could try writing it in Lua using what we've learned so far. Since we don't know how to define our own functions yet, we will just tell the code to print the result of
2x+1 using the
Hopefully this might help you understand variables, functions, and arguments a little better.
Local variables are variables that are available only in the code block they are defined in (also known as the "scope" they are defined in), and in all of the subsequent child code blocks. They are defined like this:
In most cases, you will want to use local variables so that other scripts can't override them, or so that your script won't override something from another script.
This example shows the difference between a global variable and a local variable - specifically, how scoping works:
This code will have this output:
Joining two types of data together to make a string is called concatenation. To concatenate, you just need to use
.. Here's an example:
"I don't like " .. "being apart." becomes
"I don't like being apart."
Likewise, you can join a string literal and a variable:
"Welcome to the Atlantic, Jeff" because
myName is holding the value
"Jeff". What it's really doing is:
"Welcome to the Atlantic, " .. "Jeff"
You can use
.. as many times as you want. See below:
We can even concatenate other types of values, like numbers, with a string:
The last thing we will cover here is how to use newlines inside strings. Newlines are created by writing
\n in a string where you want the new line to start. You can use newlines anywhere in the string to insert a new line (see below):
This prints this into the console:
Next we will learn about conditions.