|
| 20 Apr 2015 12:25 PM |
Why does the example on the wiki not work? O.e
_G.a = 1 setfenv(1, {_G = _G}) _G.print(a) _G.print(_G.a)
attempt to call field 'print' (a nil value) |
|
|
| Report Abuse |
|
|
MrNicNac
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 26567 |
|
|
| 20 Apr 2015 12:26 PM |
| Becuase those functions are no longer stored in the _G table. |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 20 Apr 2015 12:27 PM |
Because Roblox's _G is not the same. You'd have to do
setfenv(1,{_G = getfenv(1)}) |
|
|
| Report Abuse |
|
|
|
| 20 Apr 2015 12:29 PM |
So, the wiki hasn't been updated in a while? http://wiki.roblox.com/index.php?title=Function_dump/Basic_functions#setfenv |
|
|
| Report Abuse |
|
|
|
| 20 Apr 2015 01:14 PM |
That example isn't necessarily wrong. Add this beforehand:
_G.print = print
That's like saying this example is incorrect:
for i = 1,10 do Door.CFrame = Door.CFrame = CFrame.new(Door.CFrame.X, i, Door.CFrame.Y) end
is incorrect, but in reality, Door just isn't defined :)
Back before, where the global environment was kept stored in _G like in actual Lua, that example did work. But after changes it no longer does. |
|
|
| Report Abuse |
|
|
|
| 20 Apr 2015 02:04 PM |
And if you ever do ask, this may be false, but when I think of it this way it makes more sense;
The number in the beginning of the function is the scope a blank scope returns the default scope (getfenv()) you can only reference a scope that the current scope is within(direct or indirect)
getfenv(1)["Hi"] = "HI!"
function foo() print(getfenv(1)["Hi"]) -- HI! print(getfenv(2)["Hi"]) -- nil setfenv(2,getfenv(1)) print(getfenv(2)["Hi"]) -- HI! end
foo()
HI! nil HI!
|
|
|
| Report Abuse |
|
|
|
| 20 Apr 2015 02:30 PM |
@Cre He understands setfenv, plus you're wrong :)
It's not the scope, it's the function.
do do getfenv(1) end end
getfenv doesn't return do's scope. -_- |
|
|
| Report Abuse |
|
|
| |
|
|
| 20 Apr 2015 06:43 PM |
| You don't know know what a scope is -_- |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 20 Apr 2015 06:45 PM |
| Environments are different than scopes Cres. |
|
|
| Report Abuse |
|
|
|
| 20 Apr 2015 07:35 PM |
Oh god, i confused them.
Anyway, you can probably understand what I meant if cnt could understand what I mistook scopes for. |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 21 Apr 2015 01:56 AM |
| The number is the stack level, not the scope. The difference is just basically where it was invoked from, I guess? New function call, new stack level. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 21 Apr 2015 05:40 PM |
| Hence the name "function" environments you blue nerd. |
|
|
| Report Abuse |
|
|