I'm writing an entity component system and want to store each type of component separately and contiguously in memory in a way that's easy to iterate over.
Component is a simple base class from which like classes like MoveComponent derive.
I store components inside the World class:
struct World { ... std::vector<Component> components; }; This would work fine if there was only one type of component, just a Component class. The problem is I don't know how to handle component subclasses. For example every MoveComponent should be stored contiguously in memory in a way that I can easily iterate over and process them.
I want to avoid this kind of copy-paste junk code for hopefully obvious reasons:
struct World { ... std::vector<MoveComponent> move_components; std::vector<PhysicsComponent> physics_components; etc. }; I'd like to instead structure it in a way that permits iterating over each component of a certain type in a World like, for example, this (inspired by Overwatch's entity component system video, see here):
for (MoveComponent *mc : ComponentIterator<MoveComponent>(world)) { ... } I'm at a loss as to how to implement this.