Hey,
I’m a bit overwhelmed when it comes to cleaning up objects with complex dependencies and would like to ask you all what your design choices are when it comes to creating and removing objects or scenes cleanly.
I’ve found various issues regarding removing nodes and cleaning up things, but I’d like to know if there are any recommended design choices for how to handle object/scene removing, level changing etc.
There are many good tips here in the manual, but it doesn’t fully answer my questions.
For example, I would like to cleanly remove an NPC which has many complicated dependencies (pathfinding, collision, IK joints…).
My approach would be to let every class throughout the project remove what it created itself (NodePaths, tasks, custom class instances…), however this isn’t always straight forward:
- I have a factory class to create the NPC (to split the NPC setup process from the later NPC updating). Then the factory class should probably also remove the NPC? However in this case the NPC would probably have to call the factory’s “removeNPC()” function, which creates an (ugly) cyclic dependency. Alternatively, the NPC could not know at all that it is being removed, which is also strange.
- To add and remove objects to/from the bullet collision, I need to have access to the BulletWorld. Passing instances of the BulletWorld to each object’s “remove()” member function (where it is used to remove the object from the BulletWorld) seems ugly. Having the scene or factory class remove the object from the BulletWorld seems even uglier, since then the Scene/Factory needs to know too much about the object’s internal structures.
- How do you cleanly handle references between objects, i.e. if a missile object is following an NPC while the NPC is being deleted? I’d hate for the NPC to have to hold a reference to the missile object in order to tell it that it’s being deleted…