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:
WalkerandFlyerfor entities that will walk and fly (respectively).Enemyfor enemy entities.Model3Dfor all the entities that will have a 3D Model.
Then we use these components to define our main entities:
wolf: It's anEnemy, canwalkand has amodel3D.dragon: It's anEnemy, canflyand has amodel3D.player: It's anPlayer, canwalkand has amodel3D.
And finally we define the systems that will add the logic to the game:
Walk: It will modify theWalkerentities (PlayerandWolf) moving them around.Fly: It will modify theFlyerentities (Dragon) moving them around in the sky.AI_Walk: It will modify theEnemyandWalkerentities (Wolf) using AI techniques to compute the path they will follow.Attack: It will implement all the logic for attacks betweenEnemyandPlayerentities.Draw: It will draw all the entities that hasModel3Dcomponent on the screen.
