A Roblox Community

A community of Robloxians who want to learn to script and build on Roblox Studio.
 
HomeFAQRegisterLog in
If you're a experienced coder make some tutorials! It would really help the site grow.
Make sure you read the rules(Which can be found by clicking here)
If you're a beginner at coding, try some tutorials.
We have many Moderators/Admins watching this site. Contact them with Questions.
Let us know what your favorite sport is. By clicking here to vote (Click here)
This site is becoming inactive. Lets make it active.
Log in
Username:
Password:
Log in automatically: 
:: I forgot my password
Top posters
blueymaddog
 
naknak
 
Supernapalm
 
m27frogy
 
slayer9365
 
myrco919
 
branefreez
 
ninga95
 
CloneTrooper787
 
raboy117
 
Top posting users this month
Latest topics
» Send me an Email
January 8th 2016, 10:14 am by naknak

» [v1.6.0.0] Lua Script Obfuscator [No Bytecode]
July 6th 2015, 7:38 pm by m27frogy

» New Site Possibly
July 6th 2015, 4:16 pm by m27frogy

» Ambassador!
April 15th 2015, 11:40 pm by naknak

» Boop - Tag
April 13th 2015, 9:46 pm by naknak

» Vip Class Script
April 13th 2015, 4:54 pm by naknak

» Who's active?!
April 13th 2015, 4:52 pm by naknak

» Where to go from here.
April 13th 2015, 4:51 pm by naknak

» Genesis Point
July 17th 2014, 7:04 pm by branefreez

» Reward System
July 17th 2014, 5:41 am by m27frogy

» Script Request
July 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 here
April 26th 2014, 4:01 pm by altshiftkey

» What's your favorite sport?
January 1st 2014, 2:13 pm by m27frogy

» FlashLight Script
January 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 Coding
October 26th 2013, 1:12 pm by pook03

» Simple Button
September 1st 2013, 6:19 pm by branefreez


Share | 
 

 Tutorial ~ getfenv()/setfenv()

View previous topic View next topic Go down 
AuthorMessage
Guest
Guest



PostSubject: 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 environment

The 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?
Back to top Go down
raboy117
Novice Scripter
Novice Scripter
avatar

Posts : 75
Join date : 2011-01-19
Age : 19
Location : Canada, PEI, Charlottetown

PostSubject: Re: Tutorial ~ getfenv()/setfenv()   January 23rd 2011, 11:51 pm

Nice!
Back to top Go down
myrco919
Intermediate Scripter
Intermediate Scripter
avatar

Posts : 190
Join date : 2011-01-21
Age : 19
Location : Holland

PostSubject: Re: Tutorial ~ getfenv()/setfenv()   January 24th 2011, 9:57 am

Great
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter


Posts : 393
Join date : 2011-01-17

PostSubject: Re: Tutorial ~ getfenv()/setfenv()   January 24th 2011, 8:45 pm

he makes things look easy. lol
Back to top Go down
http://hackthissite.org
blueymaddog
Administrator
Administrator
avatar

Posts : 1081
Join date : 2010-12-09
Age : 18

PostSubject: 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

_________________
blueymaddog
Administrator • Moderator • Expert Scripter
PM: blueymaddog
<+1 post. ujelly?>
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter


Posts : 393
Join date : 2011-01-17

PostSubject: 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?
Back to top Go down
http://hackthissite.org
blueymaddog
Administrator
Administrator
avatar

Posts : 1081
Join date : 2010-12-09
Age : 18

PostSubject: Re: Tutorial ~ getfenv()/setfenv()   January 27th 2011, 8:10 am

it might.

_________________
blueymaddog
Administrator • Moderator • Expert Scripter
PM: blueymaddog
<+1 post. ujelly?>
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter


Posts : 393
Join date : 2011-01-17

PostSubject: 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))

Back to top Go down
http://hackthissite.org
blueymaddog
Administrator
Administrator
avatar

Posts : 1081
Join date : 2010-12-09
Age : 18

PostSubject: 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.

_________________
blueymaddog
Administrator • Moderator • Expert Scripter
PM: blueymaddog
<+1 post. ujelly?>
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter


Posts : 393
Join date : 2011-01-17

PostSubject: 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
Back to top Go down
http://hackthissite.org
blueymaddog
Administrator
Administrator
avatar

Posts : 1081
Join date : 2010-12-09
Age : 18

PostSubject: Re: Tutorial ~ getfenv()/setfenv()   January 28th 2011, 4:48 am

Oh ya forgot about that. Try testing your script in the output.

_________________
blueymaddog
Administrator • Moderator • Expert Scripter
PM: blueymaddog
<+1 post. ujelly?>
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter


Posts : 393
Join date : 2011-01-17

PostSubject: Re: Tutorial ~ getfenv()/setfenv()   January 28th 2011, 10:18 pm

thats my problem. wheres the output?
Back to top Go down
http://hackthissite.org
blueymaddog
Administrator
Administrator
avatar

Posts : 1081
Join date : 2010-12-09
Age : 18

PostSubject: Re: Tutorial ~ getfenv()/setfenv()   January 29th 2011, 6:41 am

view>output in roblox studio

_________________
blueymaddog
Administrator • Moderator • Expert Scripter
PM: blueymaddog
<+1 post. ujelly?>
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter


Posts : 393
Join date : 2011-01-17

PostSubject: Re: Tutorial ~ getfenv()/setfenv()   January 29th 2011, 2:40 pm

k thx
Back to top Go down
http://hackthissite.org
ninga95
Administrator
Administrator
avatar

Posts : 122
Join date : 2010-07-30
Age : 21

PostSubject: 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.
Back to top Go down
blueymaddog
Administrator
Administrator
avatar

Posts : 1081
Join date : 2010-12-09
Age : 18

PostSubject: Re: Tutorial ~ getfenv()/setfenv()   March 20th 2011, 6:10 am

would this be a _G fixer:

script1:

getfenv(print).bai = "lol"

script2:
print(bai)

_________________
blueymaddog
Administrator • Moderator • Expert Scripter
PM: blueymaddog
<+1 post. ujelly?>
Back to top Go down
m27frogy
The Garbageman
avatar

Posts : 336
Join date : 2011-06-23

PostSubject: 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!
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter


Posts : 393
Join date : 2011-01-17

PostSubject: 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
Back to top Go down
http://hackthissite.org
m27frogy
The Garbageman
avatar

Posts : 336
Join date : 2011-06-23

PostSubject: 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".
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter


Posts : 393
Join date : 2011-01-17

PostSubject: 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
Back to top Go down
http://hackthissite.org
m27frogy
The Garbageman
avatar

Posts : 336
Join date : 2011-06-23

PostSubject: Re: Tutorial ~ getfenv()/setfenv()   June 30th 2011, 6:20 pm

Yes, that way of looking at it is still interesting.
Back to top Go down
blueymaddog
Administrator
Administrator
avatar

Posts : 1081
Join date : 2010-12-09
Age : 18

PostSubject: 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

_________________
blueymaddog
Administrator • Moderator • Expert Scripter
PM: blueymaddog
<+1 post. ujelly?>
Back to top Go down
Sponsored content




PostSubject: Re: Tutorial ~ getfenv()/setfenv()   

Back to top Go down
 
Tutorial ~ getfenv()/setfenv()
View previous topic View next topic Back to top 
Page 1 of 1
 Similar topics
-
» Great Tank and Weathering tutorial
» Power Sword Tutorial (with more to follow)
» Instant Mold Tutorial
» good c++ tutorial series
» After Effects Tutorial - Organic Cell Edges

Permissions in this forum:You cannot reply to topics in this forum
A Roblox Community :: Tutorials and Resources :: Advanced-
Jump to: