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.
How To Make Coroutines Run Together With Events?
Started by exploder, Aug 05 2013 09:17 AM
5 replies to this topic
#1
Posted 05 August 2013 - 09:17 AM
#2
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.
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
Posted 05 August 2013 - 10:18 AM
You have to make a loop in the displayTime function too I guess
#4
Posted 05 August 2013 - 10:50 AM
GopherAtl, 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.
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?
svdragster, 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
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
Posted 05 August 2013 - 11:39 AM
MysticT, 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











