Building an ECS #1: Where are my Entities and Components

<p>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.</p> <p>In this first entry I will go over the data structures required to find entities and which components they have. The next entry,&nbsp;<a href="https://ajmmertens.medium.com/building-an-ecs-2-archetypes-and-vectorization-fe21690805f9" rel="noopener">Building an ECS: Archetypes and Vectorization</a>, goes over how components can be stored in a cache efficient way.</p> <h2>The Entity Index</h2> <p>Entities are unique &ldquo;things&rdquo; 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&rsquo;s type (sometimes called an archetype).</p> <p>For example, a first person shooter could have a &ldquo;Player&rdquo; entity that has components &ldquo;Position&rdquo; and &ldquo;Health&rdquo;. The type of this entity would be [Position, Health].</p> <p>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:</p> <p><a href="https://ajmmertens.medium.com/building-an-ecs-1-where-are-my-entities-and-components-63d07c7da742"><strong>Click Here</strong></a></p>