Variables: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>GoldenUrg
→‎In depth: fix syntax error
>Oysi93
If you mind about the 'johndoe' -> 'oysi93', then change back. I will make this article better tomorow, I promise! =) (A lot here was wrong)
Line 34: Line 34:
== Discussion ==
== Discussion ==


A <b>variable</b> 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:
A '''variable''' is a word or letter you can assign a value to. 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:


<pre>
<pre>
x = 1
x = 1
myname = "johndoe"  
myname = "oysi93"
</pre>
</pre>


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".
Notice that the word "oysi93" is in quotes, but the number 1 is not in quotes. That's because "oysi93" is considered a [[String]], or in other words, considered as text. In short, you can perform arithmetic on numbers, but not on "oysi93".


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:
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 would be 5. Let's try that in the scripting section:


== Scripting ==
== Scripting ==
Line 50: Line 50:


* Click Workspace in the Explorer Window.
* Click Workspace in the Explorer Window.
* Click Insert > Object > Script
* Click Insert > Object > Script.
* Double click the newly inserted script.
* Double click the newly inserted script.
* A blank window should open up.
* A blank window should open up.
Line 56: Line 56:


<pre>
<pre>
x = 2 -- this assigns the value of 2 to x
x = 2 -- This assigns x to the value of 2
y = 3 -- this assigns the value of 3 to y
y = 3 -- This assigns y to the value of 3
print(x+y) -- this prints the sum of (x+y)
print(x + y) -- This prints out the sum of (x+y))
</pre>
</pre>


Line 64: Line 64:
* Your Output bar should show the number 5, which is the sum of x+y.
* Your Output bar should show the number 5, which is the sum of x+y.


==In depth==
== 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":
Variables are essential to use in a good script. A variable is a shortcut to values that you can store, then retrieve and/or change later. Defining a variable is very easy, as well. To define a variable, you just type "variableName = value":


<pre>greeting = "hello"
<pre>
luckyNumber = 3
sometext = "Hello"
brick_5 = "green"
luckynumber = 93
variableIDoNotWantToDefineJustYet = nil
brick5 = "Green"
</pre>  
VariableIDoNotWantToDefineJustYet = nil
</pre>
 
To change a variable, you just do the same thing:
 
<pre>
variable = 1 -- This assigns "variable" to the value 1.
print(variable) -- This prints the variable, which is 1 now.
 
variable = 7 -- This makes the variable 7 instead of 1.
print(variable) -- This prints 7, because "variable" is now equal to 7.
 
variable = variable + 5 -- Adds 5 to the variable, making the variable 7+5.
print(variable) -- This prints 12, because 7 + 5 is 12, you should know this math...
</pre>
 
Please note that a variable cannot start with a number, nor can it be a keyword. Which means these variable names are invalid:
 
<pre>
-- DON'T EVER USE THESE NAMES WHEN ASSIGNING VARIABLES --
if = 1
12 = 2
do = "hello"
</pre>


To change a variable, you just do do the same thing, only after the variable is defined:
While these and many others aswell are valid:


<pre>
<pre>
the_variable = 1 -- The definition of "the_variable"
a = 1
print(the_variable) -- Prints "1" in the output.
hello = "hey"
the_variable = 7 -- Making "the_variable" equal 7.
mom = "nice"
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.
</pre>
</pre>


===Local Variables===
=== 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:
 
A local variable is a variable that is ONLY known to it's scope of the script. To define one you have to have "local" before it:


<pre>
<pre>
local var1 = "Hello" -- Defines var1 as "Hello".
local var = "Hello" -- This assigns var to the string "Hello"
</pre>
</pre>


===Normal Variables===
You might not understand what these scopes are, just yet, so let me show you:
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


<pre>
<pre>
var1 = trans
function Test() -- Creates a new scope (Test)
var1 = 0 -- On this line, the transparency would be 0. A local variable would only change var1 to 0.
local A = 1
This will change both!
print(A)
end -- This ends the scope 'Test', making the variable A unaccessable outside.
 
Test() -- Runs the function above
print(A) -- Tries to print A, but since A is local to the 'Test', it prints nothing
</pre>
</pre>


===Global Variables===
Which will print 1 and then nil, because the variable A can only be accessed from within the 'Test'.
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.:
 
=== Global Variables ===
A global variable is a variable that is "readable" by all scripts. For example, one is 'Game'. When you state 'Game' it thinks of the Data Model that is your game. To define a such global variable yourself, you state "_G." and then the variable name, table indexing if you know what that is. Then when another script states the variable, it will think of the variable you set it to:


Script 1
Script 1
<pre>
<pre>
_G.var1 = 10 -- This line defines a global variable "var1" and it equals 10.
_G.Var = 10 -- This defines the variable 'Var' to a global, and set it equal to 10.
</pre>
</pre>
Script2
 
Script 2
 
<pre>
<pre>
print(var1) -- Prints 10 in the output!
wait(1) -- If Script 2 runs before Script 1, the global will not have been defined...
print(_G.Var) -- This prints 10 in the output! =D
</pre>
</pre>


Line 116: Line 146:


[[Intro to Scripting: Make a Dance Floor]]
[[Intro to Scripting: Make a Dance Floor]]
[[Absolute beginner's guide to scripting]]
[[Absolute beginner's guide to scripting]]
[[Category:Scripting Tutorials]]
[[Category:Scripting Tutorials]]

Revision as of 19:43, 27 September 2010

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 Roblox Studio. Once you have done that, you will need to click "My ROBLOX", select your map, and click "Edit". At this point, you should see your familiar map.

You will need some essentials in Roblox Studio to make this work:

  • The Output window:

File:Outputwindow.JPG

  • 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 you can assign a value to. 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 = "oysi93"

Notice that the word "oysi93" is in quotes, but the number 1 is not in quotes. That's because "oysi93" is considered a String, or in other words, considered as text. In short, you can perform arithmetic on numbers, but not on "oysi93".

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 would be 5. Let's try that in the scripting section:

Scripting

Open Roblox 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 x to the value of 2
y = 3 -- This assigns y to the value of 3
print(x + y) -- This prints out the sum of (x+y))
  • Test the script by pressing the play button File:Play.JPG
  • 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 shortcut to values that you can store, then retrieve and/or change later. Defining a variable is very easy, as well. To define a variable, you just type "variableName = value":

sometext = "Hello"
luckynumber = 93
brick5 = "Green"
VariableIDoNotWantToDefineJustYet = nil

To change a variable, you just do the same thing:

variable = 1 -- This assigns "variable" to the value 1.
print(variable) -- This prints the variable, which is 1 now.

variable = 7 -- This makes the variable 7 instead of 1.
print(variable) -- This prints 7, because "variable" is now equal to 7.

variable = variable + 5 -- Adds 5 to the variable, making the variable 7+5.
print(variable) -- This prints 12, because 7 + 5 is 12, you should know this math...

Please note that a variable cannot start with a number, nor can it be a keyword. Which means these variable names are invalid:

-- DON'T EVER USE THESE NAMES WHEN ASSIGNING VARIABLES --
if = 1
12 = 2
do = "hello"

While these and many others aswell are valid:

a = 1
hello = "hey"
mom = "nice"

Local Variables

A local variable is a variable that is ONLY known to it's scope of the script. To define one you have to have "local" before it:

local var = "Hello" -- This assigns var to the string "Hello"

You might not understand what these scopes are, just yet, so let me show you:

function Test() -- Creates a new scope (Test)
local A = 1
print(A)
end -- This ends the scope 'Test', making the variable A unaccessable outside.

Test() -- Runs the function above
print(A) -- Tries to print A, but since A is local to the 'Test', it prints nothing

Which will print 1 and then nil, because the variable A can only be accessed from within the 'Test'.

Global Variables

A global variable is a variable that is "readable" by all scripts. For example, one is 'Game'. When you state 'Game' it thinks of the Data Model that is your game. To define a such global variable yourself, you state "_G." and then the variable name, table indexing if you know what that is. Then when another script states the variable, it will think of the variable you set it to:

Script 1

_G.Var = 10 -- This defines the variable 'Var' to a global, and set it equal to 10.

Script 2

wait(1) -- If Script 2 runs before Script 1, the global will not have been defined...
print(_G.Var) -- This prints 10 in the output! =D

See Also

Intro to Scripting: Make a Dance Floor Absolute beginner's guide to scripting