Syncing a data-oriented ECS with a stateful external system
Discusses strategies for synchronizing a data-oriented ECS with an external system that keeps state.
The simulation model in The Machinery is based around a data-oriented entity-component-system framework (ECS). In this model, components are just chunks of raw data, for example, a Transform Component can be represented as:
struct tm_transform_t { tm_vec3_t pos; tm_vec4_t rot; tm_vec3_t scl; };
An entity is a collection of such components. The set of components held by an entity determines its type. For example, a typical rendered object may have the components: (transform, link, mesh). Here, the Link Component links the entity to its parent and the Mesh Component references the mesh to be rendered.
In our system, an entity cannot have multiple components of the same kind — i.e. there can’t be two Transform Components in the same entity. Thus, an entity’s type can be fully identified by a bit mask of its components.
The component data for entities of the same type are stored together, in big blocks of raw memory. When algorithms work on entities of the same type, they can just walk over this memory linearly, which ensures that the operations are cache friendly.
Systems are algorithms that run on all entities that have a certain set of components. For example, the Velocity System