Home > c++, game engine, object oriented programming > Orphans and Parents

Orphans and Parents

December 16th, 2004 zeraien

I added a new method to the base object called destroyAllChildren. This one not only makes children into orphans, but also kills them, meaning they are garbage collected and removed from the object manager upon the next loop.

This has proven useful when we add sounds as children to objects. Because as the object is destroyed, so are the sounds and any other children that are associated with it. However it is also a problem because even though a ship is destroyed, it’s torpedoes live on. So I think this method will surely not live inside the constructor. I think the best way to get rid of all useless sounds is to make their isAlive method return false when the sound is done playing.

Also I just noticed how cleverly (I think) I handled the sound issue. By making sounds into extensions of object class, I have simply been able to add sounds into the very same object container that holds all the other objects. Making sounds into children of objects, I am able to give sounds position data from their parents without relying on specific functionality in the object manager.

This seems to work extremely well, and the only downside is that right now sounds are not automatically garbage collected because their isAlive method always returns true. If I could simply make it return false when the sound is finished, garbage collecting sounds will be a piece of cake. Or rather, it will be done automatically since the function is already there.

Perhaps a similar method could be used for graphics. To create a Graphic or Animation extension of the object class, and assign this graphic as a child to whatever object it should be assigned to. Then make it’s obey parent obey the parents every move (not just match velocity), and we’re good to go. This might be the best idea I’ve had yet.

The cool thing is that with this method, we don’t need too much special functionality in the object manager, and we can also extend the Graphic object with more details such as multiple layers and whatnot.

Hungry. Must eat.

Comments are closed.