3
\$\begingroup\$

Example:
There is a bomb (like in Counter Strike) which explodes after 45 seconds. Should I—

  • store the 45 seconds in a variable, and decrease it by the delta time at every update, or
  • store the time when the bomb was planted, and check at every update if it is less than or equal to the current time minus 45 seconds?

How do I choose which approach to use?

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

There is no "best" way to do this, both methods are completely viable and personal preference. However I believe that the delta time version is superior because it's inherently less error-prone than checking the time difference.

Imagine this piece of pseudo code:

startTime = System.now

function update() {
    now = System.now
    difference = now - startTime

    if (difference >= 45) {
        explode()
    }
}

This would work great and would make the bomb explode after 45 seconds. However think about what happens if we want to pause our game. Pausing your game simply stops movement or perhaps sets delta time to 0 to emulate no time passing by. This would break this system since system time keeps ticking on even if you pause your game.

Of course this is quite easily fixed by using your own time system (akin to what Unity does) but, really, why bother if you can just chose the less error-prone way to begin with?

Delta time is more "safe" to use than checking the time difference. If we convert the above pseudo code it would look something like this:

countDown = 45

function update(delta) {
    countDown -= delta
    if (countDown <= 0) {
        explode()
    }
}

This is immediately more safe than the time difference since if you pause your game using delta manipulation this will also pause the timer on your bomb.

It is also easier to expand on since if you want to change how fast your bomb timer is - say speed it up 2x - you just multiply the delta value by your speed value. This is more difficult to accomplish using time difference and much less clean code-wise compared to the delta-time method.

\$\endgroup\$
5
  • \$\begingroup\$ I was worried if there are accumulating floating-point rounding errors when subtracting the delta time, and it would explode after 44-46 seconds instead of 45. \$\endgroup\$
    – user
    Commented Dec 23, 2017 at 0:08
  • 1
    \$\begingroup\$ @user floating point errors are most likely not large enough for this to be a problem. Even if the floating point error was something like 0.001 (\$10^{-3}\$) it would take 1000 seconds before there was a 1 second difference. But also this is probably not going to be a problem anyways since precision errors go both ways, so one frame you might have say \$0.001\$ error and the next you might have \$-0.001\$ error so they balance out to \$0.000\$ error in the end :) \$\endgroup\$
    – Charanor
    Commented Dec 23, 2017 at 0:11
  • 1
    \$\begingroup\$ @user and if you think about it this way - chances are you will still overshoot the 45 second mark (at least by some hundredth of a second) since your delta time is not always a stabile 60. If you drop to 59 FPS for one frame you have to wait a whole extra \$1 / 60\$ seconds for the bomb to explode! But when we get to this level of nitpicking there is literally nothing we can do to fix it and just have to accept that sometimes the bomb will be \$1 / 60th\$ of a second faster or slower to detonate (which is fine, since it's barely noticeable!). \$\endgroup\$
    – Charanor
    Commented Dec 23, 2017 at 0:21
  • \$\begingroup\$ System.Time is a bad choice. What if the user pauses the game? \$\endgroup\$ Commented Dec 23, 2017 at 0:25
  • 1
    \$\begingroup\$ @Draco18s I would suggest reading the paragraph just below the first code snippet where I talk about exactly that ;) \$\endgroup\$
    – Charanor
    Commented Dec 23, 2017 at 0:26
0
\$\begingroup\$

Both are completely viable, both are best.

For my tastes, I would go with the Update() and decreasing the float of 45 seconds by Time.DeltaTime - but that is just 100% preference.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .