How to manage game object hierarchy in an entity component?

How to manage game object hierarchy in an entity component?

Updating the transforms involves following the parent hierarchy up, which does not access memory in a cache-friendly manner. This means I need to implement some kind of dirty state. So we basically have a two stage “dirty bit propagation” requirement. Breaking it down like this makes it fairly straightforward to implement with a pair of systems.

How to track which components an entity has?

Since an entity is simply an ID, we need a way to track which components an entity “has”, and we also need a way to track which components a system cares about. I chose the very simple approach of using a std::bitset (modern C++ equivalent of a bitfield), called a Signature.

How to create a simple entity component system?

We need to create a data structure that is essentially a simple array, but is always a packed array, meaning it has no holes. If an entity is just an index into an array of components, then it’s simple to grab the relevant component for an entity, but what happens when an entity is destroyed?

What is the signature of an entity component system?

As an example, if Transform has type 0, RigidBody has type 1, and Gravity has type 2, an entity that “has” those three components would have a signature of 0b111 (bits 0, 1, and 2 are set). A system would also register its interest in certain components as another signature.

How are components shared between systems and entities?

Some components (like Position) are shared between systems. Other components can be private to a particular system, which allows for information hiding and greater optimization in memory layouts. For example, let’s say that one scene in the game contains a thousand entities.

How does an entity mix and match components?

Each entity can mix and match components as necessary to build up the behavior required. In this version, often the components and necessary bits of data are paired together: each component by itself can still be a class with encapsulated data (but doesn’t have to be ).

What kind of data does an EC entity have?

In some EC architectures, all entities share some common bits of data such as transform information such as position, rotation and scale, or some basic state data about whether the entity is currently “alive” in the scene. Unity GameObjects are one example of this style.