【问题标题】:Lua script repeat on key event not stoppingLua脚本在关键事件上重复不停止
【发布时间】:2021-12-09 01:20:39
【问题描述】:

简单的问题,我却不容易解决:

我有一个循环和随 OnEvent(event, arg) 函数变化的变量

但是在循环中它不会检测到例如变化

local cancel_action = false

function OnEvent(event, arg)
  if (event == "MOUSE_BUTTON_PRESSED" and arg == 2) then
    cancel_action = not cancel_action
    OutputLogMessage("DETECT  cancel_action :")
    OutputLogMessage(tostring(cancel_action))
  end

  if (event == "MOUSE_BUTTON_PRESSED" and arg == 3) then
    test()
  end
end

function test()
  count_ = 0
  repeat
     count_ = count_ + 1
     OutputLogMessage("cancel_action ?")
     OutputLogMessage(tostring(cancel_action))
     if ( cancel_action ) then
       OutputLogMessage("do something and stop")
       cancel_action = not cancel_action
       break
     else
        OutputLogMessage("do something else and loop again")
     end
  until count_ > 10
end

这里 cancel_action 检测到更改并在 OnEvent 函数中正常工作,但在测试函数的循环中从未检测到。

所以总结一下我想要的是使用保存状态但这些状态在我的测试函数中没有正确更新的变量。

我做错了什么?在循环中是否可以检测到变量的变化?该事件似乎只有在循环完成后才会触发

【问题讨论】:

标签: lua logitech-gaming-software


【解决方案1】:

您不会在循环中更新cancel_action,并且当您的代码忙于运行循环时,不会处理更多事件。那么cancel_action应该如何改变它的值呢?

使用IsMouseButtonPressed(2) 终止循环。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 2021-04-21
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    相关资源
    最近更新 更多