Anyone knows what this 2 functions are for?
myVariable = "Hello, environments" -- Note: a global variable (non-local)
local env = getfenv()
print(env["myVariable"]) --> Hello, environments
It’s like a _G without calling _G?
Anyone knows what this 2 functions are for?
myVariable = "Hello, environments" -- Note: a global variable (non-local)
local env = getfenv()
print(env["myVariable"]) --> Hello, environments
It’s like a _G without calling _G?
Manipulation of the stack is one of those things I Believe is generally undertaken by advanced programmers who understand everything else and are looking for something new to learn.
You’ve answered your question. getfenv()
returns a table of the global environment of your script.
When you do
a = 5
print(a)
you are actually doing
getfenv().a = 5
print(getfenv().a)
Using getfenv()
is discouraged though. It disables some of Luau’s optimizations.
Hi,
Each and every function in LUA has an “environment” what these functions do is get their global environment.
Examples of a global variable;
Non-Global variables;
local Test = "Test"
To get the environment of your current script do this
GlobalVariable = "Hiiii"
local Env = getfenv()
print(Env.GlobalVariable)
Expected output
Hiiii
setenv() just sets the environment