Variables: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Anaminus
No edit summary
>Anaminus
this is difficult. need to make a visual
Line 82: Line 82:
In this example, ''x'' is assigned to the original value of ''y'', and ''y'' is assigned to the original value of ''x''.
In this example, ''x'' is assigned to the original value of ''y'', and ''y'' is assigned to the original value of ''x''.


<!--


<!--
== Variable Scopes ==
== Variable Scope ==
Sometimes you may want to limit the access of one variable to another. You can do this by allowing a variable to be visible only to others in the same area. These areas are called '''scopes'''.
In normal Lua, there are two scopes, known as '''global''' and '''local'''.


local, global, shared




=== Local Variables ===
=== 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:
A local variable is a variable that can only be accessed by other things in the same scope. A variable is made local by adding the keyword '''local''' before the variable:
 
<pre>
<pre>
local var = "Hello" -- This assigns var to the string "Hello"
local word = "Hello"
</pre>
</pre>


You might not understand what these scopes are, just yet, so let me show you:
Each [[block]] of code has its own scope. Nice blocks can be [[nested]], each scope may have an '''outer scope''' and an '''inner scope'''.
*The inner scope is a "child" of the current scope. The current scope cannot access variables here.
*The outer scope is a the "parent" of the current scope. The current scope has access to any variables here.


<pre>
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
</pre>


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 stored in the environment of your script and is accessible to the entire script. This is the default when the "local" keyword is not used.


=== Shared Variables ===
=== Shared Variables ===

Revision as of 12:19, 28 September 2010

A variable is a name you can use to hold a value. In Lua, a variable can have any data type, such as a number or a string.

Assignment

Assigning a value to a variable can be done using the = operator, like so:

x = 10
word = "Hello"

Variable x now has a value of the number 10, and word has a value of the string "Hello". If we print out these variables, we should see the value they have:

print(x)     --> 10
print(word)  --> Hello

A variable can be changed simply by assigning another value to it:

n = 12
print(n)    --> 12

n = 24
print(n)    --> 24

Even though n was assigned to a value of 12, it was overwritten by the second assignment of 24.


You can also use a variable as a value when assigning.

n = 78
x = n

print(x)   --> 78

When x was assigned to n, it's value became the value of n.


Remember! A variable is always on the left side of the = while a value is on the right side.

Names

The name you give a variable can only contain letters (uppercase and lowercase), numbers, or an underscore ( _ ). A variable name cannot be a reserved keyword or start with a number. Other than that, there are no restrictions.

LETTERS = "ABC"          -- valid
x86 = 1978               -- valid
var_name = "variable"    -- valid
_ = "blank"              -- valid

if = 12                  -- NOT valid!
16th = "Lincoln"         -- NOT valid!

Multiple Assignment

Lua allows you to assign more than one value to more than one variable.

a, b, c = 1, 2, 3

print(a, b, c)    --> 1 2 3

Each variable and each value is separated by a comma. Values to the right of the = are assigned to their corresponding variables to the left. The first variable is assigned to the first value, the second variable is assigned to the second value, and so on. The above example is the same as this:

a = 1
b = 2
c = 3

print(a, b, c)    --> 1 2 3

Swapping

Multiple assignment is useful because it allows you to easily swap the values of two variables.

x, y = 8, 36
print(x, y)    --> 8 36

x, y = y, x

print(x, y)    --> 36 8

In this example, x is assigned to the original value of y, and y is assigned to the original value of x.