Search code examples
relationshipgame-enginecomponent-based

Component based game engine : How to manage relation between game object?


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:-

  1. Rocket = Main Body Component + Turret Component
  2. Floating turret = Turret Component (alone)

The component detail are :-

  1. Main Body Component = one physical body + one graphical body
  2. 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 :-

  1. delete constraint
  2. 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.)

  1. inside a new component in a new dedicated game object (new entity)
  2. inside a new component in the same entity as Rocket
  3. 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.)

  1. 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.

  2. 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.)

Solution

  • In your example of the Rocket game object, the relation is an ownership and should be kept in the Rocket. If your game engine architecture allows it, the relation can be deleted by a destructor function in the game objects called by the manager system prior their deletion.

    EDIT: If you don't want the objects or components to have any knowledge of their ownership or relations, I think the best choice would be to keep a list ( as you suggested) of the relation with a reference to the owner of the relation. That way you can check the list first, to see if your game object (Rocket) can be deleted or if it has any relation/s to delete first.