Variables

From Goodblox Wiki
Jump to navigationJump to search

Introduction

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

Setup

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:

and

  • 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.

Discussion

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:

Scripting

Open GoodBlox Studio and make sure that there aren't any other scripts in the Explorer window. We're going to start off with a fresh script.

  • 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.

In depth

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. 

Local Variables

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

Normal Variables

A normal variable is a variable that changes the value it holds. You do not need to put in local. For example, let's say "trans" is the Transparency of a brick. The transparency = 0.5

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!

Global Variables

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.:

Script 1

_G.var1 = 10 -- This line defines a global variable "var1" and it equals 10.

Script2

print(var1) -- Prints 10 in the output!

See Also

Intro to Scripting: Make a Dance Floor

Absolute beginner's guide to scripting