Top posting users this month | |
Latest topics | » Where to go from here.September 14th 2020, 1:20 pm by MrNicNac» Send me an EmailSeptember 14th 2020, 1:16 pm by MrNicNac» [v1.6.0.0] Lua Script Obfuscator [No Bytecode]July 6th 2015, 7:38 pm by m27frogy» New Site PossiblyJuly 6th 2015, 4:16 pm by m27frogy» Ambassador!April 15th 2015, 11:40 pm by naknak» Boop - TagApril 13th 2015, 9:46 pm by naknak» Vip Class ScriptApril 13th 2015, 4:54 pm by naknak» Who's active?!April 13th 2015, 4:52 pm by naknak» Genesis PointJuly 17th 2014, 7:04 pm by branefreez» Reward SystemJuly 17th 2014, 5:41 am by m27frogy» Script RequestJuly 10th 2014, 11:43 am by naknak» local scripts?July 10th 2014, 11:39 am by naknak» Project: Reconstruction [Died]July 10th 2014, 11:36 am by naknak» Hi. I am new hereApril 26th 2014, 4:01 pm by altshiftkey» What's your favorite sport?January 1st 2014, 2:13 pm by m27frogy» FlashLight ScriptJanuary 1st 2014, 2:11 pm by m27frogy» Gun Making! [READ DESC]January 1st 2014, 2:10 pm by m27frogy» Hi, I am new here!November 26th 2013, 3:33 pm by Keanu73» Improve CodingOctober 26th 2013, 1:12 pm by pook03» Simple ButtonSeptember 1st 2013, 6:19 pm by branefreez |
|
| Tutorial ~ getfenv()/setfenv() | |
| | Author | Message |
---|
Guest Guest
| Subject: Tutorial ~ getfenv()/setfenv() January 23rd 2011, 11:49 pm | |
| This is a tutorial about setfenv() and getfenv(). Since it's in the advanced section, you should understand functions, tables, variables, etc. Knowing what pointer variables are is good too, but since we don't have those in Lua, I'll cover those too. Now, you may be wondering, "What do setfenv and getfenv mean? What the heck is a fenv?" Well, "fenv" is short for "function environment". Without overcomplicating things, the function environment is all the variables that you have access to in a certain spot. variables and the function environmentThe function environment is like a table, and when you set a variable, you're setting a new index (the variable) to a new value (the value it is being assigned to). - Code:
-
Pie = "Pie"
In the above example, you are setting a new index of Pie in the function environment, and giving it the value of "Pie". setfenv()setfenv is a function that you use to change the function environment to a new function environment. The first argument is where to change the function environment. You can give it an actual function to change the environment of, or you can tell it how many stack levels to go down. Zero would be telling it to manipulate the function environment that the setfenv() is inside, one would tell it to change the function environment of where the current function was called from, etc. The second argument is a table that will become the new function environment. The indices you set are variables that can be accessed in the function, and the values are the values those variables are assigned to. - Code:
-
setfenv(1, {Stuff = "Hello, World!"}) print(Stuff)
Now, if you were to test the above code, it would give you output something like this: Script:2:Attempt to call global 'print' (a nil value)You may be wondering why that is. print is a global function, right? Well, when you use setfenv(), you completely reset the function environment. Global variables need to be re-defined. - Code:
-
setfenv(1, {Stuff = "Hello, World!", print = print}) print(Stuff) -->Hello, World!
getfenv()getfenv is another function, but before I explain too much about it, I'm going to explain a little bit about pointer variables. Pointer variables are variables that, instead of just having the same value as another variable, actually use the same data as another variable. For example: - Code:
-
a = 5 b = a a = 6 print(a,b) -->6 5
See how b didn't changed? It's because b simply got a clone of the data that a contained, instead of actually having the same data. Pointer variables use the same exact data. Let's say that I could create a pointer variable by adding an asterisk after a variable. Here's how it would work: - Code:
-
Var1 = 5 Var2* = Var1 -- this is now a pointer variable :O Var1 = 6 print(Var1, Var2) -->6 (numbers/letters)
But why did it print like that? That's where the data is stored. The pointer variable doesn't refer to the data itself, it refers to the spot where the data is. We will use the ampersand (&) to refer to the data itself. - Code:
-
Var1 = 5 Var2* = Var1 Var1 = 6 print(Var1, &Var2) -->6 6
See? And since pointers point the the space where the data is stored, they can change the data too by changing what is in that spot: - Code:
-
Var1 = 5 Var2* = Var1 Var2 = 6 print(Var1) -->6
See how when we edited Var2, Var1 changed too? That's the usefulness of a pointer variable. Sadly, we can't use any type of pointer variable in Lua, or RBX.Lua. Now back to getfenv. The reason I had to explain pointers to you is because getfenv returns something that acts like a pointer variable. When you use getfenv(), it returns a table of the function environment. It only takes one argument, and it acts just like a pointer variable, because when you manipulate it, you manipulate the function environment that it refers to. Note: When using getfenv(), leaving the parenthesis empty is the same as putting a zero in them. It takes the function environment of the current function. - Code:
-
pie = getfenv() pie.Caik = "Hello!" print(Caik) -->Hello!
See? We added an index to the function environment that we were currently in, and then we were able to use it. A shorter way to do this is: - Code:
-
getfenv().Caik = "Hi!" -- no just shortening the word doesn't make it a shorter script print(Caik) -->Hi!
And another example of each: - Code:
-
function Pie() print(lolwut) end
function Caik() print(lolwut) end
getfenv(Pie).lolwut = "Hola" setfenv(Caik, {print = print, lolwut = "Bonjour"}) Pie() -->Hola Caik() -->Bonjour
See? Simple as that. FEEDBACK PL0X? |
| | | raboy117 Novice Scripter
Posts : 75 Join date : 2011-01-19 Age : 27 Location : Canada, PEI, Charlottetown
| Subject: Re: Tutorial ~ getfenv()/setfenv() January 23rd 2011, 11:51 pm | |
| | |
| | | myrco919 Intermediate Scripter
Posts : 190 Join date : 2011-01-21 Age : 26 Location : Holland
| Subject: Re: Tutorial ~ getfenv()/setfenv() January 24th 2011, 9:57 am | |
| | |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: Re: Tutorial ~ getfenv()/setfenv() January 24th 2011, 8:45 pm | |
| he makes things look easy. lol | |
| | | blueymaddog Administrator
Posts : 1081 Join date : 2010-12-09 Age : 26
| Subject: Re: Tutorial ~ getfenv()/setfenv() January 26th 2011, 8:48 am | |
| Great tutorial. But would this work?
Script 1: getfenv().hi = "lol" Script 2: print(hi) >lol
| |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: Re: Tutorial ~ getfenv()/setfenv() January 27th 2011, 3:48 am | |
| that might work... might this is my question - Code:
-
setmetatable(tab = {}, {__metatable = "access denied"}) print(getfenv(getmetatable(tab).__metatable = nil)) --> no complaints? would that work? | |
| | | blueymaddog Administrator
Posts : 1081 Join date : 2010-12-09 Age : 26
| Subject: Re: Tutorial ~ getfenv()/setfenv() January 27th 2011, 8:10 am | |
| | |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: Re: Tutorial ~ getfenv()/setfenv() January 27th 2011, 8:29 pm | |
| wait no i meant this - Code:
-
print(getfenv(setmetatable(tab = {}, {__metatable = "access denied"}).__metatable = nil)) | |
| | | blueymaddog Administrator
Posts : 1081 Join date : 2010-12-09 Age : 26
| Subject: Re: Tutorial ~ getfenv()/setfenv() January 27th 2011, 9:00 pm | |
| it won't work, you can't use more than one '=' in one line except for '=='. the parameter for getfenv has to be a number, blank or function. not a table. | |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: Re: Tutorial ~ getfenv()/setfenv() January 27th 2011, 9:18 pm | |
| first of all, you can use more than one line in tables - Code:
-
tab = {a = 1, b = 2, c = 3} and second of all, setmetatable() is a function | |
| | | blueymaddog Administrator
Posts : 1081 Join date : 2010-12-09 Age : 26
| Subject: Re: Tutorial ~ getfenv()/setfenv() January 28th 2011, 4:48 am | |
| Oh ya forgot about that. Try testing your script in the output. | |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: Re: Tutorial ~ getfenv()/setfenv() January 28th 2011, 10:18 pm | |
| thats my problem. wheres the output? | |
| | | blueymaddog Administrator
Posts : 1081 Join date : 2010-12-09 Age : 26
| Subject: Re: Tutorial ~ getfenv()/setfenv() January 29th 2011, 6:41 am | |
| view>output in roblox studio | |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: Re: Tutorial ~ getfenv()/setfenv() January 29th 2011, 2:40 pm | |
| | |
| | | ninga95 Administrator
Posts : 122 Join date : 2010-07-30 Age : 29
| Subject: Re: Tutorial ~ getfenv()/setfenv() March 6th 2011, 11:13 pm | |
| Heres an observation i made earlier about this for people who are knew to this ... To always remember what Setfenv() and Getfenv() are you remember it like this.
Set in Setfenv() is Set, f in Setfenv() is function, env in Setfenv() is Environment, and put it all together and it means Setfenv() Set's the function's environment,
Get in Getfenv() is Get, f in Getfenv is function, env in Getfenv() is environment, put it all together, and it means Getfenv() Get's a function's environment.
Nice little observation i made earlier today lol, but this is a good way to remember what Setfenv() and Getfenv() does. | |
| | | blueymaddog Administrator
Posts : 1081 Join date : 2010-12-09 Age : 26
| Subject: Re: Tutorial ~ getfenv()/setfenv() March 20th 2011, 6:10 am | |
| would this be a _G fixer:
script1:
getfenv(print).bai = "lol"
script2: print(bai) | |
| | | m27frogy The Garbageman
Posts : 336 Join date : 2011-06-23
| Subject: Re: Tutorial ~ getfenv()/setfenv() June 30th 2011, 11:53 am | |
| Great job NotAPotato! - Quote :
- Heres an observation i made earlier about this for people who are knew to this ... To always remember what Setfenv() and Getfenv() are you remember it like this.
Set in Setfenv() is Set, f in Setfenv() is function, env in Setfenv() is Environment, and put it all together and it means Setfenv() Set's the function's environment,
Get in Getfenv() is Get, f in Getfenv is function, env in Getfenv() is environment, put it all together, and it means Getfenv() Get's a function's environment.
Nice little observation i made earlier today lol, but this is a good way to remember what Setfenv() and Getfenv() does. Not a bad way of looking at it ninga! | |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: Re: Tutorial ~ getfenv()/setfenv() June 30th 2011, 5:20 pm | |
| erm. thats why the creator of the lua gave this function its name
setfenv: set function environment getfenv: get function environment | |
| | | m27frogy The Garbageman
Posts : 336 Join date : 2011-06-23
| Subject: Re: Tutorial ~ getfenv()/setfenv() June 30th 2011, 5:46 pm | |
| I knew that Supernapalm. That still doesn't effect my actual statement, "way of looking at it". | |
| | | Supernapalm Expert Scripter
Posts : 393 Join date : 2011-01-17
| Subject: Re: Tutorial ~ getfenv()/setfenv() June 30th 2011, 5:51 pm | |
| actaully, its pretty much the only way of looking at it, cuz the lua creator said so | |
| | | m27frogy The Garbageman
Posts : 336 Join date : 2011-06-23
| Subject: Re: Tutorial ~ getfenv()/setfenv() June 30th 2011, 6:20 pm | |
| Yes, that way of looking at it is still interesting. | |
| | | blueymaddog Administrator
Posts : 1081 Join date : 2010-12-09 Age : 26
| Subject: Re: Tutorial ~ getfenv()/setfenv() February 11th 2012, 7:54 am | |
| lol it's the way I looped at it before i saw this tutorial. I think it's the only way of seeing it. :P | |
| | | Sponsored content
| Subject: Re: Tutorial ~ getfenv()/setfenv() | |
| |
| | | | Tutorial ~ getfenv()/setfenv() | |
|
Similar topics | |
|
| Permissions in this forum: | You cannot reply to topics in this forum
| |
| |
| Who is online? | In total there is 1 user online :: 0 Registered, 0 Hidden and 1 Guest
None
Most users ever online was 136 on October 21st 2024, 12:16 pm
|
| Please tell us what time zone you are in here
Got a really good idea? Post it in projects and it might show up here! |
|