generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Setfenv

Previous Thread :: Next Thread 
epicbreaker is not online. epicbreaker
Joined: 23 Apr 2011
Total Posts: 2791
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 is not online. 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 is not online. 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
epicbreaker is not online. epicbreaker
Joined: 23 Apr 2011
Total Posts: 2791
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
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
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
CrescentJade is not online. CrescentJade
Joined: 07 Jul 2010
Total Posts: 5494
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
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
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
CrescentJade is not online. CrescentJade
Joined: 07 Jul 2010
Total Posts: 5494
20 Apr 2015 05:43 PM
Do doesnt have a scope.
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
20 Apr 2015 06:43 PM
You don't know know what a scope is -_-
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
20 Apr 2015 06:45 PM
Environments are different than scopes Cres.
Report Abuse
CrescentJade is not online. CrescentJade
Joined: 07 Jul 2010
Total Posts: 5494
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 is not online. 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 is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
21 Apr 2015 05:40 PM
Hence the name "function" environments you blue nerd.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image