This guide is for absolute beginners. It is intended to familiarize you with Variables in Lua. If you haven't already, please see Your first script as a beginner tutorial.
What are we trying to do?
At the end of this guide, you will be able to:
- Know what a variable is
- Create a variable
- Assign a value to a variable
- Use a variable in a mathematical equation
You will need to open GoodBlox Studio. Once you have done that, you will need to click "My GoodBlox", select your map, and click "Edit". At this point, you should see your familiar map.
You will need some essentials in GoodBlox Studio to make this work:
- The Output window:
- The Explorer window:
- The Command bar:
- To make sure you have the Output window, click View / Output.
- To make sure you have the Explorer window visible, click View / Explorer.
- To make sure you have the Command bar visible, click View / Toolbars / Command.
A variable is a word or letter to which you can assign a value. For instance, in algebra, you can give "x" the value of 10. In lua, there's a little more liberty -- not only can a variable be a number, but it can also be a word, like your name:
x = 1 myname = "johndoe"
Notice that the word "johndoe" is in quotes, but the number 1 is not in quotes. That's because "johndoe" is considered a String. In short, you can perform arithmetic on numbers, but not on "johndoe".
Arithmetic can be performed on variables, just as on numbers. For example, if x is equal to 2 and y is equal to 3, then x+y=5. Let's try that in the scripting section:
- Click Workspace in the Explorer Window.
- Click Insert > Object > Script
- Double click the newly inserted script.
- A blank window should open up.
- Insert the following script:
x = 2 -- this assigns the value of 2 to x y = 3 -- this assigns the value of 3 to y print(x+y) -- this prints the sum of (x+y)
- Test the script by pressing the play button
- Your Output bar should show the number 5, which is the sum of x+y.
Variables are essential to use in a good script. A variable is a value you can store, then retrieve and/or change later. Defining a variable is very easy, as well. To define a variable, just type "variableName = initial value":
greeting = "hello" luckyNumber = 3 brick_5 = "green" variableIDoNotWantToDefineJustYet = nil
To change a variable, you just do do the same thing, only after the variable is defined:
the_variable = 1 -- The definition of "the_variable" print(the_variable) -- Prints "1" in the output. the_variable = 7 -- Making "the_variable" equal 7. print(the_variable) -- Prints "7" in the output. Not 8. we did not add. the_variable = the_variable + 5 -- Making "the_variable" increment by 5. print(the_variable) -- Prints "12" in the output.
A local variable is a variable only known ONLY to the script that defines it. To define one you have to "local" before it:
local var1 = "Hello" -- Defines var1 as "Hello".
var1 = trans var1 = 0 -- On this line, the transparency would be 0. A local variable would only change var1 to 0. This will change both!
A global variable is a variable that is "readable" by all scripts. One is "game". When you state "game" it thinks of the Data Model that is you game. To define one, you state "_G."(no space)then the variable name. Then when another script states the variable, it will think of the variable you set it to. Only the script that defines the global variable can change it.:
_G.var1 = 10 -- This line defines a global variable "var1" and it equals 10.
print(var1) -- Prints 10 in the output!