i want to know what getfenv really does, I know that I has stack thingies but idk what they do
what is getfenv(1), getfenv(2) and getfenv(3) and if there are more tell me what they do
pls explain ok thx
i want to know what getfenv really does, I know that I has stack thingies but idk what they do
what is getfenv(1), getfenv(2) and getfenv(3) and if there are more tell me what they do
pls explain ok thx
Its used for accessing the global environment of other functions in a callstack (getfenv)
It can also be used for setting the global environment of other functions (setfenv)
local function c()
print(getfenv(2).b) --> 5
end
local function a()
getfenv().b = 5
c()
end
a()
HOWEVER, It should not be used in Luau as it is both deprecated and messes with a lot of it’s optimisations
Functions can be queried by name, they do not need to occupy a level of the call stack.
local f
do
v = "Hello world!"
f = function()
end
end
local e = getfenv(f)
print(e.v) --'Hello world!'
As for the original post getfenv
is an abbreviation of ‘get function environment’ and setfenv
is an abbreviation of ‘set function environment’. Documentation regarding both functions can be found here.
but what does the 2 mean, or any other numbers
The number refers to how far up the callstack you want to get to
Assume the following callstack
getEnvironment
deleteAllNeighbours
recheckZone
update
If I was to call getfenv(2)
from getEnvironment
, it would then pull global variables in deleteAllNeighbours
. Equally, if I set it to 3 in the same function, it would then pull from recheckZone.