I'm working on an ECS model for a C++ game I'm developing. I feel like I've gotten a good grasp on the ECS model, however, I'm struggling to determine how I should actually organize and store the components. This question has been raised by the dilemma of how I want my engine to iterate over my components and systems.
Here are two routes I could see myself going:
1) Store all components within entities themselves. This model would involve me creating an object pool of an entity type. Each entity type would contain a specific set of components relevant to that entity (e.g. graphics component, physics component, health component, etc.)
I would then register this object pool with the appropriate system (e.g. physics system) which would then iterate over the objects as necessary.
Pros: My systems would only have to iterate over entities that are known to have relevant components. Since the entities contain the actual components, the act of initializing each entity is (subjectively) easier.
Cons: I need to register multiple object pools with each system (e.g. projectiles and enemies both have health components, thus I would need to register each object pool to the health system).
2) Store all components of the same type in a container and then give my entities references to these components. I would then register a single component container with a system (e.g. give the physics component array to the physics system). The system would then only need to iterate over a single container instead of multiple containers.
I'm envisioning that all components are held within their respective system (e.g. physics system contains an array of physics components). Each entity would then contain a reference (pointer or ID handle) to an individual component.
My entities would then essentially become objects that just contain references to components, but not the actual components themselves.
Pros: The entire concept or register object pools to each system becomes obsolete, and each system only has to iterate over a single container.
Cons: The process of initializing an entity becomes (subjectively) difficult. For example, to create a projectile, I would need to request individual components from each system. It then becomes possible that during the creation of an entity, I'm able to obtain one component but obtain another. Thus the entity is only partially created. I would need to account for all of these fail cases.
I'm curious what your thoughts are on either option?