Difference between revisions of "Setfenv"
From GiderosMobile
m (Text replacement - "</source>" to "</syntaxhighlight>") |
|||
| Line 3: | Line 3: | ||
'''Class:''' [[(global)]]<br/> | '''Class:''' [[(global)]]<br/> | ||
| + | '''This function is deprecated''' | ||
=== Description === | === Description === | ||
Sets the environment to be used by the given function. | Sets the environment to be used by the given function. | ||
| Line 19: | Line 20: | ||
=== Return values === | === Return values === | ||
'''Returns''' (varies) returns provided function or nil<br/> | '''Returns''' (varies) returns provided function or nil<br/> | ||
| + | |||
| + | === Example === | ||
| + | <syntaxhighlight lang="lua"> | ||
| + | a = 1 -- create a global variable | ||
| + | -- change current environment | ||
| + | setfenv(1, {_G = _G}) | ||
| + | _G.print(a) --> nil | ||
| + | _G.print(_G.a) --> 1 | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | === See also === | ||
| + | '''https://www.lua.org/pil/14.3.html''' | ||
{{(global)}} | {{(global)}} | ||
Revision as of 16:23, 2 February 2025
Available since: Gideros 2011.6
Class: (global)
This function is deprecated
Description
Sets the environment to be used by the given function.
(varies) = setfenv(f,table)
f can be a Lua function or a number that specifies the function at that stack level: Level 1 is the function calling setfenv. setfenv returns the given function.
As a special case, when f is 0 setfenv changes the environment of the running thread. In this case, setfenv returns no values.
Parameters
f: (varies) function or number(call stack level)
table: (table) environment table to set
Return values
Returns (varies) returns provided function or nil
Example
a = 1 -- create a global variable
-- change current environment
setfenv(1, {_G = _G})
_G.print(a) --> nil
_G.print(_G.a) --> 1
See also
https://www.lua.org/pil/14.3.html