Lua调试 - 检测变量的值何时更改 [英] Lua Debugging - Detect when the value of a variable changes

查看:757
本文介绍了Lua调试 - 检测变量的值何时更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用lua调试库检测变量的值何时更改。
类似一个回调函数,它将给出一些细节,比如改变值的功能,以前的值等。
这样的事情可能吗?



我读了关于钩子,但我不确定钩子可以设置为变量。

解决方案

如果你不介意使用调试器,那么一些调试器可以让你设置Watch表达式,表达式中的条件是真实的。我将在 MobDebug (它使用lua调试库,但没有直接的方式来检测一个变量,据我所知)。



让我们说一个脚本 start.lua 像下面一样,想要检测哪里 foo 获取值2:

  print(Start)
local foo = 0
for i = 1,3 do
local function bar()
print(in bar)
end
foo = i
print(Loop)
bar()
end
print(End)




  1. 下载 mobdebug.lua 并使其可用于您的脚本(最简单的方法是将其放入脚本中的文件夹中)。

  2. 启动服务器使用 lua -erequire('mobdebug')。listen()命令

  3. 启动客户端使用 lua - erequire('mobdebug')。loop()命令。

  4. 您将在服务器窗口中看到提示:'>'。键入 load start.lua 加载脚本。

  5. 键入 step 然后再次步骤。您将看到暂停在文件start.lua第3行。

  6. 让我们看看 foo 的价值是什么。键入 eval foo ,您应该会看到0。

  7. 现在我们可以设置我们的手表。键入 setw foo == 2 。您可以在setw命令之后指定任何Lua表达式;

  8. 使用run命令继续执行脚本。

  9. 现在观看,它将显示如下消息:暂停在文件start.lua第8行(表达式1:[foo == 2])。这意味着以前的表达式将$ code> foo 的值更改为2,执行在第8行停止。然后可以检查脚本和当前值(可以使用 eval和exec命令来运行任何Lua代码以在脚本环境中进行评估)来查找触发更改的内容。

这种方法的好处是,您不仅限于监视表值,还可以指定任何表达式。主要的缺点是您的脚本在调试器下运行,并且表达式在每个步骤后进行评估,这可能会很慢。


Is it possible to detect when the value of a variable has changed using the lua debug library. Something like A callback function which would give details like the function in which the value was changed, previous value, etc. Is such a thing possible?

I read about hooks, but I'm not sure hooks can be set to variables.

解决方案

If you don't mind using a debugger, then some debuggers allow you to set Watch expressions, which will be triggered when the condition in the expression is true. I'll show how this can be done in MobDebug (it is using lua debug library, but there is no direct way to detect a variable change as far as I know).

Let say we have a script start.lua like the one below and want to detect where foo gets value 2:

print("Start")
local foo = 0
for i = 1, 3 do
  local function bar()
    print("In bar")
  end
  foo = i
  print("Loop")
  bar()
end
print("End")

  1. Download mobdebug.lua and make it available to your scripts (the simplest way is to put it into the folder with your scripts).
  2. Start the server using lua -e "require('mobdebug').listen()" command.
  3. Start the client using lua -e "require('mobdebug').loop()" command.
  4. You will see the prompt in the server window: '>'. Type load start.lua to load the script.
  5. Type step and then step again. You will see "Paused at file start.lua line 3".
  6. Let's see what the value of foo is. Type eval foo and you should see 0.
  7. Now we can set up our watch. Type setw foo == 2. You can specify any Lua expression after setw command; the execution of your script will be stopped when the condition is evaluated as true.
  8. Continue execution of the script using "run" command.
  9. The watch now fires, which will show you the message like: "Paused at file start.lua line 8 (watch expression 1: [foo == 2])". This means that the previous expression changed the value of foo to 2 and the execution is stopped at line 8. You can then inspect your script and the current values (you can use "eval" and "exec" commands to run any Lua code to be evaluated in your script environment) to find what triggered the change.

The benefit of this approach is that you are not limited to monitoring table values and can specify any expression. The main disadvantage is that your script runs under a debugger and the expression is evaluated after each step, which may get really slow.

这篇关于Lua调试 - 检测变量的值何时更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆