What's an Entity System?

("Component/Entity System", "CES")

Component/Entity Systems are an architectural pattern used mostly in game development. A CES follows the Composition over Inheritance principle to allow for greater flexibility when defining entities (anything that's part of a game's scene: enemies, doors, bullets) by building out of individual parts that can be mixed-and-matched. This eliminates the ambiguity problems of long inheritance chains and promotes clean design. However, CES systems do incur a small cost to performance.

Concept

A game or application making use of the C/ES pattern involves at least the following:

  1. Entity - A container into which components can be added, usually hierarchical (any Entity can have sub-Entities).
  2. Component - Class of objects through which behaviors, looks and data can be added to an entity.

Whereas in an inheritance-based design, an enemy may have a complex inheritance chain such as Damageable <- Actor <- GroundBasedActor <- Enemy <- ZombieEnemy, in a C/ES-based design you might have a Health component, a GroundBasedMovement component and a ZombieAi component added to an entity named ZombieEnemy.

This eliminates ambiguity issues encountered in an inheritance-based design (should the root be Actor <- DamageableActor or Damageable <- Actor? Should items be implemented twice as PickupActors or DamageablePickupActors?). At the same time, components can be freely combined: a damageable inventory item? add the Collectable and Health components to an entity.

In a pure CES design, entities will not even have positions in the scene. Their location could be provided by an optional Transform or Location component. Their looks (3D model, sprite, icon) are also components.

Rendering and updating may take place either by traversing the entire entity tree (slow), by having entities register themselves to the respective services (service locator hell) or in a hybrid approach where the renderer/clock system remember which components they found in the tree while entities merely provide a change notification if components are added to them or removed from them, allowing the renderer/clock system to re-check that part of the tree (complex). This aspect of the design is however not part of the C/ES pattern.

More Information

A well-known game engine implementing a Component/Entity System is Unity. Its GameObjects are entities with a Transform component built-in and the engine user can write his/her own components in UnityScript, C# or Boo.

The navbar on the left has links to get you started, and a large selection of source code examples.

See also: ES Terminology for a list of terms used within Component/Entity Systems.

We also have a list of basic ES approaches