0
\$\begingroup\$

I'm making an in-game clock/calendar system. In-game time is 12x real time. In addition, in-game events can fast-forward time. I also have in-game events that reset or trigger at the turn of each day.

My issue: I don't want to perform an end-of-day check every time there's a frame update, like:

void Update(float _delta) {
    gameTimeInSeconds += _delta * timeScale; // time scale is 12.
    while (gameTimeInSeconds >= currentDay * 86400.0)
        NextDayReached();  // increments currentDay, and updates in-game events.
}

It feels inefficient to make the check on every frame. Is there a more efficient way to determine when a new day has been reached?

Or am I worrying over an ultimately tiny issue?

\$\endgroup\$
4
  • \$\begingroup\$ It's probably tiny, but realize that with multithreading, you don't have to have everything running off the UI thread. Some things, like physics for instance, may need to update 120x/second. Others like AI can get away with 1/sec. Not everything needs to run off your framerate clock. Just things that ultimately relate to graphical smoothness. Like your HUD elements, object movement, animations, etc. Things that can be directly seen. \$\endgroup\$
    – RobStone
    Apr 21 '16 at 3:33
  • \$\begingroup\$ Is this native code? Or are you using a framework? \$\endgroup\$
    – Engineer
    Apr 21 '16 at 7:11
  • 3
    \$\begingroup\$ This seems to me like it's premature optimization. You should profile first to see if there is actually an issue there, or if you'd better spend time optimizing real bottlenecks. \$\endgroup\$
    – Vaillancourt
    Apr 21 '16 at 7:18
  • \$\begingroup\$ Yeah, I suspected that it could be simply premature. Just needed a sense of scale. \$\endgroup\$
    – Mark Ang
    Apr 21 '16 at 8:32
1
\$\begingroup\$

This is unlikely to be an issue. A single branch 60 times a second is not even a blip on the radar for a profiler.

However if you have arbitrary events that trigger at certain points (not just on turn of day) then you can make newDay a fixed recurring daily event.

Then the update loop will be:

void Update(float _delta) {
    gameTimeInSeconds += _delta * timeScale; // time scale is 12.
    while ((event = eventQueue.getNextTriggeredEvent(gameTimeInSeconds)) != null);
        event.handle();
}
\$\endgroup\$
1
\$\begingroup\$

In native code, where you're using a while loop you've set up in main, this is exactly how all checks are done. There is in fact no other way to do it, under these circumstances, since the native while loop runs as fast as possible and only acts when some condition is met (like yours).

In non-native framework code or for certain platforms (like JS), there may be ways to use the underlying native event loop to do this more efficiently, like JS setInterval() - you don't want to be running conditionals in the interval handler 30-60 times every second, when you could rather be calling the handler only when the native interval triggers. Native is more efficient, typically. HTH.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.