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
 

Re: getfenv()

Previous Thread :: Next Thread 
izzatnasruna is not online. izzatnasruna
Joined: 09 Jun 2009
Total Posts: 1927
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
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
03 Jan 2016 07:27 PM
hi = 5

print(getfenv().hi)



I has a few uses, but not many.
Report Abuse
izzatnasruna is not online. izzatnasruna
Joined: 09 Jun 2009
Total Posts: 1927
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
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
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 is not online. 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
izzatnasruna is not online. izzatnasruna
Joined: 09 Jun 2009
Total Posts: 1927
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 is not online. 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
izzatnasruna is not online. izzatnasruna
Joined: 09 Jun 2009
Total Posts: 1927
03 Jan 2016 08:14 PM
getfenv()returns a table?
Report Abuse
QuintinityZero is not online. QuintinityZero
Joined: 01 Oct 2008
Total Posts: 10618
03 Jan 2016 08:34 PM
Yes, it returns a table.


Report Abuse
ForeverDev is not online. ForeverDev
Joined: 04 Oct 2008
Total Posts: 13300
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
izzatnasruna is not online. izzatnasruna
Joined: 09 Jun 2009
Total Posts: 1927
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
QuintinityZero is not online. QuintinityZero
Joined: 01 Oct 2008
Total Posts: 10618
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 is not online. 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
izzatnasruna is not online. izzatnasruna
Joined: 09 Jun 2009
Total Posts: 1927
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
wonderful72pike is not online. wonderful72pike
Joined: 13 Jul 2010
Total Posts: 7009
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 is not online. 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
wonderful72pike is not online. wonderful72pike
Joined: 13 Jul 2010
Total Posts: 7009
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 is not online. 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
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