# Reset timer in XNA

I use a simple timer to count the time the player has to play the game. After 60 seconds the game is terminated. But if the player whants to play again, the timer continue and not resets! How can I reset the timing?

 seconds = gameTime.TotalGameTime.Seconds;
if (seconds == 60)
{
currentGameState = GameState.GameOver;
seconds = 0;
}


## 3 Answers

Since you're using XNA, I think the ideal (and typical) solution is to use your own timer variable. You're almost there, but you can't use TotalGameTime because of the comments in the other answers and because you can't be sure that the level would start exactly on the first second of a minute since the game started. Sure, it's possible if you have no menus and your game runs immediately and restarts every 60 seconds. But going forward I'm sure you're going to want to make it a little bit more robust.

So, what you want to do is create a field variable float seconds; at the top of your class. Then, in your update, you want to do this:

seconds += (float)gameTime.ElapsedGameTime.TotalSeconds; //Add the elapsed seconds so far
if (seconds >= 60) { //Check if 60s have elapsed
seconds = 0; //Restart the seconds counter
//End or restart the game
}


This way, all you have to do to reset the counter is set the seconds variable to 0 and it will start counting up again.

• +1 ElapsedGameTime should be the perfect solution. For the poster, note that we're now in floating point territory, hence the >= check ^^ Commented Jul 18, 2012 at 8:15

Your first problem is that Seconds can only be 59, it resets to 0 every minute and will never hit 60.

Either check if it's 59 - or write your own routine that uses TotalSeconds instead (which will continue to increase over 59 but never reset to 0) or some other way to detect the transition from 59 to 0.

if (seconds == 59)
{
...handle end round....
}


There are many ways to handle the issue now that it's pin-pointed, here's one

// When round starts
TimeSpan roundStartTime = gameTime.TotalGameTime;

// In update loop
if (roundStartTime.TotalSeconds.Add(60) >= gameTime.TotalGameTime.TotalSeconds
{
....handle end round...
}

• TotalGameTime.Seconds is an integer, so yes, it can be exactly 60. Commented Jul 17, 2012 at 13:22
• No it won't ^^ but you're right, changing the answer. Commented Jul 17, 2012 at 13:23
• Right, silly me as well. Commented Jul 17, 2012 at 13:33
• Wahey. TimeSpan.Seconds can be -59. Negative time! Commented Jul 17, 2012 at 13:33
• I just love how more complex issues hide beneath seemingly simply problems ^^ Commented Jul 17, 2012 at 13:34

The problem you have is that TotalGameTime.Seconds goes from 59 to 0 instead of 60, so it never actually hits 60, so that's the first thing to consider. You cannot simply check for TotalGameTime.Seconds == 0 either, because your game would terminate in the very first game loop when no time has really passed.

So what you really have to check for is the transition from gametime 59 to 0.

Therefore, what you could do is something like:

int seconds = 0;   //a field outside the game loop
...
if(gameTime.TotalGameTime.Seconds == 0 && seconds == 59)
{
//60 seconds have passed, terminate the game now
}
else
{
seconds = gameTime.TotalGameTime.Seconds;
}


Note however the following remark:

Fixed-step clocks update by a fixed time span upon every clock step. This results in uniform clock steps that may not actually track the wall clock time. Fixed step clocks were popular on console systems where one had tight control over code and a fixed system performance. Fixed-step clocks are also useful when trying to achieve deterministic updates for debugging, offline rendering, or other such scenarios.

That means that even if your TotalGameTime says 60 seconds have passed, it may not be real world 60 seconds. In the worst case, if a player slows down his system, it might even be a possible exploit/cheat, so you should explore other ways of tracking time as well.