I’m trying to access MonoGame’s gameTime outside of my Game’s Update and Draw functions.
They take a parameter of GameTime gameTime, and I don’t know where that comes from.
Currently I declared a public static GameTime gameTime in the Game1 Class and update it’s value every Update and Draw using the value that was passed into them.
It seems redundant though as it is getting an already existing gameTime from somewhere else, which is also updated (I’m guessing only once) elsewhere.
Is there a better way to access gameTime other than my static option, or is that it?
Can someone explain where the passed in gameTime comes from?
Your Game1 class derives from Game, which internally handles calling your Update and Draw methods (note how they’re overridden). GameTime is updated and passed into those methods from Game.
What you did sounds fine if it works well for you. What I like to do is write a static wrapper and update it using the passed in GameTime values before updating anything else. Again, this just a personal preference; do what works best for you.
Your static option works, which is what I would’ve done. You could always, pass the GameTime as a variable, or calculate some double values being deltaTime, etc., which are declared in a static Time class or something, and you update them with Update.
Alright thanks guys. I will just keep using my static GameTime.
Like the question says I thought there would be a way to reference the original, even if it’s readonly.
After the very first Update, all GameTime references passed in are copies of the original object.
If you use ref, it wouldn’t be a new object.
You would have to architect your solution appropriately, but I apply dependency injection and use a source. Dependency injection ,for any unfamiliar, is an application of the dependency inversion principle, which states that concretions should depend on abstractions and helps you reduce the need for change by only coupling to the bare minimum.
So something like this
/// time fields/helpers
Then, this gets injected into any classes that need it.
Something ( IGameTimeSource timeSource)
timeSource_ = timeSource;
Note that I am doing poor man’s DI containers. No DI framework so it isn’t a sea of indirection.
What does this get me? A few fun things!
- I can pause the game time easily. This lets me pause while letting other parts of the game continue. I can apply a decorator and apply an offset for in game stuff.
- I can thread protect time updates and access easily via a decorator.
- I can test the whole thing. I can also easily spoof time in any unit or acceptance test.
- I avoid memory allocations because I can use helpers to return things like “elapsed time since frame started” as a double. So everything in the system can use time without allocating time anywhere.
A static works in some ways, but that static will also be shared in a test environment. That makes it very hard to work with. It also makes it hard to manage control flow with respect to time. If a change, such as a pause offset, has to happen, you can’t easily pull apart the grilled cheese sandwich of a coupling to a static field.
Hope those thoughts help a little.