This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
snailworks's profile picture

Event Handler that will not halt program

Started by snailworks, 25 June 2013 - 08:40 AM
snailworks #1
Posted 25 June 2013 - 10: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
Lyqyd #2
Posted 25 June 2013 - 11: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.
snailworks #3
Posted 25 June 2013 - 12:22 PM
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