Global Functions: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Sncplay42
→‎Introduction: changing intro, things like :Clone() are unrelated
>Camoy
A better written article
Line 3: Line 3:


== Introduction ==
== Introduction ==
Global functions are function accessible in all environments and threads.  Global functions must be contained within what is called the global environment which is represented by a table called the global table.  _G is the variable that holds the global table.  In ROBLOX _G works differently than in normal Lua.  In ROBLOX you must index values from the global table if you set them.


Have you ever wanted to share information between scripts without using the -Value objects? You can by using the _G table!
{{Example|<pre>
_G["foo"] = "bar"
print(_G["foo"]) -- bar
</pre>}}


_G is a [[table]] that all scripts can read and change.
Any function, and any thread can access the "foo" variable so long as the example code is run before the other thread is executed.


== How a global command works ==
== History ==
Sometime in 2010 it was decided that _G was a security issue.  Before this date all threads could read and write to the global table.  Instead, a substitute table was put in place of _G.  This was a table called "shared".  All threads could access the "shared" table, however you must actually index it:


If you know how to use the _G[] (global) command, this might come more easily to you than others. This is basically how a global command works:
{{Example|<pre>
shared["foo"] = "bar"
print(shared["foo"]) -- bar
</pre>}}


<pre>_G["Bob"] = game.Workspace.Bob</pre>
The way _G worked before allowed this:


If you're in a ScriptBuilder, you can exit that script and open a new one. Put this in it:
{{Example|<pre>
_G["foo"] = "bar"
print(foo) -- bar
</pre>}}


<pre>_G.Bob.Torso:Remove()</pre>
Values within the _G table would be replicated into all the threads environments, not just the _G table itself.  The shared table can still be accessed today, however it is suggest that one would use _G instead.
 
run that script, and Bob's torso gets removed! Why? Because when we made the assignment using _G["Bob"]=, we are creating an entry in the _G table, and we can reference that entry by calling "_G.Bob".
 
== Getting a global function ==
 
<pre>
_G["Lazer"] = function(person)
person.Torso:Remove()
end
</pre>
 
Now exit that script, and run this script:
 
<pre>_G.Lazer(game.Workspace.Bob)</pre>
 
If your character's name is "Bob," you should have your torso removed.


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

Revision as of 21:33, 23 September 2010

Introduction

Global functions are function accessible in all environments and threads. Global functions must be contained within what is called the global environment which is represented by a table called the global table. _G is the variable that holds the global table. In ROBLOX _G works differently than in normal Lua. In ROBLOX you must index values from the global table if you set them.

Example
_G["foo"] = "bar"
print(_G["foo"]) -- bar


Any function, and any thread can access the "foo" variable so long as the example code is run before the other thread is executed.

History

Sometime in 2010 it was decided that _G was a security issue. Before this date all threads could read and write to the global table. Instead, a substitute table was put in place of _G. This was a table called "shared". All threads could access the "shared" table, however you must actually index it:

Example
shared["foo"] = "bar"
print(shared["foo"]) -- bar


The way _G worked before allowed this:

Example
_G["foo"] = "bar"
print(foo) -- bar


Values within the _G table would be replicated into all the threads environments, not just the _G table itself. The shared table can still be accessed today, however it is suggest that one would use _G instead.