Global Functions: Difference between revisions
>Camoy past tense |
>Samacado pre/code change, removed subjective opinions from article - todo: 'shared' article user:legend26? |
||
Line 5: | Line 5: | ||
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. | 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|< | {{Example|<code lua> | ||
_G["foo"] = "bar" | _G["foo"] = "bar" | ||
print(_G["foo"]) -- bar | print(_G["foo"]) -- bar | ||
</ | </code>}} | ||
Any function, and any thread can access the "foo" variable so long as the example code is run before the other thread is executed. | Any function, and any thread can access the "foo" variable so long as the example code is run before the other thread is executed. | ||
Line 15: | Line 15: | ||
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: | 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|< | {{Example|<code lua> | ||
shared["foo"] = "bar" | shared["foo"] = "bar" | ||
print(shared["foo"]) -- bar | print(shared["foo"]) -- bar | ||
</ | </code>}} | ||
The way _G worked before allowed this: | The way _G worked before allowed this: | ||
{{Example|< | {{Example|<code lua> | ||
_G["foo"] = "bar" | _G["foo"] = "bar" | ||
print(foo) -- bar | print(foo) -- bar | ||
</ | </code>}} | ||
Values within the _G table would be replicated into all the threads environments, not just the _G table itself | Values within the _G table would be replicated into all the threads environments, not just the _G table itself. | ||
[[Category:Scripting Tutorials]] | [[Category:Scripting Tutorials]] |
Revision as of 02:19, 14 January 2012
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.
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:
The way _G worked before allowed this:
Values within the _G table would be replicated into all the threads environments, not just the _G table itself.