4

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);
        }
Kokodoko
  • 26,167
  • 33
  • 120
  • 197
  • 1
    It's easy enough to check for yourself what these base methods do since MonoGame is open source. https://github.com/mono/MonoGame/blob/develop/MonoGame.Framework/Game.cs – craftworkgames May 09 '15 at 14:09

1 Answers1

4

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.

ReMinoer
  • 402
  • 3
  • 11