0
\$\begingroup\$

Specifically talking in the context of JavaScript game development.

For example, I can use the language specific timeout mechanism:

// Timeout is not tied to game loop at all, tied to language specific timer loop
setTimeout(() => player.age += 1, 1000);

Or I can create some custom game timer

game.createFixedDelayTimer(() => player.age += 1, 1000);

// In update loop
let lastTimerCheckTime = Date.now();
function update(game) {
  checkTimers(game, lastTimerCheckTime); // Fire timer if specified delay has passed.
  lastTimerCheckTime = Date.now(); 
}

The disadvantage to approach 2 is that the checks are less granular... but maybe it's still desirable so that game behavior is deterministic?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

It's your choice to make, quite honestly. You can do research to see if you find setTimeout to be more or less efficient than your custom timer. Personally, I don't like to create timers using the browser's api when I already made a game loop I can keep track of time with.

The 'less granular' checks - as you put it - isn't a disadvantage since the check is happening in your update loop. Checking if a timer has expired more often than you update isn't more helpful :)

\$\endgroup\$

You must log in to answer this question.

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