Jump to content




How To Make Coroutines Run Together With Events?


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

#1 exploder

  • Members
  • 69 posts
  • LocationLatvia

Posted 05 August 2013 - 09:17 AM

Hello. I have a problem that I can't solve. I'm trying to make a clock that would refresh every second and print time on screen but when I make coroutine that I put inside my main loop that has an os.pullEvent() then the function won't run every second but only once and that's it. Could you please tell what is the best way to resolve this problem?

Pastebin link: http://pastebin.com/YN6pE8gW -- The main loop is at line 122.

Thanks.

#2 GopherAtl

  • Members
  • 888 posts

Posted 05 August 2013 - 10:13 AM

coroutines aren't functions.

displayTime displays the time, sleeps, and exits. Once it exits, it's coroutine is dead, and it will not run anymore, no matter how many times you try to resume() it.

Coroutines, just like programs, need to contain a loop if they're going to keep running indefinitely.

#3 svdragster

  • Members
  • 222 posts
  • LocationGermany

Posted 05 August 2013 - 10:18 AM

You have to make a loop in the displayTime function too I guess

#4 exploder

  • Members
  • 69 posts
  • LocationLatvia

Posted 05 August 2013 - 10:50 AM

View PostGopherAtl, on 05 August 2013 - 10:13 AM, said:

coroutines aren't functions.

displayTime displays the time, sleeps, and exits. Once it exits, it's coroutine is dead, and it will not run anymore, no matter how many times you try to resume() it.

Coroutines, just like programs, need to contain a loop if they're going to keep running indefinitely.

Then what would be the best way to make that function run every second without interrupting events?

View Postsvdragster, on 05 August 2013 - 10:18 AM, said:

You have to make a loop in the displayTime function too I guess

Well, if I put loop inside there, makes no diffirence.

#5 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 05 August 2013 - 11:17 AM

Use a timer instead of sleep and handle the "timer" event in the main loop. Something like this:
local clockTimer = os.startTimer(1) -- start a timer for 1 second

-- Main loop
while true do
  local evt, p1, p2, p3, p4, p5 = os.pullEvent()
  if evt == "timer" then
    if p1 == clockTimer then
      displayTime() -- redraw the clock
      clockTimer = os.startTimer(1) -- restart the timer
    end
  elseif evt == "some other event" then
    -- handle the event
  end
end


#6 exploder

  • Members
  • 69 posts
  • LocationLatvia

Posted 05 August 2013 - 11:39 AM

View PostMysticT, on 05 August 2013 - 11:17 AM, said:

Use a timer instead of sleep and handle the "timer" event in the main loop. Something like this:
local clockTimer = os.startTimer(1) -- start a timer for 1 second

-- Main loop
while true do
  local evt, p1, p2, p3, p4, p5 = os.pullEvent()
  if evt == "timer" then
	if p1 == clockTimer then
	  displayTime() -- redraw the clock
	  clockTimer = os.startTimer(1) -- restart the timer
	end
  elseif evt == "some other event" then
	-- handle the event
  end
end

When I tried that last time it didn't work (it wasn't reading mouse x and y positions, etc) but now it worked without errors. Thanks.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users