This is part of an ongoing series where we write a complete 2D game engine in C++ and SFML. A new tutorial is released every Monday. You can find the complete list of tutorials here and download the source code from the projects GitHub page.
When we started our component system, we created Awake and Start methods for our objects. However, they are not yet being called anywhere. We need to fix this before next weeks tutorial (where we begin our animation system) so the first part of this tutorial will focus on that. Also, we are currently responsible for maintaining, updating, and drawing our objects ourselves. We only have one object at the moment so it’s not really a problem, but we do not want to have to manually call update and draw functions when we have 100+ objects. Therefore we will also create a structure to do just that, the ObjectCollection.
There’s no background information required to understand the concepts in this weeks tutorial so let’s get coding straight away by creating our ObjectCollection.
1. We separate the processing of new objects into its own method so it can be run at a convenient time (normally the beginning or end of a frame). This method will be responsible for moving objects added that frame from a temporary new object collection to the main object collection so they are updated, drawn etc.
2. This is used to temporarily store recently added objects so that we can choose when they are added to the game by invoking the ProcessNewObjects. This will become clearer shortly when we write the method.
The Update, LateUpdate, and Draw methods are straightforward. They will simply loop through all objects contained in the objects collection and call the methods of the same name within the objects.
When we add an object we insert it into a new objects list. This list is processed by the ProcessNewObjects function.
We will call ProcessNewObjects at the beginning of the frame (although this can be any other convenient time). When we process new objects we need to call there Awake and Start methods, which resolves our first issue (the Awake and Start methods are now being called when an object is added to the game). Lastly, we copy the new objects to our objects list so we can update and draw them in next frame.
We’ll add the ObjectCollection to our game scene class. There may come a time when we need to add an object to a number of collections, if/when that happens we will look into how we can implement that; but until then each scene will have its own object collection, which will contain unique objects for that scene.
Make sure you delete the reference to the player as we’ll be using our new object collection.
We’ll once again change our update and draw methods in our game scene. Rather than update and draw the player directly, we’ll update/draw our object collection. Now, whenever we add an object to our collection we no longer need to manually call its update/draw methods.
We’ll change the OnCreate function so that it creates a new player object and adds it to our collection.
If you run the game you’ll notice that the player is not being drawn. This is because we are adding the object to the collection but we are not calling the ProcessNewObjects function that moves the object from the new objects collection to the actual collection that is updated and drawn. We’ll process the new objects at the start of a new frame before we do anything else.
Now when we run the game the Viking is back again!
So we can now add objects but what happens when we want to remove an object? We will need a way to flag an object as ready for removal and then create a similar function as ProcessNewObjects but for processing removals.
Creating a removal flag can be accomplished with a simple bool and some accessors/mutators in our Object class.
This is a flag that will be used to determine if an object should be removed from a collection. Let’s create a new method in ObjectCollection that will process removals.
This loops through all of our current objects and if the removal flag is set it is removed from the collection. We need to call this method in our game scene at a suitable moment. Let’s do this just before we process new objects in the Update method.
With this implemented, if we want to remove an object from the game, it is as simple as calling the QueueForRemoval method on that object (we’ll demo this in a later tutorial). It should be noted that if any of our classes keeps its own reference to an object it will no longer be drawn or updated by the collection but because we are using smart pointers you will still be able to access the various components of the object and even manually update/draw the object if need be.
By creating a class responsible for objects, we can: have different collections for different scenes, which will allow us to: easily draw/update different objects depending on the scene; manage when objects are added and removed from a scene; and have a centralised place for storing objects.
It’s also worth briefly mentioning that currently we are creating new objects and erasing them as need be. This is very rarely the best way to do this and in future tutorials, we will definitely be looking into implementing object pooling. You can see an example of an object pool that I wrote in Unity here.
As always, if you have any suggestions for what you would like covered or are having any trouble implementing a feature, then let me know in the comments and I’ll get back to you as soon as I can. Thank you for reading 🙂