4
\$\begingroup\$

Using XNA, is it possible to access the GameTime object before Update is called for the first time?

Can it be used in the game constructor, Initialize or LoadContent methods?

\$\endgroup\$
5
  • 4
    \$\begingroup\$ Wouldn't you just assume its value is zero anyway? \$\endgroup\$ Commented Apr 18, 2011 at 14:38
  • 2
    \$\begingroup\$ I'm curious as to what is your purpose there, if you wish to share. \$\endgroup\$
    – jv42
    Commented Apr 18, 2011 at 14:52
  • \$\begingroup\$ I need to pass it to another static class that manages game time as well as other time related things that are specific to the project. At the moment the first opportunity I have to do this is on the first Update loop, every loop after that the line is redundant. I was wondering if there was a cleaner way to do it. \$\endgroup\$
    – Jaxe
    Commented Apr 18, 2011 at 16:27
  • \$\begingroup\$ It's not that big of a deal... It's certainly not going to cause massive frame loss or anything. \$\endgroup\$ Commented Apr 18, 2011 at 17:44
  • 1
    \$\begingroup\$ I realise that, but it's not very elegant. \$\endgroup\$
    – Jaxe
    Commented Apr 18, 2011 at 17:49

1 Answer 1

5
\$\begingroup\$

I don't believe this is possible:

The GameTime object received by Draw and Update isn't technically owned by anyone, but is instead re-created each Game.Tick and passed to Update and Draw from there.

Internally, Tick fills the value of the Total/ElapsedRealTime properties based off of the current high performance counter value as reported by Stopwatch.GetTimestamp(). If the PC the program is running on does not have a high performance counter, then it returns DateTime.Now.Ticks.

The Game Time properties (as opposed to real-time) also use the Stopwatch.GetTimestamp, however elapsed time since application launch and last frame are computed internally and then filled in before GameTime is passed to Draw or Update - so there's no external way to compute those values directly.

Source: http://forums.create.msdn.com/forums/t/10587.aspx, bold mine. Also included there are several work arounds, similar to what you've already outlined though.

\$\endgroup\$
3
  • \$\begingroup\$ Based on this, the main game Update method could probably update the static time manager class mentioned before any subsequent game components needed it, ie before calling base.Update. \$\endgroup\$
    – DrDeth
    Commented Apr 18, 2011 at 19:01
  • \$\begingroup\$ Yes, that is likely, but it still doesn't remove having a line in the Update method which passes gameTime somewhere else. \$\endgroup\$
    – Nate
    Commented Apr 18, 2011 at 19:12
  • \$\begingroup\$ Thanks, I'll play around with these workarounds and see what works. \$\endgroup\$
    – Jaxe
    Commented Apr 19, 2011 at 13:59

You must log in to answer this question.

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