In Component based game engine, I have trouble when it come to relation between game objects / components.
Where to store the relation , and how to delete them?
To keep it simple, here is an example.
A Gradius-clone game consists of 2 types of game object:-
- Rocket = Main Body Component + Turret Component
- Floating turret = Turret Component (alone)
The component detail are :-
- Main Body Component = one physical body + one graphical body
- Turret Component = one physical body + one graphical body
Turret Component is designed to have a relation (physical constraint or ownership) with to Main Body Component.
There is a requirement that the relation must be removed BEFORE both component.
- For example, the relation in this case also contains physical constraint implemented by external Physic Library, e.g. Bullet Physics.
These are quite an example-specific description, provided just in case ....
Steps to delete Rocket have to be in this order :-
- delete constraint
- delete Main Body Component and Turret Component (any order is ok)
The relation should also be removed when a Main Body Component is deleted, leaving Turret Component alone, so let it become Floating turret.
Where should the relation be stored? (All seem ok, but related to next question.)
- inside a new component in a new dedicated game object (new entity)
- inside a new component in the same entity as Rocket
- inside a new manager system that keep a list of this specific kind of relation
How should relation be deleted? (Both seem to be bad ideas.)
create a flag to check for a impending deletion of Main Body Component or Rocket, then call a new dedicated system to delete relation just before delete others component, it must be called before other manager systems every time step.
let's other existing manager call a new dedicated system when it want to delete Main Body Component or Rocket
I expected answers for general cases where there are a lot of types of relation. (between Game Objects or Components)
Edit 1: The proposed solution about making ownership and adding code in Rocket's destructor directly is quite against Component Based design.
- It will make the three components (include constraint) very couple.
- Furthermore, components should not have non-trivial function.
I believe the destructor is one of them, that should be avoided.
(I once has bloating destructor of some objects, it destroy all good modularity.)