I just began using the component-entity design pattern for my game objects and on the general surface, the concept makes sense. But I continue to struggle with how to adequately connect the various components with one another in an effective fashion. I am treating entities are mere GUIDs and the components that an entity consists are held and managed by respective subsystems.
In a simple example, we have a component that stores the entity's position and orientation along with another that stores the mesh for rendering. Before the render component can be drawn by the rendering engine, the position and orientation are needed or default values would be used placing the mesh at the wrong location.
I don't want to impose any restrictions on the order of adding or creating components for an entity as I think the system should be capable of handling this situation. Therefore, when an entity is to be created, the entity subsystem invokes each component subsystem to create the necessary components. When components are first created by their subsystems, they are placed in a 'new component' list to avoid lock issues or state problems if entity creation is done in a separate thread from the main game loop. Once all components are attached to the entity, each component's OnInitialize() method is invoked. This method allows the component to do whatever initialization steps it needs to do before being thrown into the game world mix. This is where a component could invoke an immediate callback through the event subsystem to obtain things like position, which our render component would do. Once this step has finished, the entity subsystem dispatches a final event to the event subsystem to add the entity to the world that will be processed on the next game loop iteration. When the event subsystem gets this event, each subsystem is dispatched this event and notified so they can move the entity from the 'new component' list to the 'active component' list. At this point, each component is ready to go.
It seems to me then using this type of event system mechanism, the subsystems are what register event handlers and not the components. I guess because the logic being invoked are held in the subsystem classes themselves and the components are mostly data containers, this makes sense. But would there ever be a time where I would need a component to register with the event system? And then in that scenario, wouldn't I need for each component registration to consider it's entity guid so that the component isn't receiving notifications for other instances of that component for other entities?
Anyone have any thoughts to share on how to wire this up effectively?
Event Handling help with Components, Entities, and Systems
Moderator: Coders of Rage