Accurate Countdown Timers

09/01/2010

Time is a critical component in many games. How long until your health returns? How long until your crops are ready to be harvested? How long until the next player’s turn in a turn-based game?

There are two main ways to do timers, depending on what you are trying to represent. If you are just trying to represent a “count down” to a single user, to keep them waiting in suspense, then you want the illusion of a certain number of seconds passing. It does not matter if they are close to accurate.

Conversely, if you are trying to really synchronize a user with a server-side game state (or other players) then you want the timer to be as accurate as possible.

Simple countdown

In the case where accuracy is a minor concern, you can make a timer which is guaranteed to count down from a million to 0 like this:

var start_count_1 = 1000000; function timer1(){ start_count_1--; render("timer1", start_count_1); } setInterval(timer1, 1000); 

 

The code is Javascript, but the principle works for any language which can simulate a callback once per second (or sleep for a second before calling your code).

By definition, every number will be hit exactly once. But various delays caused by other tasks, processing time, etc. mean that the numbers will not necessarily be hit every second. This code will take more than a million seconds to run. How many more seconds depends on how busy the computer is. On a heavily loaded machine maybe your million second countdown will take two million seconds. It’s unlikely to be that bad, but it’s bad enough to be a problem.

One extreme situation that I have seen in real life was an applet that stopped counting down when the focus was on another tab or screen!

More accurate countdown

A more accurate solution is to make a counter that corrects itself automatically by keeping track of the time that has passed since it started. Even a heavily loaded computer can keep time properly, because the time-keeping hardware is separate from the CPU in the Real Time Clock (RTC).

Now if the Real Time Clock hardware is inaccurate for any reason then either technique will fail. But if we presume it is good, then our second technique will give you a more accurate countdown by skipping seconds when necessary. This can be disconcerting for the user, but if it is what’s required for your game logic then it is still the right choice.

Here is an implementation of a more accurate counter, which skips seconds when necessary:

var start_count_2 = 1000000; var start_time_2 = (new Date()).getTime(); function timer2(){ var now = (new Date()).getTime(); var seconds_elapsed = (now - start_time_2)/1000; var current_count = start_count_2 - seconds_elapsed; render("timer2", current_count); } setInterval(timer2, 1000); 

This example does not truncate milliseconds, so you can actually watch and see how many milliseconds the interval counter has “lost” between ticks and use that to predict when an integer second would be skipped in a real application. The best way to simulate this is to really overload the computer with many CPU-intensive tasks like busy loops and Flash applications.

“More accurate” is not the same as “accurate”

Albert Einstein proved that the notion of a universally correct time is an illusion. As long as bits travel down a wire at less than the speed of light, the client computer will never be absolutely synchronized with the server computer. There are also limits of engineering quality to consider. But the biggest issue is that hackers may try to fiddle with your game’s sense of time. For all of these reasons, you must treat the game player’s clock with suspicion and minimize dependence on it.

Summary

The first technique I showed was like counting down during a game of hide-and-seek. It feels familiar to human beings, because every second is counted. The second example is like checking your watch over and over again and saying: “How long is it until it is time for the event?” If the user happens to look at the screen when a second is skipped, it might seem unnatural, but the count is more accurate.

Computers are nearly as bad at accurately counting down as humans are. Just as we can get distracted during a countdown, they do too. Just as you would use a stopwatch if you wanted an accurate countdown, you should force your program to consult the computer’s built-in watch, the Real-Time-Clock.

Subscribe to our Blog by Email