# Get GameTime's milliseconds in plain format

I am trying to fire an action every 125ms.

To get the current milliseconds, I go this way:

double passedMs = GameTime.TotalGameTime.TotalMilliseconds;

//this is a double as well
currentMs += (currentMs - passedMs)

if (currentMs > 125)
{
//Action
currentMs = 0;
}


But the value I get in passedMs is not simply milliseconds, it's more like 8833.351.

So what could I do here?

My understanding is that TotalGameTime represents how long it's been since the executable opened. In your case, it's been up around 8 seconds. For your += statement, I think currently you're taking the difference of the two, and adding it to the current time...? I'm not even sure what that resulting value would be.

I think more likely, you're looking for ElapsedGameTime (documentation on MSDN); replace the contents of the parentheses with just that to get what you're looking for:

currentMs += GameTime.ElapsedGameTime.TotalMilliseconds;


Also, I would advise you to subtract 125 from currentMs, rather than setting it to 0. If currentMS becomes 200, then it's going to have somewhat inconsistent timing.

if (currentMs > 125)
{
// Action
currentMs -= 125;
}


Use an accumulation buffer, this way if you wait too long you don't waste the left-over. (But get your timing logic straight first)

currentMs += GameTime.ElapsedGameTime.TotalMilliseconds;

if (currentMs > 125) {
//Action
currentMs -= 125;
}


EDIT: oh well other answer has got the same code now lol

• Sorry :) I was like "wow this answer would be great if it had some code", so I added it before I saw yours! People: Upvote this guy too! Oct 4, 2013 at 12:46