Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Better way to get stack level?

Asked by
LuaQuest 450 Moderation Voter
8 years ago

I'm trying to get the stack level of a certain scope, and kinda confused on how i can successfully do this. Here's what I've tried so far:

local function GetStackLevel(Env)
    local stack=1
    local current=getfenv(0)
    setfenv(1,Env)
    while getfenv(stack)~=current do
        stack=stack+1
    end
    print(stack)
end


setfenv(1,{x=1,print=print,getfenv=getfenv})
GetStackLevel(getfenv())
print(getfenv(2))

However, the GetStackLevel function returns "3", while the maximum level I can get with getfenv in that environment is 2. Can anyone help?

1 answer

Log in to vote
0
Answered by 8 years ago

I may be wrong, but shouldn't you start at "stack = 0"? And I don't see getfenv(2) working outside of functions.

Ad

Answer this question