Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);
        }
    }
}
share|improve this question
2  
Very interesting discovery. –  ashes999 Apr 19 '12 at 19:37
    
I'm curious as to how you are reading these values GameTime.TotalGameTime.Milliseconds, or GameTime.TotalGameTime.TotalMilliseconds? –  Joe Apr 19 '12 at 20:18
1  
I went ahead and pinged Shawn Hargreaves. Maybe he can answer this :) –  Joe Apr 19 '12 at 20:38
    
I use TimeSpan.toString() –  user658091 Apr 19 '12 at 20:55
    
can you post the code you are using to determine this? –  Joe Apr 19 '12 at 21:17
show 3 more comments

1 Answer

up vote 7 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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