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)?
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)?
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).
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.
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!
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!