Hylith Rage
Hylith Rage

Reputation: 1

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

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.

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

Upvotes: 0

Views: 780

Answers (1)

Piglet
Piglet

Reputation: 28994

You don't update cancel_action in your loop and while your code is busy running the loop no further events are being processed. So how is cancel_action supposed to change its value?

Use IsMouseButtonPressed(2) to terminate your loop.

Upvotes: 0

Related Questions