Clarification on timers

I haven’t seen this addressed in the documentation or in other forum posts – it appears that timer.performWithDelay has an upper limit on frequency (or lower limit on delay) which corresponds to the current frame rate, is this correct? And if so, is this intentional?

For example:

timer.performWithDelay(500, t, 0)

Appears to “fire” the timer event roughly every 500 ms indefinitely, as expected.

However:

timer.performWithDelay(5, t, 0)

Will not “fire” the timer event every 5 ms as one might expect, but rather every 16 ms (at 60fps) or 33ms (at 30fps).

Is there some other built-in mechanism that I might use to get a periodic function call more often than once per frame update? I’ve already developed an alternative mechanism myself but it’s kind of a kludge and I’d prefer a cleaner solution if possible. :slight_smile:

Thanks in advance for your help… [import]uid: 8836 topic_id: 2045 reply_id: 302045[/import]

Just because I am curois, why do you want something to be called faster than the possible update/draw rate of the engine? [import]uid: 5712 topic_id: 2045 reply_id: 5986[/import]

My question is sort of academic at this point, as I am brand new to Corona/Lua and I’m just trying to understand how it works.

However, I discovered this behavior in the context of exploring multithreading and attempting to develop a general-purpose coroutine dispatch mechanism.

Let’s say that I have some sort of lengthy “background” task that I wish to perform, without interfering with the primary game/UI cycle. So, I can write a coroutine which advances this background task in very small chunks and then yields, so that no single “slice” of the routine would take longer than say 5 ms.

If I dispatch this coroutine once per frame update at 30fps, it could potentially take 6x longer to complete the background task than it might need to (i.e., I theoretically have time to call 6 slices @ 5ms each in the 33ms between frames).

No question, that scenario is not going to arise in every project one might develop in Corona, but as I said I’m just looking for an understanding of how the system works so that if/when such a need arises I’ll know what I can and can not do with the tool. So far, I’m favorably impressed with Corona/Lua, but of course there are some features that I’m accustomed to with other tools that don’t seem to be available here – but since as I say I’m brand new to the system, it may well be that I just don’t know how to do what I’m trying to do. :slight_smile: [import]uid: 8836 topic_id: 2045 reply_id: 5993[/import]

First, you can tell Corona to update at 60 FPS That gives you more time to calculate stuff.

Second, there is no multithreading in LUA. Coroutines are like functions. The difference is that you can jump out of a coroutine when you want and go back to the point where you jumped out later on.

And to my knowledge, Corona also doesn’t support any kind of multithreading regarding event listeners.
[import]uid: 5712 topic_id: 2045 reply_id: 5999[/import]

Thanks Mike, I understand the FPS adjustment, but I don’t necessarily want/need the overhead associated with updating the screen more often.

I understand how coroutines work, as well as the multithreading limitations of LUA (technically, I believe coroutines are a form of multithreading, just not *parallel* multithreading).

So in general, what I’m playing with is a coroutine dispatcher so that I can arbitrarily activate any number of “background threads”, and the dispatcher will handle the repeated calls to “resume” each thread as needed until they are complete. When I want to perform some background task, I can simply register the function to perform with this dispatcher, and the dispatcher can handle the rest.

The dispatcher mechanism itself is fairly trivial, and I’ve got one built already. The trouble is in how often the dispatcher itself has a chance to iterate through any active coroutines to give each coroutine a chance to execute.

The dispatch iteration itself needs to be continuous but interruptable, in order to process the coroutines as quickly as possible but still allow other code (and screen updates, etc.) to execute as needed. A tight loop won’t work, as no other code would get a chance to run.

Which is why I was trying a timer event, in an attempt to have my coroutine iteration execute continuously but allow as much time as possible to run the active coroutines. I figured if I ask for the dispatcher to fire every millisecond (not expecting to actually achieve that, of course, but to get the events as quickly as possible) then I could run more-or-less continuously while still allowing screen updates and other events to process as needed. Not necessarily an optimal solution, but theoretically it should work as a starting point for development at least.

Given my observations about how the timer mechanism seems to be working, I’ve already tweaked my dispatcher to loop through my active coroutines as much as possible, up to a set number of elapsed milliseconds, at which point the dispatcher stops until the next time it’s called. I could then trigger my dispatcher with either a timer or at the exit of each frame (which seems like a better solution than the timer). As I mentioned in my original post, that approach seems to work, but it’s a bit of a kludge to me because I’d have to specify that I want coroutines to execute for X milliseconds on each frame, when what I really want is for coroutines to be able to execute as much as they need to between frames. [import]uid: 8836 topic_id: 2045 reply_id: 6011[/import]

I’m in agreement with Icarus here…there are lots of tasks (social API access, multi-player synchronization, db synchronization, etc) that we should be able to (partially) perform between screen updates. What’s the official position from Ansca on allowing timers to fire more frequently than the device frame-rate?? [import]uid: 6175 topic_id: 2045 reply_id: 7085[/import]

I filed case 1346 to track this question/issue. I’ll have someone from the engineering team chime in on the larger question.

thanks,
Tim [import]uid: 8196 topic_id: 2045 reply_id: 7115[/import]

Under the hood, timer.performWithDelay() is implemented in Lua using enterFrame events, so you cannot get called faster than the screen updates.

As in all cooperative multitasking environments (e.g. coroutines, Mac OS Classic, etc), task management is more manual. [import]uid: 26 topic_id: 2045 reply_id: 25787[/import]