Variables: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Anaminus
No edit summary
m Text replacement - "</SyntaxHighlight>" to "</syntaxhighlight>"
 
(13 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{CatUp|Tutorials}}
A '''variable''' is a name you can use to hold a value. In Lua, a variable can have any [[Data Types|data type]], such as a [[number]] or a [[string]].
A '''variable''' is a name you can use to hold a value. In Lua, a variable can have any [[Data Types|data type]], such as a [[number]] or a [[string]].


Line 4: Line 5:
== Assignment ==
== Assignment ==
Assigning a value to a variable can be done using the '''=''' [[operator]], like so:
Assigning a value to a variable can be done using the '''=''' [[operator]], like so:
<pre>
<syntaxhighlight lang="lua">
x = 10
x = 10
word = "Hello"
word = "Hello"
</pre>
</syntaxhighlight>


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:
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:
<pre>
<syntaxhighlight lang="lua">
print(x)    --> 10
print(x)    --> 10
print(word)  --> Hello
print(word)  --> Hello
</pre>
</syntaxhighlight>


A variable can be changed simply by assigning another value to it:
A variable can be changed simply by assigning another value to it:
<pre>
<syntaxhighlight lang="lua">
n = 12
n = 12
print(n)    --> 12
print(n)    --> 12
Line 22: Line 23:
n = 24
n = 24
print(n)    --> 24
print(n)    --> 24
</pre>
</syntaxhighlight>
Even though ''n'' was assigned to a value of 12, it was overwritten by the second assignment of 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.
You can also use a variable as a value when assigning.
<pre>
<syntaxhighlight lang="lua">
n = 78
n = 78
x = n
x = n


print(x)  --> 78
print(x)  --> 78
</pre>
</syntaxhighlight>
When ''x'' was assigned to ''n'', it's value became the value of ''n''.
When ''x'' was assigned to ''n'', it's value became the value of ''n''.


Line 39: Line 40:


== Names ==
== 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. <!-- call me on this -->
The name you give a variable can only contain letters (uppercase and lowercase), numbers, or an underscore ( _ ), this means that you shouldn't use space characters in the name of your variable. It must start with an underscore or a letter. It cannot be a keyword (one that is highlighted blue in the editor).


<pre>
<syntaxhighlight lang="lua">
LETTERS = "ABC"          -- valid
LETTERS = "ABC"          -- valid
x86 = 1978              -- valid
x86 = 1978              -- valid
Line 49: Line 50:
if = 12                  -- NOT valid!
if = 12                  -- NOT valid!
16th = "Lincoln"        -- NOT valid!
16th = "Lincoln"        -- NOT valid!
</pre>
</syntaxhighlight>


== Multiple Assignment ==
== Multiple Assignment ==
Lua allows you to assign more than one value to more than one variable.
Lua allows you to assign more than one value to more than one variable.


<pre>
<syntaxhighlight lang="lua">
a, b, c = 1, 2, 3
a, b, c = 1, 2, 3


print(a, b, c)    --> 1 2 3
print(a, b, c)    --> 1 2 3
</pre>
</syntaxhighlight>


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:
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. You can see how it saves a lot of space:
<pre>
<syntaxhighlight lang="lua">
a = 1
a = 1
b = 2
b = 2
Line 67: Line 68:


print(a, b, c)    --> 1 2 3
print(a, b, c)    --> 1 2 3
</pre>
</syntaxhighlight>


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


<pre>
<syntaxhighlight lang="lua">
x, y = 8, 36
x, y = 8, 36
print(x, y)    --> 8 36
print(x, y)    --> 8 36
Line 79: Line 80:


print(x, y)    --> 36 8
print(x, y)    --> 36 8
</pre>
</syntaxhighlight>
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''.


Line 94: Line 95:


=== Global Variables ===
=== Global Variables ===
A global variable is a variable that is visible to all scopes of a script.
A global variable is a variable that is visible to all scopes of a script. (Not to be confused with adding variables to the "_G" or "shared" tables, which are shared amongst scripts.)


<pre>
<syntaxhighlight lang="lua">
a = 1
a = 1
if true then
if true then
Line 107: Line 108:
end
end
print(a)            --> 2
print(a)            --> 2
</pre>
</syntaxhighlight>
 
All variables will default to global, unless they are declared local (explained next).


=== Local Variables ===
=== Local Variables ===
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:
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. It is true that local variables are obtained faster than normal variables. This is because they are integrated into the current environment they were created in.
<pre>
<syntaxhighlight lang="lua">
local word = "Hello"
local word = "Hello"
</pre>
</syntaxhighlight>


A local variable is not visible to things in its outer scope.
A local variable is not visible to things in its outer scope.
<pre>
<syntaxhighlight lang="lua">
if true then    -- a new block is created
if true then    -- a new block is created
     local b = 2
     local b = 2
end
end
print(b)        --> nil
print(b)        --> nil
</pre>
</syntaxhighlight>


However, they are visible to things in its inner scope.
However, they are visible to things in its inner scope.
<pre>
<syntaxhighlight lang="lua">
local b = 2
local b = 2
if true then
if true then
     print(b)    --> 2
     print(b)    --> 2
end
end
</pre>
</syntaxhighlight>


If a variable has the same name as one in its outer scope, the outer scope's value is overwritten in the inner scope, but not the outer scope.
If a variable has the same name as one in its outer scope, the outer scope's value is overwritten in the inner scope, but not the outer scope.  


<pre>
<syntaxhighlight lang="lua">
a = 1
a = 1
if true then
if true then
Line 140: Line 143:
end
end
print(a)          --> 1
print(a)          --> 1
</pre>
</syntaxhighlight>
 


=== Shared Variables ===
=== Shared Variables ===
[[Image:Shared variables2.png|thumb|300px|Shared variables are partially visible to the global scope, such that they can be accessed, but not changed.]]
In Roblox Lua, there is a third type of variable called a shared variable. Shared variables are variables that are visible to every script (that is, they are shared between them). Some examples of a shared variable include [[Function_Dump/Core_Functions#print_.28.C2.B7.C2.B7.C2.B7.29|print]], [[game]], or [[Instance]]. A list of all shared variables can be found at the [[Function Dump]].


In Roblox Lua, there is a third type of variable called a shared variable. Shared variables are variables that are visible to every script (that is, they are shared). Some examples of a shared variable include [[Function_Dump/Core_Functions#print_.28.C2.B7.C2.B7.C2.B7.29|print]], [[game]], or [[Instance]]. A list of all shared variables can be found at the [[Function Dump]].
Remember that there are some major differences between normal Lua and Roblox Lua.
 
*In normal Lua, these variables are considered global variables, because they are completely visible in the global scope.
In normal Lua, these variables are called global variables, because they are visible in the global scope. In Roblox, these variables have been separated from the global scope, because Roblox uses multiple scripts, each having they're own [[environment]]. For security reasons, they have been modified further to prevent modification.
*In Roblox Lua, these variables have been separated from the global scope because Roblox uses multiple scripts, each having they're own [[environment]].
 
*In Roblox Lua, these variables cannot be modified for security reasons.
<!--continue-->
 
 
 
<!--
=== Shared Variables ===
 


<!-- Advanced explanation; probably not suited for this article
Since an environment is a table, they can have a [[metatable]]. Shared variables can be indexed because a script's environment has it's metatable set to index the original global environment.
-->


 
== See Also ==
A shared 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 shared variable yourself, you used the table "shared", ".", and then the variable name. Then when another script states the variable, it will think of the variable you set it to:
*[[Global Functions]]
 
*[[Scope]]
Script 1
 
<pre>
shared.Var = 10 -- This defines the variable 'Var' to a shared variable, and set it equal to 10.
</pre>
 
Script 2
 
<pre>
wait(1) -- If Script 2 runs before Script 1, the shared variable will not have been defined...
print(shared.Var) -- This prints 10 in the output! =D
</pre>
 
== See Also ==  
 
[[Intro to Scripting: Make a Dance Floor]]
[[Absolute beginner's guide to scripting]]
-->


[[Category:Scripting Tutorials]]
[[Category:Scripting Tutorials]]

Latest revision as of 06:16, 27 April 2023

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 ( _ ), this means that you shouldn't use space characters in the name of your variable. It must start with an underscore or a letter. It cannot be a keyword (one that is highlighted blue in the editor).

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. You can see how it saves a lot of space:

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.


Variable Scopes

Every time a block is opened, a new scope is created. Variables in the outer scope are visible, while variables in the inner scope are not. Variables that aren't local are apart of the global scope (shown in blue), and are visible to all scopes, no matter where they were created.

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.

Each block of code has its own scope. Since 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 in the inner scope.
  • The outer scope is a the "parent" of the current scope. The current scope has access to any variables in the outer scope.

Global Variables

A global variable is a variable that is visible to all scopes of a script. (Not to be confused with adding variables to the "_G" or "shared" tables, which are shared amongst scripts.)

a = 1
if true then
    print(a)        --> 1
    if true then
        a = 2
        print(a)    --> 2
    end
    print(a)        --> 2
end
print(a)            --> 2

All variables will default to global, unless they are declared local (explained next).

Local Variables

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. It is true that local variables are obtained faster than normal variables. This is because they are integrated into the current environment they were created in.

local word = "Hello"

A local variable is not visible to things in its outer scope.

if true then    -- a new block is created
    local b = 2
end
print(b)        --> nil

However, they are visible to things in its inner scope.

local b = 2
if true then
    print(b)    --> 2
end

If a variable has the same name as one in its outer scope, the outer scope's value is overwritten in the inner scope, but not the outer scope.

a = 1
if true then
    local a = 2
    print(a)      --> 2
end
print(a)          --> 1

Shared Variables

Shared variables are partially visible to the global scope, such that they can be accessed, but not changed.

In Roblox Lua, there is a third type of variable called a shared variable. Shared variables are variables that are visible to every script (that is, they are shared between them). Some examples of a shared variable include print, game, or Instance. A list of all shared variables can be found at the Function Dump.

Remember that there are some major differences between normal Lua and Roblox Lua.

  • In normal Lua, these variables are considered global variables, because they are completely visible in the global scope.
  • In Roblox Lua, these variables have been separated from the global scope because Roblox uses multiple scripts, each having they're own environment.
  • In Roblox Lua, these variables cannot be modified for security reasons.


See Also