|
| 03 Jan 2016 07:23 PM |
Read the function dump, but still not clear to me.
Can someone explain it a bit to me, and what's the purpose or advantage I can use for scripting? (A simple example would do) |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2016 07:27 PM |
hi = 5
print(getfenv().hi)
I has a few uses, but not many. |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2016 07:31 PM |
| So, from what I understand, getfenv() allows you to get variables as if they're in a table? |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2016 07:34 PM |
Only global variables. It can't access local variables.
But it can be used a lot for sandboxing, escaping sandboxes, and wrapping and such. It has its uses, but not very many are really practical or useful. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 03 Jan 2016 07:45 PM |
| Global variables of a function are stored in a special table, by default the "global environment." |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2016 07:59 PM |
| They are stored in a special table, very specifically for 'a script', or it is accessible to all server script? What if it is a LocalScript? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 03 Jan 2016 08:01 PM |
Per function, and each script has it's own global environment for reasons I won't dive too deeply into... just kidding I already have:
docs.google dot com/document/d/1A1SBjRqFKFxaw7_RnsI96CKzBCcAOkuDPOTG8DIZywA |
|
|
| Report Abuse |
|
|
| |
|
| |
|
|
| 03 Jan 2016 08:47 PM |
getfenv returns the function environment of the supplied function
function a()
end
getfenv(a)
when you execute a lua script, what it actually does is wrap it in an anonymous function and call that function
(function() -- all of your code end)()
when you call getfenv with no arguments, it returns the environment of that anonymous function |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2016 08:49 PM |
So, if getfenv() == {},
According to your function a(),
for i,v in pairs(getfenv(a)) do print(v) end
What does it print out? |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2016 08:51 PM |
Script function: 19A9A698
Where Script is the script that the global variable 'script' points to, and 'function: 19A9A698' is the 'a' function.
|
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 03 Jan 2016 08:53 PM |
| And all the cool things (math table, print function, etc) are hidden in the global environment's metatable |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2016 08:55 PM |
Is this all or there's more? I would like to try something with it. If there is, what would you recommend me to do? |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2016 08:57 PM |
Think of what an environment is in real life. Your environment has buildings, houses, signs, roads, all that stuff in it.
"fenv" is a function environment. It's the environment of a function. It has variables, functions, and code in it.
getfenv() gets the function environment and lets you access all variables and functions directly.
When used with the require() method this can allow you to directly use functions inside of a ModuleScript without calling the ModuleScript at all (like including a library in C++). |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 03 Jan 2016 09:09 PM |
| That's misleading, all an environment holds are the globals. "functions" are referenced to by variables in Lua (they're first class much like other types) and the "code" is not stored in the env. |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2016 09:10 PM |
| Okay then, revision: all of the variables and functions are stored there. I say those as two different things because I would think declaring a variable and declaring a function are two separate things. But if that's not the case I guess it only holds variables. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 03 Jan 2016 09:14 PM |
| It's not really the case in Lua since they're first class. |
|
|
| Report Abuse |
|
|