# gametime

Hi

As a total beginner, what does the parameter GameTime gameTime in Update() and Draw() mean?

How to understand this and do we ever have to do something with this parameter, other then base.Update(gameTime) and base.Draw(gameTime)?

1 Like

I’m sure someone can give a more detailed explanation, but “long story short”, GameTipe is a Monogame class. We use this class for a variety of things in game, for example the Update method and others methods like ElapsedGameTime, which can help you track the framerate of your game with a little math trick. Always remember this:

GameTime → Game - Time. It’s the class that holds information about the time state of a Game (which is why Update method uses it, since everything in update method is updated every frame).

1 Like

GameTime stores the amount of time that has passed, or elapsed, since the last Update or the last Draw. It’s used to account for variable frame rates. If you write your code to properly account for the time that has elapsed between frames, you can make it so that your game functions whether it’s being run at 30 FPS or 120 FPS, and can even do things like slow down or speed up “time” by modified the elapsed game time before using it in your code.

Here’s a theoretical scenario that may help you understand it. Imagine that you want to move a `Rectangle` across the screen. Each update, you might try doing something like this:

``````public override void Update(GameTime gameTime)
{
rectangle.X += 5;
}
``````

That will move the rectangle five pixels to the right each frame. So far, so good. But what if you wanted to support a variable frame rate rather than enforcing a fixed frame rate? If the frame rate were 30 FPS instead of 60 FPS, the rectangle would move a lot slower, and if it were 120 FPS, the rectangle would move a lot faster. So how would you support any frame rate while making sure that the rectangle moves across the screen at the same speed?

That’s where GameTime comes in. Since it tells you exactly how much time passed since the last update, you can use it to move the rectangle a variable amount of distance depending on that elapsed time.

``````const double sixtyFramesASecond = 1.0 / 60.0;

double horizontalLocation = rectangle.X;

public override void Update(GameTime gameTime)
{
horizontalLocation += 5 * (gameTime.ElapsedGameTime.TotalSeconds / sixtyFramesASecond);
rectangle.X = (int)horizontalLocation;
}
``````

That code might look a bit scary, but it’s actually pretty simple. It takes the original movement value of 5 and multiplies that by the result of dividing the actual elapsed game time (in seconds) by what the elapsed game time would be at 60 frames a second. If the actual elapsed game time is exactly 1/60th of a second, then the rectangle will be moved five pixels to the right. If the actual elapsed time is more than that (meaning the frame rate is lower), then the rectangle will be moved further, to compensate for the increased delay between frames. If it’s less than that (meaning the frame rate is higher), then the rectangle is moved less, to compensate for the smaller delay.

The horizontal location is stored in a separate floating point variable to remove integer roundoff error that would otherwise occur.

By default, I believe, a MonoGame project has fixed time step enabled. Fixed time step means the game will try to always run at the specified frame rate, so the GameTime value will always be the same. The result is that, even in code that accounts for GameTime, if fixed time step is enabled, no variable frame rate processing happens. If your game were being run on a lower-spec PC that couldn’t run it at the full FPS, instead of having a slower frame rate but regular gameplay, the gameplay would slow down, too. Conversely, if your game were being run on a high-spec PC with a high refresh rate monitor, with fixed time step the game wouldn’t be able to render at a higher frame rate to take advantage of the monitor’s refresh rate.

If you want to support variable frame rates, you’ll need to disable fixed time step and account for GameTime differences in your update code.

To disable fixed time step, in your `Game1`, you’ll need to set `IsFixedTimeStep = false;`, either in your constructor or in one of the initialization methods, though the value can also be changed even after your game is up and running.

Note that if VSync is enabled, your game still won’t run at higher frame rates than what your monitor is capable of. VSync syncs the frame rate of the game to the refresh rate of your monitor. To disable Vsync in MonoGame, you set the `SynchronizeWithVerticalRetrace` property of your `GraphicsDeviceManager` to false. But even if it’s disabled in your game, it can still be forced on by your graphics drivers, like if you’ve enabled VSync in the NVIDIA control panel if you have an NVIDIA GPU, for instance.

VSync is generally desirable, as it prevents screen tearing, but I’ve heard that it can affect input latency. And if you want to test your game at higher refresh rates than what your monitor supports, you’ll have to turn it off, both in MonoGame and in your graphics control panel if it’s enabled there.

1 Like

You should write documentation for monogame website if you ever have time!
There is very little in there and beginners have to ‘guess’ a lot.
GameTime is one of those things beginners like me hold their breath for.
I finally understand it now thx to your explanation!

1 Like

Glad I could help! As far as working on official documentation, I don’t know, I feel a bit more comfortable in the more informal setting of a forum. Also, actually working on the official docs would require a certain amount of dedication, and since I’m awfully non-committal, that’s a problem!