Coding - Variables
Variables let your script hold information. Using a variable you can put aside some data for later and then call it back whenever you want. Variables themselves don't have a type assigned to them and they can hold absolutely anything.
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!
If you remember, last time we did this:
This was simple enough. Whenever the script was opened, 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.
So, let's take our script apart so we can understand it.
print is the name of the binding. The ( and the ) (these symbols are called parentheses) are where you put the text for print to print.
"Hello World!" is the text we chose to print. 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:
doesn't work. Lua will think that Hello and World! are the name of a variable, instead of a string.
Any time we want lua to know that the information we're giving it is text, we put it inbetween quotation marks. Text in quotation marks is called a string.
Whew! Are you still with me? These are the words you need to remember so far: Variable Binding Function Print Parentheses Quotation Marks String
Okay, now that we've gone over how print works and about text and whatnot, we'll move onto how to write variables.
A variable can be created by doing this:
The name of the variable is on the left. The equal sign says you want to set this variable to what's on the right. If you remembered well, "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 myName is Jeff. Heh, see?
You can change variables the same way you make them. Like so:
The example above prints "Jeff" first, and "Josh" second.
Local variables are variables that are available only in the code block they are defined in, and in all child code blocks. They are defined like this:
In most cases you want to use local variables, so other scripts can't override them or so that your script won't break other scripts.
This example shows the difference between a global variable and a local variable:
This code will have this output:
"I don't like ".."being apart." becomes "I don't like being apart.".
Likewise, you can join a string and a variable:
This prints "Welcome to the Atlantic, Jeff" because myName is "Jeff". So really what it's doing is: "Welcome to the Atlantic, ".."Jeff"
You can use .. as many times as you want. See below:
This prints :
This prints this into the console:
Next we will learn about operators.