0

I just found something that caught my interest. See, I usually do my "timers" with an int Time = Duration; and in my update it would go Time -= gameTime.ElapsedGameTime.MilliSeconds;. However, I just tried to do it with gameTime.ElapsedGameTime.Seconds instead, since I thought it would be more performance-friendly. But now it isn't updating at all!

My code is basically:

Declaring variables: int Time = 120; SpriteFont spriteFont1;

Updating: Time -= gameTime.ElapsedGameTime.Seconds;

Drawing a string to tell me the time: spriteBatch.Draw(Time, Position, Color.White)

Somehow, the string only tells me that the time is decreasing when I'm doing it with MilliSeconds, not Seconds. Why?

Neophyte
  • 55
  • 6
  • Isn't ElapsedGameTime the time elapsed between every update? I could be wrong, but if it's the time between each update, and each update take less than a second, it will always be zero because it resets every time. – Pierre-Luc Pineault Jun 13 '13 at 19:55
  • Use `Total*` like `TotalSeconds`, instead of `Seconds` – Diamondo25 Jun 13 '13 at 19:56
  • @Diamondo25 TotalSeconds would not work for his countdown timer. It would do -1 second, then -2, -3, etc. And by the way OP, Seconds is not more "performance-friendly" than milliseconds. Well maybe if you have a million calls you could save a few micro-seconds (Don't know exactly how it works internally), but there's no way it will be the bottleneck of your app. – Pierre-Luc Pineault Jun 13 '13 at 20:04

1 Answers1

1

gameTime.ElapsedGameTime.Seconds is the amount of time since the last update. Since it updates very fast the time between updates are smaller than seconds. Seconds gets returned as an int so you will always get 0.

computer10171
  • 445
  • 4
  • 12
  • Thanks, was already said in the comments, but this is the only physical answer. Also, if I wanted it to be the actual time, not the time between frames, how would it be done? – Neophyte Jun 13 '13 at 20:19
  • Either use `TotalGameTime` or just add the milliseconds to a variable. By storing the time in a variable, you will be able to 'reset' the timer. – computer10171 Jun 13 '13 at 20:30