Clear
Introduction
The Clear function is a function that will empty a PositionedObjectList of all contained references - it operates the same way as calling Clear on a normal List. However, although Clear is a very common function when dealing with regular Lists, it is not often used on PositionedObjectLists.
If you are using Clear, make sure you understand the information discussed in this wiki.
Why should Clear (usually) not be used on PositionedObjectLists made in Glue?
The Clear function should usually not be used when dealing with lists in Glue. Let's investigate why this is the case. For this discussion we'll assume that your game has an Entity called Bullet, and that your Screen has a PositionedObjectList of Bullets which will is called BulletList.
In this example the BulletList will have bullets added to it. Sample code for this might look like:
You might expect that simply calling BulletList.Clear will remove all bullets from your game; however, this is not the case. To understand this, let's add some comments to the code listed above so we can understand what is happening:
The important thing to note here is that calling BulletList.AddBullet is not required for the bullet to be drawn and updated. The reason is because the reference to your new bullet is stored in two places:
The FlatRedBall Engine
The BulletList
Since the new Bullet is stored by both the FlatRedBall Engine as well as the BulletList, then to fully-destroy the Bullet it must be removed from both the FlatRedBall Engine as well as the BulletList.
However, the Clear function will only remove the Bullet from the BulletList. Calling Clear will result in the BulletList being empty, but the Bullets will still be a part of the engine.
Since this is such common behavior in FlatRedBall, all Entities have a Destroy method. When Destroy is called, the Entity removes itself from both the FlatRedBall Engine and any PositionedObjectList it is a part of. This is why the preferred method of clearing a list is to call Destroy on the contained objects:
Last updated