Retrieving components from game objects (entities)
I'm working on an entity-component-system game at the moment. I've looked in particular at the Artemis framework and a c# port of it but I am writing my own implementation of the pattern.
I'm puzzled as to how components are retrieved from the EntityManager. As far as I can tell I have two options:
- Assign each type of component a static ID, that is used as the index of the component (my entities are held as EntityComponent[entityID][componentIndex]) but I'm concerned that in a situation where an entity has a component of ID 9, that I will then have size the array to 10 and have 9 empty elements preceding the sole component, consuming memory unnecessarily.
- Alternatively I could write a generic method e.g EntityManager.GetComponent(int entityID) but this would require a look up or OfType<> call against the entities components.
Surely there is a more efficient way. What am I missing? Artemis includes a ComponentMapper class but I struggle to understand what it does. I want to ensure the basics of the system work as efficiently as possible. Many thanks in advance for any feedback.