Appearance
Overview
ECS (Entity Component System) is an architectural pattern where computation is defined as a list of systems operating on a set of entities, each of which consists of a dynamic set of pure data components. Systems select the entities to process via means of persistent, efficient queries over the entities' component "shapes".
Here's a short glossary cribbed from the getting started guide (which you should probably read first):
- entities: an object with a unique ID that can have multiple components attached to it.
- components: different facets of an entity, e.g. geometry, physics, hit points. Data is only stored in components.
- systems: pieces of code that do the actual work within an application by processing entities and modifying their components.
- queries: used by systems to determine which entities they are interested in, based on the components attached to the entities.
- world: a container for entities, components, systems and queries.
The usual workflow when building an ECS based program:
- Create the component types that shape the data you need to use in your application.
- Create entities and attach components to them.
- Create the systems that will use these components to read and transform the data of entities selected by a query.
- Execute all the systems each frame.
Example
Let's say we want to create a game where the player fights with wolves and dragons. We will start by defining components that will be attached to entities:
Walker
andFlyer
for entities that will walk and fly (respectively).Enemy
for enemy entities.Model3D
for all the entities that will have a 3D Model.
Then we use these components to define our main entities:
wolf
: It's anEnemy
, canwalk
and has amodel3D
.dragon
: It's anEnemy
, canfly
and has amodel3D
.player
: It's anPlayer
, canwalk
and has amodel3D
.
And finally we define the systems that will add the logic to the game:
Walk
: It will modify theWalker
entities (Player
andWolf
) moving them around.Fly
: It will modify theFlyer
entities (Dragon
) moving them around in the sky.AI_Walk
: It will modify theEnemy
andWalker
entities (Wolf
) using AI techniques to compute the path they will follow.Attack
: It will implement all the logic for attacks betweenEnemy
andPlayer
entities.Draw
: It will draw all the entities that hasModel3D
component on the screen.