Entity Component System Basics

entity component system explained

You add a flying enemy, then a flying enemy that shoots, and the class tree starts to sprawl. Entity component system explained briefly: entities are identities, components are plain data, and systems are the behaviour that processes every entity with the right components.

The pattern is easiest to judge by refactoring a small enemy hierarchy, which this article does below. It is one of several building blocks in 2d game programming for beginners, and it pays off only under certain conditions.

The three parts and what each one does

An entity component system (ECS) splits what used to live in one game object into three parts. Unity’s Entities documentation puts it directly: an ECS architecture separates identity (entities), data (components) and behaviour (systems). Meta’s Spatial SDK describes the same split, noting that the pattern breaks down every object in a scene into three parts.

A running game scene is a set of game objects; in an ECS each one becomes an entity whose behaviour comes from the components attached to it.

Entity: identity and nothing else

An entity is an object that groups related components together. In Meta’s description it is a lightweight container with no data of its own. In practice you can treat it as an id. A player, a bat and a coin are all just ids until components are attached to them.

Component: data without behaviour

A component stores data and defines what an entity is or has. A component is a container object without complex logic, such as a health value held as an integer. Position, velocity, health and sprite are typical components. Methods that move or damage things do not belong here.

System: behaviour applied to matching entities

Systems contain all the logic. Richard Lord, an author of entity system frameworks, summarises the split as all the data in the components and all the logic in the systems. A movement system, a rendering system and a damage system each do one job and know nothing about enemies or players as types.

Four common misunderstandings

Common assumption What ECS actually does Planning use
An entity is an object with methods An entity only groups components and carries no data of its own Keep entities as ids or thin handles, and put nothing else in them
A component can hold its own update logic Components store data; systems contain the logic If a component grows a method, move that code into a system
A system belongs to one kind of entity A system queries for entities with specific component combinations and processes their data Write systems around components, not around enemy or player types
ECS is a deeper class hierarchy Entities are composed from simple components instead of complex class hierarchies Add behaviour by attaching a component, not by subclassing

These contrasts come from Meta’s Spatial SDK ECS documentation and Unity’s Entities manual.

Refactoring an enemy hierarchy into components

The clearest argument for ECS is a hierarchy that has outgrown its tree. The refactor below shows why games lean on composition instead of inheritance.

The inheritance version

Start with an Enemy class that has a position, health and an update method. Flyers need different movement, so you add FlyingEnemy. Then a design asks for an enemy that flies and fires projectiles, and you add ShootingFlyingEnemy. A walking shooter would need yet another class, or duplicated shooting code. Every new combination of traits adds a class, and the tree cannot express a trait shared across branches without copying code or restructuring the parents.

Composition over inheritance

Composition over inheritance means building an object from small parts it has rather than from a chain of parents it extends. Meta’s documentation states the ECS version of the idea: instead of creating complex class hierarchies, compose entities from simple components. Applied to the enemies, the classes disappear and a list of components takes their place:

A loose analogy: one set of rules applied to many different pieces on a board, much as a system applies one behaviour to every entity that matches its query.
  • Position and Health: every enemy carries these, so the damage and rendering systems can treat them uniformly.
  • Velocity: attached to anything that moves, so a movement system handles walkers and flyers alike.
  • Flying: a marker component that tells the movement rules to ignore ground constraints.
  • Shooter: data for firing, such as a cooldown, read by a shooting system. A flying shooter is an entity with both Flying and Shooter, with no new class required.

Meta also advises that each component should represent one concept and each system should handle one behaviour. That guidance is why Flying and Shooter stay separate instead of merging into a single FlyingShooter component.

The table view of entities

It helps to picture the world as a table. Each row is an entity id, each column is a component type, and a blank cell means the entity lacks that component. A bat has Position, Velocity, Health and Flying. A coin has Position and perhaps a pickup component, and nothing that moves. Because the table is only data, you can inspect it, save it and change a row without touching any class definitions.

A circuit board with chips and surface-mount components; no table or data layout is visible.

How a system finds the entities it needs

A system does not hold a list of enemies. It asks for every entity that has a particular set of components. Meta’s description is that systems query for entities with specific component combinations and process their data. A movement system asks for entities with both Position and Velocity, so coins without Velocity are skipped automatically.

Unity’s implementation makes the query explicit. To identify which entities a system should process, you use an EntityQuery, which searches archetypes (groups of entities sharing the same component set) for matching requirements. A query can use All, Any and None categories, and an archetype must contain all of the component types in the All category to match. The same documentation explains that systems transform data from an input state to an output state by reading streams of component data.

A simple ECS in JavaScript

A small browser game does not need a library to try the idea. Components can be plain objects stored in maps keyed by entity id, and a system is a function that loops over one map and looks up the others. The sketch below is an illustration of the pattern, not a production framework.

const createEntity = () => Symbol();
const positions = new Map();
const velocities = new Map();

const bat = createEntity();
positions.set(bat, { x: startX, y: startY });
velocities.set(bat, { x: speedX, y: speedY });

function movementSystem(dt) {
  for (const [id, vel] of velocities) {
    const pos = positions.get(id);
    if (!pos) continue;
    pos.x += vel.x * dt;
    pos.y += vel.y * dt;
  }
}

The loop over velocities plus the positions.get check is a hand-written query for entities that have both components. Adding a shooting system means adding a shooters map and another function, and the movement code stays unchanged. Call each system once per frame inside your game loop, in an order you choose.

Where ECS is used and why it is data-oriented

ECS appears in engines and frameworks of very different sizes. Unity’s Entities package is built on it, and Meta’s Spatial SDK uses the pattern for objects in a spatial scene. A Game Developer article notes that big companies like Unity, Epic or Crytek incorporate this pattern into their frameworks. Richard Lord’s overview likewise points to Unity as a well-known example of growing popularity for entity systems.

Entity system frameworks follow a data-oriented paradigm in which game objects are not self-contained OOP objects, as Lord explains. A game object is no longer a thing with its own methods and fields. It is a row of data that systems read and write. That shift in thinking matters more than any particular library.

When ECS is more than a small game needs

ECS is overkill when your game has few object types and the inheritance tree is not fighting you. A single-screen arcade game with a player, one enemy kind and some pickups gains little from separating identity, data and behaviour. Plain classes or a few object literals will be quicker to write and easier to read.

Slot machines on a casino floor with on-screen bonus graphics and a player in the foreground.

Reach for ECS when the signs of strain appear:

  • Trait combinations multiply: new enemy variants keep requiring new subclasses, as in the flying shooter example above.
  • Behaviour must be shared across unrelated objects: the same movement or damage rules apply to enemies, projectiles and pickups.
  • Objects change what they are during play: in Unity’s model you can change an entity’s archetype by adding or removing components at runtime, which a fixed class cannot do.

Lord argues that entity systems simplify game architecture and encourage a clean separation of responsibilities, which is the benefit to weigh against the extra structure. Meta’s adoption advice suits a small project well: start simple with basic entities and gradually add complexity. Convert one troublesome group of objects first, such as enemies, and leave the rest alone.

Adopting ECS also changes the questions you ask next. Handling screens, pausing and restarting becomes a matter of game state management over your component data. A collision system that tests every pair of entities is the point to look at quadtree collision detection.

Frequently asked questions

Question Answer
Can I use ECS with Unity GameObjects? Yes. Unity’s documentation says you can use GameObjects and MonoBehaviours and create a conversion system to map them to entities, so adoption does not have to be all at once.
Can an entity change what it is while the game runs? Yes. Adding or removing components at runtime changes the entity’s archetype, so a walking enemy can gain a Flying component without a new class.
Is ECS the same as data-oriented design? Not exactly. ECS follows a data-oriented paradigm, because game objects are not self-contained OOP objects, but data-oriented design is the wider approach that ECS applies to game objects.
Should a component ever contain logic? No. Components are plain data containers without complex logic, and systems hold the behaviour. One concept per component and one behaviour per system keeps both easy to reuse.