Variables

From GMod Wiki

Jump to: navigation, search

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 Msg with a variable instead of text. As you know by now, the first way we used message was like so.

Msg("Hello World!")


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

myName="Jeff" 
Msg(myName)


Whenever this runs, Msg 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" 
Msg(myName) 
myName="Josh" 
Msg(myName)


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


Concatenation

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:

Msg("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" 
Msg("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" 
Msg("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."

Newlines

Last thing we will cover here is how to use newlines. Up until now, we have been using Msg without any newlines. Please note that text given to Msg needs to be ended with a newline, otherwise you'll start seeing weird things, like new messages being printed at the end of old messages.

All text given to Msg should have an \n at the end. See below:

Msg("Testing Message 1\n") 
Msg("Testing Message 2\n") 
Msg("Testing Message 3\n")


With newlines, the following is put in the console:

Testing Message 1
Testing Message 2
Testing Message 3


But... without newlines: Testing Message 1Testing Message 2Testing Message3

So you see why they're so important. You can use newlines anywhere in the string to make a new line (see below):

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


This prints this into the console:

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


So, give this a try yourself! In notepad, type the following:

myName="Jeff" 
Msg(myName.."\n") 
Msg("Hello "..myName.."\n") 
Msg("Hello "..myName..", how are you today?\n") 
Msg("Trying to find "..myName..". "..myName.." is lost!\n") 
Msg("One line.\nAnother line.\nA third line.\n")


Save this in your lua folder as "varsAndStrings.lua" and open it in Garry's Mod. If everything went well your console should have this printed into it:

Jeff
Hello Jeff, how are you today?
Trying to find Jeff. Jeff is lost!
One line.
Another line.
A third line.


Experiment with this and see what you can make!


Yarin Kaul Icon ArrowSquare32 left.png About the Tutorial

Yarin Kaul Icon ArrowSquare32.png Back to Lua Tutorial Series

If, then, else Yarin Kaul Icon ArrowSquare32 right.png


Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox