I'm creating a component-based game object system. Some tips:
GameObjectis simply a list of
- There are
GameSubsystems. For example, rendering, physics etc. Each
GameSubsystemcontains pointers to some of
GameSubsystemis a very powerful and flexible abstraction: it represents any slice (or aspect) of the game world.
There is a need in a mechanism of registering
GameObject is created and composed). There are 4 approaches:
- 1: Chain of responsibility pattern. Every
Componentis offered to every
GameSubsystemmakes a decision which
Componentsto register (and how to organize them). For example, GameSubsystemRender can register Renderable Components.
Components know nothing about how they are used. Low coupling. A. We can add new
GameSubsystem. For example, let's add GameSubsystemTitles that registers all ComponentTitle and guarantees that every title is unique and provides interface to quering objects by title. Of course, ComponentTitle should not be rewrited or inherited in this case. B. We can reorganize existing
GameSubsystems. For example, GameSubsystemAudio, GameSubsystemRender, GameSubsystemParticleEmmiter can be merged into GameSubsystemSpatial (to place all audio, emmiter, render
Components in the same hierarchy and use parent-relative transforms).
con. Every-to-every check. Very innefficient.
Subsystems know about
- 2: Each
Componentsof specific types.
pro. Better performance than in
Subsystems still know about
Componentregisters itself in
GameSubsystem(s). We know at compile-time that there is a GameSubsystemRenderer, so let's ComponentImageRender will call something like GameSubsystemRenderer::register(ComponentRenderBase*).
pro. Performance. No unnecessary checks as in
Components are badly coupled with
- 4: Mediator pattern.
GameSubsystems) can implement registerComponent(Component*).
GameSubystems know nothing about each other.
con. In C++ it would look like ugly and slow typeid-switch.
Which approach is better and mostly used in component-based design? What Practice says? Any suggestions about implementation of