Garry's Mod Wiki

Coding - Variables

Variables, what are they?

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.

Recapitulation and the print function

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:

print( "Hello World!" )

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( "Hello World!" )

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:

print( Hello World! )

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.

Defining variables

A variable can be created by doing this:

myName = "Jeff"

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.

print("Hello World!")

We're using a string inside of print() to tell it what to print. But if we want to use a variable, we do this:

myName = "Jeff" print( myName )

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:

myName = "Jeff" print( myName ) myName = "Josh" print( myName )

The example above prints "Jeff" first, and "Josh" second.

Local Variables

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:

local myName = "Jeff" print( myName )

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:

local myName = "Jeff" local myAge = 18 myPetName = "Peter" filename = "test.lua" print( myName, myPetName, myLastName, myAge, filename ) if ( true ) then local myName = "George" local myLastName = "Jackson" local filename = "nope.jpg" myPetName = "Bob" myAge = 20 print( myName, myPetName, myLastName, myAge, filename ) end print( myName, myPetName, myLastName, myAge, filename )

This code will have this output:

Jeff Peter nil 18 test.lua George Bob Jackson 20 nope.jpg Jeff Bob nil 20 test.lua


Now the next thing we will learn is how to join two strings together. Joining two types of data together to make a string is called concatenation. To concatenate, you just need to use .. - See below:

print( "I don't like " .. "being apart. ")

"I don't like ".."being apart." becomes "I don't like being apart.".

Likewise, you can join a string and a variable:

myName = "Jeff" print( "Welcome to the Atlantic, " .. myName )

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:

myName = "Jeff" print( "Welcome to the Atlantic, "..myName..". As you know, we are very glad to have you. If you ever get uncomfortable " .. myName .. ", we have a sick bay on board to assist you." )

This prints :

Welcome to the Atlantic, Jeff. As you know, we are very glad to have you. If you ever get uncomfortable Jeff, we have a sick bay on board to assist you.


Last thing we will cover here is how to use newlines. Newlines are denoted by \n in a string. You can use newlines anywhere in the string to make a new line (see below):

myName = "Jeff" print( "Hello, " .. myName .. "!\nHow are you today?\nI'm feeling great." )

This prints this into the console:

Hello, Jeff! How are you today? I'm feeling great.

Multiline strings can also be defined by using double square brackets instead of quotes. This allows you to create newlines in the string by just pressing enter.


print([[Hello, Jeff! How are you today? I'm feeling great.]])
Hello, Jeff! How are you today? I'm feeling great.

What's next?

Next we will learn about operators.

Page Links

Special Pages



Render Time: 69ms

DB GetPage 6
Generate Html 33
SaveChanges (1) 14
Render Body 0
Render Sidebar 13