I am following the basic Monogame tutorial from this website. What I can't figure out is why Game1.cs includes calls to base.Update and base.Draw? If I remove these function calls, it does not seem to affect my game in any way...
Code Game1.cs
protected override void Update (GameTime gameTime)
{
// update my car class
_myCar.Update();
// what does this do? It seems I can leave it out?
base.Update (gameTime);
}
protected override void Draw (GameTime gameTime)
{
// draw my car
_spriteBatch.Begin ();
_myCar.Draw (_spriteBatch);
_spriteBatch.End ();
// what does this do?
base.Draw (gameTime);
}
Accordingly to the source code of MonoGame linked by Dylan Wilson, the Update and Draw methods from the base class Game just called Update(...)
and Draw(...)
on a each element of a filtered GameComponentCollection
, a Collection of IGameComponent
.
IGameComponent
is an interface that show how XNA and Monogame want you to manage your code. It comes with other interfaces such IUpdateable
and IDrawable
. Two other class implement these interfaces and provide the logic : GameComponent
and DrawableGameComponent
.
The idea behind these interfaces and classes is that all components of a game have a similar cycle of update/draw. It standardize the method's names and parameters (Initialize, LoadContent, Update, Draw,...) and provides a skeleton of what your custom component should be like. Depending if your classes needs to be updated, drawn or none of these, you can inherit of these classes.
This is where GameComponentCollection
operates : you can add your components to Game.Components
and the base Game class will automatically handle the update cycle by calling Update() and Draw() without your intervention. You can also set the order of call between the component with the properties offers by GameComponent
and DrawableGameComponent
.
Anyway, it seems that some people (me include) and numbers of tutorials don't use that possibility in their code. You're free to call Initialize, LoadContent, Update and Draw from your custom class by yourself. In this case, it seems that calls to base.Update(...)
and base.Draw(...)
are not required in your program.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With