简体   繁体   English

Lua 脚本在关键事件上重复不停止

[英]Lua script repeat on key event not stopping

Simple problem yet not easy for me to solve :简单的问题,但对我来说并不容易解决:

I have a loop and variables that change with OnEvent(event, arg) function我有一个循环和随 OnEvent(event, arg) 函数变化的变量

But while in the loop it does not detect change for exemple但是在循环中它不会检测到例如变化

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

Here cancel_action change is detected and work well in the OnEvent function but is never detect while in the loop in the test function.在这里, cancel_action更改被检测到并且在 OnEvent 函数中运行良好,但在测试函数的循环中永远不会被检测到。

So to summarize what I want is to use variables that hold states but thoses states are not updated correctly in my test function.所以总结一下我想要的是使用保存状态的变量,但这些状态在我的测试函数中没有正确更新。

What did I do wrong ?我做错了什么 ? Is it possible to detect change of variable while in the loop ?是否可以在循环中检测变量的变化? The event seems to trigger only after the loop is done该事件似乎仅在循环完成后才触发

You don't update cancel_action in your loop and while your code is busy running the loop no further events are being processed.您不会在循环中更新cancel_action并且当您的代码忙于运行循环时,不会处理进一步的事件。 So how is cancel_action supposed to change its value?那么cancel_action应该如何改变它的值呢?

Use IsMouseButtonPressed(2) to terminate your loop.使用IsMouseButtonPressed(2)终止循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM