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

[1.41]Repeating Timers aka os.startTimerRepeating

Started by Namarius, 12 September 2012 - 04:05 PM
Namarius #1
Posted 12 September 2012 - 06:05 PM
Self repeating timers would be a big help in fight against timers and sleep.
Sleep kills every timer which runs out while sleep waits. This makes creating repeating timers a real pain.
A small demonstration:

timer = os.startTimer(1)
sleep(2) --timer gets killed
while true do
e,p = os.pullEvent()
print(e..":"..p)
if e=="key" and p==1 then return end
end

So in addition repeating timers should be started like os.startTimerRepeating or os.startTimer(length,repeating) and timers could be stopped using the timer table itself like timer:stop().
This shouldn't be too hard to implement in CC.
Cloudy #2
Posted 12 September 2012 - 07:00 PM
Just don't use sleep if you're waiting for a timer, or use sleep as the timer. It really isn't a big deal that sleep eats events if you work round it with trivial amounts of code.

You could also create your own replacement sleep which keeps any events.
Namarius #3
Posted 12 September 2012 - 07:51 PM
I know :P/>/> and my initial timer-sleep problem is more complex than my example above.
It would just be easier to code with repeating timers and much more real os like :)/>/>.

And APIs should keep work away from programmers and shouldn't react mysteriously which sleep does.

Anyway my respect for your work.
immibis #4
Posted 13 September 2012 - 07:41 AM
You'd still miss a timer event because you were sleeping.
Namarius #5
Posted 13 September 2012 - 01:59 PM
Yes but this is acceptable at this specific situation.

Besides of my suggestion I think coroutine.yield isn't a good way to give control back in the API, because this destroys the potential of user made coroutines and os.pullEvent(). I have an idea for a system without coroutine.yield at API level but don't know if its works, because I haven't seen the internals of CC.
Cloudy #6
Posted 13 September 2012 - 02:45 PM
No, it doesn't destroy any potential of user made coroutines - you can use coroutines however you like. Why would it stop you doing whatever you want with coroutines?
Namarius #7
Posted 13 September 2012 - 03:44 PM
Simple example of coroutine breaking:

co_a = coroutine.create(function ()
print("co_a start")
print("Do something")
sleep(1) --We want to sleep here and bam we yield out of this (doh!!)
print("Do something else")
print("co_a end")
end)
co_b = coroutine.create(function ()
print("co_b start")
print("Do something")
os.pullEvent("redstone") --We want to wait for redstone event and bam we yield out of this (doh!! again)
print("Do something else")
print("co_b end")
end)
--Testing co_a
print("Start coroutine co_a")
coroutine.resume(co_a)
print("Finish\n")
--Testing co_b
print("Start coroutine co_b")
coroutine.resume(co_:)/>/>
print("Finish")
This code will output this which is not something you would expect.

Start coroutine co_a
co_a start
Do something
Finish
Start coroutine co_b
co_b start
Do something
Finish

So timer and sleep cannot be used effectively in coroutines. Thats my point.
Cloudy #8
Posted 13 September 2012 - 04:17 PM
But that is how the system works. You need to take that into account when designing your system. I really don't see the issue? Everything that happens (all yielding etc) is done Lua side, in code you can read, and write yourself. It doesn't BREAK anything. You just need to take it into account.

FYI - coroutines are used natively in CC Lua side, and they don't break.
Namarius #9
Posted 14 September 2012 - 05:10 PM
Okay I understand it. It is just surprising to discover such behavior, because it's nowhere mentioned in documentation (as I know). Anyway I am experienced enough to get around of these small obstacles and i hope they will be away one day.

PS:
Take this as high level critics, because I would even use this mod if it broken like hell and nearly unusable which it isn't. It's work nearly perfect and perfection is hard to achieve.