Jump to content




Event Handler that will not halt program


  • You cannot reply to this topic
2 replies to this topic

#1 snailworks

  • New Members
  • 2 posts

Posted 25 June 2013 - 09:40 AM

I am trying to have a program that will repeat a process based on a previously stored event value. For example, in a game if you moved a joystick to the left, the player would start and continue moving left until a new input was received which changed direction. Now the player would move in that direction until a new input, etc...

The problem is that the os.pullEvent() halts the program and waits for a new input. In the example below, there is only one Tick. When input is received, the program will Tock then Tick.
That is needed is a Tick, Tick, Tick... until an input is received. Then a Tock & repeat ticks, etc.

I read the forums and about Lua online. Is there an event handler that will not halt the program? Or - a completely different way of accomplishing this task?

--[[ Main loop ]]
while true do
--[[ Clear events ]]
sleep(0)
--[[ Repeat until button is pressed... ]]
repeat
  --[[ Do work... ]]
  repeatedTick()

  --[[ CAPTURE EVENT ]]
  event,side,x,y = os.pullEvent()

  --[[ Repeat... ]]
until event == "monitor_touch"
--[[ New work and settings based on event ]]
tock(x,y)

--[[ Repeat entire process... ]]
end


thanks-
-Snails

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 25 June 2013 - 10:03 AM

Split into new topic.

Use timer events. os.startTimer() is what you're looking for. Start one before the main loop, and trigger tick() in a timer event handling part of the loop. Start the next timer after tick runs.

#3 snailworks

  • New Members
  • 2 posts

Posted 25 June 2013 - 11:22 AM

Thanks - use the timer to trigger each cycle and move the program along...

This should work.

Thanks again-
-Snails

--[[ Main loop ]]
tick = os.startTimer(1)
while true do
--[[ Repeat until button is pressed... ]]
repeat
  --[[ CAPTURE EVENT ]]
  event,param,x,y = os.pullEvent()
  if event == "timer" and param == tick then
   --[[ Do work... ]]
   repeatedTick()
   tick = os.startTimer(1)
  end
  --[[ Repeat... ]]
until event == "monitor_touch"
--[[ New work and settings based on event ]]
tock(x,y)
--[[ Repeat entire process... ]]
end






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users