Timer Firing With Greater Delay Than Expected

That’s the best title I could come up with, I’ve got two game modes:

  • get as many points as you can in X amount of time
  • how fast can you get to X points

Both of these obviously need good time keeping, for the second I’m calling a function every tenth of a second to update the on screen timer.

GameTimer = timer.performWithDelay(100, UpdateTimer, 0)   

The timer seemed to be running slowly so I added some debug code to see how many miliiseconds there was really between each time the function was called.

time\_debug = system.getTimer()  
GameTimer = timer.performWithDelay(100, UpdateTimer, 0)   
function UpdateTimer()  
 local time\_out = tostring(system.getTimer() - time\_debug)  
 print('game time ' .. time\_out)  
 time\_debug = system.getTimer()  
end  

The actual delay comes back as 112 or 132 depending if I’m running 60 or 30 fps. I appreciate the timers aren’t guaranteed but is there a good solution to this?

  • I could use the actual delay to update the screen time (and game time) rather than relying on the time to fire with the exact delay.
  • Can I shift it off onto a separate thread?
  • Is there a better way of doing this rather than using the timer functions? [import]uid: 68937 topic_id: 11818 reply_id: 311818[/import]

When I say thread, I mean coroutine we’re in lua here.

Anyway, this might be a simulator thing, although it fluctuates on the device (between 99 and 109 depending on the load) it’s much more consistent.

Still, if there’s a better way I’m open to suggestions. [import]uid: 68937 topic_id: 11818 reply_id: 43062[/import]

Try keeping track of time yourself with a variable. And maybe use an enterFrame event to constantly update the time on screen. That’s what I’m doing and it’s accurate enough for my game.

I believe the timers within Corona aren’t “high performance” as an Ansca staff member posted this was the case. In other words, there is some “drift” in the timers as you have found by checking the ms elapsed. [import]uid: 26769 topic_id: 11818 reply_id: 43080[/import]