gpt4 book ai didi

Lua:如何使用 debug.getlocal

转载 作者:行者123 更新时间:2023-12-04 08:15:10 27 4
gpt4 key购买 nike

local function ThisIsAFunction()
local test1 = "im a local var"
local asd = "im also a local var"
end

我将如何使用 debug.getlocal 获取“ThisIsAFunction”中局部函数的值

最佳答案

像这样的东西应该可以工作:

local function printLocals()
local i = 1
while true do
local name, value = debug.getlocal(2, i)
if not name then break end
print(name, i, value)
i = i + 1
end
end

local function ThisIsAFunction()
local test1 = "im a local var"
local asd = "im also a local var"
printLocals()
end

ThisIsAFunction()

这应该打印:

test1   1   im a local var
asd 2 im also a local var

请注意,getlocal 只能获取已在堆栈上的函数/帧的值(这就是为什么您需要从要检查的函数中调用 printLocals ).

您可以使用 getinfo 检查函数(从外部),但是您需要执行要检查的函数,因此您需要显式调用 printLocals 或使用调试 Hook 来执行隐式调试 Hook 。

关于Lua:如何使用 debug.getlocal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65743935/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com