If you haven't created a new Monogame 3.8 game project yet to follow along with this tutorial, now would be the time to do it. For more information on creating a new project, check out the official documentation at https://docs.monogame.net/articles/getting_started/2_creating_a_new_project_vs.html.
In this part of the tutorial, we are going to create the code necessary for our
Scene class. This class will be the foundation for all scenes we could create in our game.
Perform the Following
In your project, create a new class file called Scene.cs and add the code found below. In the sections of this page after the code, we'll go over each of the aspects of this class file and what all of it is doing.
As you can see, the structure of it is pretty much the same as our Game1 class, with the exception of the two fields and the
So lets break this down section by section and discuss what each part of this is doing.
The Scene class has two fields;
We can use the
_game field to access some of the usual stuff like
Game1.GraphicsDevice. This also allows us to set public fields/properties in our Game1 class and make them easily accessible from the Scene.
_content field will be used to load content that is specific to the Scene. This will be a separate
ContentManager instance from the one in the Game1 instance. This has two benefits
- All content loaded from the Game1 class will act as sort of global content manager, since it's accessible from any scene through
_game.Content. This would be assets like fonts and audio that would be used in multiple scenes.
- By each scene having its own content manager, this means we don't have to load every asset up front at runtime. Instead loading only the global stuff as to point #1 above, and then the scene specific stuff per scene. This means a smaller memory footprint throughout the game life cycle.
Next we'll take a look at the constructor
As far as constructors go, this a pretty simple one. It takes an instance of
Game1 as the only parameter. An
ArgumentNullException is thrown if the value given is
null. From here we just cache the reference into the
The Initialize method is where all initializations will be performed for our scene.
An important thing to note is that this is where the
_content field is initialized, and immediately the
LoadContent() method is called. This is to ensure that all content is loaded before the implementing scene initializes, mimicking the same way that the
Game class is done in MonoGame. We do this so any information about assets width, height, etc is available for any objects that need them when we initialize.
In the order of events, Initialize will be called on the same frame that the scene is set as the active scene, and before the first Update is called on the scene.
LoadContent() method is where all the content for the scene will be loaded.
As we went over in the Initialize section,
LoadContent() is called from within the
UnloadContent() allows us to unload any content loaded by the scene when the scene ends.
When we switch from one scene to another,
UnloadContent will be called to free up any memory before the next scene is loaded.
Update(GameTime) method is pretty simple.
We'll use this to handle all update logic for the scene.
BeforeDraw(SpritBatch, Color), we'll handle preparing the scene for rendering to the screen.
The method will take two parameters; A
SpriteBatch instance and a
Color value. Next we clear the backbuffer using
_game.GraphicsDevice.Clear(Color), giving it the color value provided. Then we call
spriteBatch.Begin() to get us started with rendering.
BeforeDraw(SpriteBatch, Color) is call, we'll call
This will take in the same
SpriteBatch instance that was used in
BeforeDraw(SpriteBatch, Color) so we can use it to render visual assets to the screen.
Finally, we have
AfterDraw(SpriteBatch), which will be called immediately after
Draw(SpriteBatch) is called.
This takes the same
SpriteBatch instance that was used in
BeforeDraw(SpriteBatch, Color) and
Draw(SpriteBatch) so that we can call the
SpriteBatch.End() method so signal that we are done drawing.
On the next page, we're going to update our
Game1 class to make use of the
Scene class we just created.