This is the first in a series of posts about how to build an Entity Component System. Each post will cover a different part of the design and turn it inside out, so that at the end of each post you will have enough information to implement one yourself.
In this first entry I will go over the data structures required to find entities and which components they have. The next entry, Building an ECS: Archetypes and Vectorization, goes over how components can be stored in a cache efficient way.
The Entity Index
Entities are unique “things” in your game, often represented as a unique integer value. Entities can have components, which can be represented as a vector of component identifiers. The total set of components is the entity’s type (sometimes called an archetype).
For example, a first person shooter could have a “Player” entity that has components “Position” and “Health”. The type of this entity would be [Position, Health].
In an ECS we often need to know which components an entity has. A simple data structure that would allow for that is a map that stores a vector of component ids for each entity: