1
\$\begingroup\$

I'm looking to change the way in which my game uses GameTime to run the game so that I can easily pause the game (elapsedTime = 0) or change how slow/fast the in game elements interact as a scaler of the actual time passed (elapsedTime *= timeScale).

In my project, I always make the assumption that the game is not running on a fixed time step for a number of reasons. Due to this, any changes of states have the elapsed time factored in from GameTime. I'm hoping this means I can simply simulate slow-mo, fast-mo, force short freezes (like when you kill an enemy in TLoZ: Windwaker - there's a short pause for emphasis) or pause the game by interfering with the GameTime.ElapsedGameTime that all of my Update() code uses.

My first thought is to stop using GameTime within any update code, and to instead wrap a new class (MyTime) around GameTime.

MyTime would do the following:

  • Seperates real time and game time. Real time is the passage of time outside of the system. Game time is the passage of time within the game's world, i.e if slow motion is in effect, the time passed within the game would be lower for the period of slow motion.
  • Time scaling can either be started and ended, or requested to be active for a length of real time or game time.
  • Time can be paused or frozen with either manual start and stop calls, or for a length of time, again based on either real or game time.

Based on this discussion, where it's states that GameTime is recreated every tick within XNA's base Game class, I'm hestitant to just store a reference to GameTime within MyTime as I personally don't know how GameTime is altered. I also don't know the 'best' way to retrieve the GameTime reference in the first place, the reference is available within the Update() and Draw() calls, however since gameTime isn't made available throughout any derived from Game class, it's implied taking the gameTime reference out of those update and draw calls isn't intended.

Is my intended implementation of this the right way of achieveing this system? What is a good to expose GameTime to MyTime? Should I implement my own way to track time and not rely on GameTime at all?

\$\endgroup\$
2
\$\begingroup\$

I would still use GameTime to track the passage of time (no sense reinventing that wheel), but I would not bother storing it in your MyTime object and you certainly shouldn't try to subclass GameTime. Just create your own type with whatever fields you want and then use the GameTime provided to the Game class to update your instance however you want, applying your time scale, etc. This gives you full customization of the timing structure without having to worry about how the Game stores/updates the GameTime object.

Edit: Humorously I'm going against my own advice in that link but you learn a thing or two from six additional years of game development. So listen to me by not listening to me. :)

| improve this answer | |
\$\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.