8
\$\begingroup\$

I was trying three different ways of using ElapsedGameTime and TotalGameTime, because I want an exact match, so everything is updated/drawn accordingly.

As I was experimenting, I learned that the very first update, ElapsedGameTime and TotalGameTime are both 0.

Second update, ElapsedGameTime is 0.0166667, which is correct (60 updates per second). But TotalGameTime is 0, I don't understand why.

So, if I start to add from the third update (time += gameTime.ElapsedTime), ElapsedGameTime is equal to TotalGameTime, otherwise there will be always a 0.0166667 difference.

Can someone explain that to me?

UPDATED: code

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using System.Diagnostics;

namespace TestTime
{
    class TimeTest2
    {
        TimeSpan totalTimeElapsed;
        TimeSpan frequency = TimeSpan.FromSeconds(5.5f);

        int times = 0;
        int timesCheckpoint = 1;

        public void load()
        {

        }

        public void initialize()
        {   
            totalTimeElapsed = TimeSpan.Zero;
        }

        public void update(GameTime gameTime)
        {
            times++;
            String debug = "";
            TimeSpan zero = TimeSpan.Zero;

            if( times > 2 )
            {
                totalTimeElapsed += gameTime.ElapsedGameTime;  
            }

            if( totalTimeElapsed != gameTime.TotalGameTime )
            {
                debug += " Diff time:"+times+" ["+totalTimeElapsed.ToString() + " != " + gameTime.TotalGameTime.ToString() + "]";
            }

            TimeSpan checkpoint = TimeSpan.FromSeconds(5.5f*timesCheckpoint);
            if( gameTime.TotalGameTime >= checkpoint )
            {
                debug += "5.5f MARK ";
                timesCheckpoint++;
            }

            if( !debug.Equals("") )
            {
                addDebug(debug + "  -" + gameTime.TotalGameTime.ToString());
                addDebug("");
            }
        }

        public void draw()
        {

        }

        public void addDebug(string str)
        {
            Debug.WriteLine(str);
        }
    }
}
\$\endgroup\$
8
  • 2
    \$\begingroup\$ Very interesting discovery. \$\endgroup\$
    – ashes999
    Commented Apr 19, 2012 at 19:37
  • \$\begingroup\$ I'm curious as to how you are reading these values GameTime.TotalGameTime.Milliseconds, or GameTime.TotalGameTime.TotalMilliseconds? \$\endgroup\$
    – user159
    Commented Apr 19, 2012 at 20:18
  • 1
    \$\begingroup\$ I went ahead and pinged Shawn Hargreaves. Maybe he can answer this :) \$\endgroup\$
    – user159
    Commented Apr 19, 2012 at 20:38
  • \$\begingroup\$ I use TimeSpan.toString() \$\endgroup\$
    – user658091
    Commented Apr 19, 2012 at 20:55
  • \$\begingroup\$ can you post the code you are using to determine this? \$\endgroup\$
    – user159
    Commented Apr 19, 2012 at 21:17

1 Answer 1

7
\$\begingroup\$

Looks like a little bug, or a precisely correct behaviour, from which point you want to look at it. See, the Update method is called twice before first call to Draw, first time being outside of a main game loop.

TotalGameTime is updated in the moment between end of the Update and beginning of the Draw, inside the game loop. ElapsedGameTime on the other hand is updated after start of the main loop and before Update. Both of them are initialized to 0 before call to the first Update.

If you read the description of a ElapsedGameTime is says:

The amount of elapsed game time since the last update.

At the first frame inside the game loop it is a second Update call. So ElapsedGameTime shows time elapsed since first Update call.

OTOH, TotalGameTime says:

The amount of game time since the start of the game.

This is a more vague, but it looks like game can't be considered started, until it entered a main loop and begins drawing itself. Thus TotalGameTime updates itself first time after second call to Update. Hence the difference you observe.

\$\endgroup\$

You must log in to answer this question.

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