Delay vs Timer

BlitzPlus Forums/BlitzPlus Beginners Area/Delay vs Timer

laughing_man(Posted 2014) [#1]
Can someone explain the difference between Delay and the timer functions?


Yasha(Posted 2014) [#2]
Delay takes a number of milliseconds, and simply pauses the application for that many milliseconds before continuing.

The timer commands are more complex. Instead of pausing for a specified length of time, WaitTimer pauses until a given timer object fires and resets itself. Timer objects are always ticking over in the background regardless of where you are in your program or how long has passed, generating a tick event at regular intervals until the timer is destroyed. So WaitTimer pauses until the next tick event from the given timer object - at the call point, you don't know how long that will be. You also create a timer object with a Hz parameter rather than milliseconds (number of times it should fire per second rather than the wait in between).

The point of timers is that they internally track how long it has been since the last tick and know when to fire on their own, so you can freely write code for your main loop without needing to worry about how long it takes or whether it varies a bit from frame to frame.


That said, I don't think people actually use timers all that much in complex code, as there are more advanced techniques that can do what they do, but better. Most often people start by relying on Flip/VWait and then add a tweening system later that takes care of this stuff in a more flexible way (or if it's a GUI app, you'll want it to be event-based instead).


laughing_man(Posted 2014) [#3]
Sounds very complex. It will take me a lot of time to get hang of it.

So, is it possible to create a simple Galaxy type game using only Delay?


GfK(Posted 2014) [#4]
You don't want to use Delay - your entire game will freeze for the duration of the delay. I can't think of any circumstance where you'd want that to happen.


Yasha(Posted 2014) [#5]
The main use - arguably the only use, in complex games - of Delay is that it's the only function that relinquishes unused CPU time (it sleeps the thread; the other timing options all burn cycles by busywaiting). It's therefore used in final releases of applications to ensure that you don't cause the computer to run hot unnecessarily, to "cut out" time you don't need from the final run.

But it doesn't actually contribute anything to the "design" of a program when used in that way - it isn't providing timing, it's just slotted around real timing commands to minimize the amount of busywaiting they have to do. So you don't need to worry about it while setting up.


As I hinted in post #2, if you're developing a graphical application, you can rely on Flip (or VWait, essentially the same thing) as a timer - it will lock the loop you put it in to the refresh rate of the screen. This is what most people do and is fine for initially developing your game (you can worry about advanced techniques for handling variable refresh rates later).

So Flip provides essentially the same service as WaitTimer, only it's already built-in (and more precisely tied to the display, which is good).


laughing_man(Posted 2014) [#6]
It will take some time for me to get a hold of all this GUI stuff after the CUI stuff. Sorry for the delay. Thank you everyone.