This tutorial will walk you through how to create entities which inherit from FlatRedBall types. More accurately, the process of having Entities inherit from FlatRedBall types is very simple – all you have to do is change the BaseType to the desired type. However, this tutorial will discuss how to work with entities that inherit from FlatRedBall types, and why taking advantage of this feature can be helpful.
First we’ll create a simple project with a single Entity that includes a Sprite, and a Screen which includes a large list of instances of this entity.
To create the Entity:
- Right-click on Entities
- Select “Add Entity”
- Name the Entity “SpriteEntity”
- Right-click on the newly-created Entity’s Objects
- Select “Add object”
- Select “Sprite” as the type
- Click OK
Now that we have an Entity, let’s set the Sprite to use a Texture:
- Download the Bear.png image from here:
- Go to the location where you saved the file
- Drag+drop the PNG from the location where you saved it into the SpriteEntity’s “Files”
- Select the SpriteInstance object
- Set its Texture to “Bear”
Now that we have an Entity, let’s create Screen:
- Right-click on Screens
- Select “Add Screen”
- Name your Screen “MainScreen”
- Click OK
Next let’s create a List of SpriteEntites:
- Right-click on Objects
- Select “Add Object”
- Select “PositionedObjectList<T>” as the type
- Select “Entities\SpriteEntity” as the List Type
- Name the list “SpriteEntityList”
- Click OK
Finally let’s populate the List in code:
- In Glue select the “Project”->”View in Visual Studio” menu option
- Open MainScreen.cs
- Add the following in CustomInitialize:
int numberToCreate = 4000;
for (int i = 0; i < numberToCreate; i++)
Entities.SpriteEntity newInstance = new Entities.SpriteEntity();
(float)FlatRedBallServices.Random.NextDouble() * 3;
Camera.Main.PositionRandomlyInView(newInstance, 40, 40);
Measuring resource usage
If you’re running this project on a modern computer then it’s likely that you may not be noticing any performance problems. However, despite running without problems on the PC, this same project may have significant performance and memory consumption issues on less powerful devices, such as the iPhone.
One solution to this problem would be to make the Entities be manually updated; however that may not be so easy in this particular case. The reason is because our Entities are all spinning and require every-frame logic to have their rotation applied.
However, making the Entities inherit from sprite can actually improve performance and memory usage. To understand why this is the case, let’s first investigate how many manually updated objects are in the FlatRedBall engine. To do this:
- Open your MainScreen.cs in Visual Studio
- Add the following code to CustomActivity:
For more information on this method call, see the WriteAutomaticallyUpdatedObjectInformation page.
We can see that we have 4000 Sprites (one for each instance of our SpriteEntity) and also 4000 PositionedObjects (each SpriteEntity inherits from PositionedObject).
Using Inheritance to reduce automatically-updated object count
Now that we have this set up, let’s change our SpriteEntity to inherit from a Sprite. To do this:
- Select SpriteEntity in Glue
- Change “BaseEntity” to Sprite
Now we can simply have the SpriteInstance inside of SpriteEntity be a reference to the SpriteEntity itself. To do this:
- Select SpriteInstance
- Set “IsContainer” to true
Now that the SpriteEntity inherits from a Sprite, and the SpriteInstance is the container, we have essentially eliminated half of our runtime objects. To see this result, run the game again:
You can see that since all of the entity instances are Sprites, all PositionedObjects have been removed (the engine reports 0 PositionedObjects).