TExcelTAD
Home › Game Engine Architecture in C++ › Entities, Components and Systems

Entities, Components and Systems

Early engines modelled game objects with class hierarchies: GameObject → Actor → Pawn → Character → PlayerCharacter. It reads well on a whiteboard and turns painful in year two. A flying turret that can be picked up needs bits of three branches, behaviour gets pushed up into base classes "just in case", and every object carries fields it never uses.

An Entity Component System (ECS) splits the object into three ideas:

Part What it is Example
Entity an ID, nothing more 42
Component plain data attached to an entity Position{3, 4}, Health{80}
System a function that runs over every entity with a given set of components "move everything that has Position and Velocity"

What an entity is comes from which components it has, not from its class. A turret is Transform + Turret + Health; add Pickup and it can be carried. No new class is needed. The entities and components lesson in the roadmap course shows the gameplay side of this idea. Here we look at it as an engine programmer: how the data is laid out and why that matters.

Inheritance first

Here is the hierarchy version. Each object is allocated separately and updated through a virtual call.

cpp · try it yourself
#include <iostream>
#include <memory>
#include <string>
#include <vector>

struct GameObject {
    std::string name;
    explicit GameObject(std::string n) : name(std::move(n)) {}
    virtual ~GameObject() = default;
    virtual void update(double dt) = 0;
};

struct Mover : GameObject {
    double x = 0, vx = 0;
    Mover(std::string n, double v) : GameObject(std::move(n)), vx(v) {}
    void update(double dt) override { x += vx * dt; }
};

struct Turret : GameObject {
    int ammo = 3;
    using GameObject::GameObject;
    void update(double) override { if (ammo > 0) ammo--; }
};

// A moving turret: which base class? Copy the code? Multiple inheritance?
struct MovingTurret : Mover {
    int ammo = 3;
    using Mover::Mover;
    void update(double dt) override { Mover::update(dt); if (ammo > 0) ammo--; }
};

int main() {
    std::vector<std::unique_ptr<GameObject>> world;
    world.push_back(std::make_unique<Mover>("crate", 2.0));
    world.push_back(std::make_unique<Turret>("tower"));
    world.push_back(std::make_unique<MovingTurret>("tank", 1.0));
    for (int frame = 0; frame < 2; frame++)
        for (auto& obj : world) obj->update(0.5);
    for (auto& obj : world) std::cout << obj->name << " updated\n";
    std::cout << "sizeof(Mover) = " << sizeof(Mover) << ", sizeof(MovingTurret) = " << sizeof(MovingTurret) << "\n";
}
crate updated
tower updated
tank updated
sizeof(Mover) = 56, sizeof(MovingTurret) = 64

The ammo logic now exists twice, and MovingTurret had to pick a side. Every object also lives in its own heap block, so the update loop jumps around memory.

The same world as components

In an ECS, each component type lives in its own array, and systems loop over those arrays. The simplest possible version uses one std::vector<std::optional<T>> per component, indexed by entity ID:

cpp · try it yourself
#include <iomanip>
#include <iostream>
#include <optional>
#include <string>
#include <vector>

using Entity = std::size_t;

struct Position { double x; };
struct Velocity { double vx; };
struct Turret   { int ammo; };
struct Name     { std::string text; };

struct World {
    std::vector<std::optional<Position>> positions;
    std::vector<std::optional<Velocity>> velocities;
    std::vector<std::optional<Turret>> turrets;
    std::vector<std::optional<Name>> names;

    Entity create(std::string name) {
        positions.emplace_back(); velocities.emplace_back(); turrets.emplace_back();
        names.emplace_back(Name{std::move(name)});
        return names.size() - 1;
    }
};

// Systems: plain functions over the component arrays.
void movementSystem(World& w, double dt) {
    for (Entity e = 0; e < w.positions.size(); e++)
        if (w.positions[e] && w.velocities[e]) w.positions[e]->x += w.velocities[e]->vx * dt;
}

void turretSystem(World& w) {
    for (auto& t : w.turrets)
        if (t && t->ammo > 0) t->ammo--;
}

int main() {
    World w;
    Entity crate = w.create("crate");
    w.positions[crate] = Position{0}; w.velocities[crate] = Velocity{2};
    Entity tower = w.create("tower");
    w.positions[tower] = Position{5}; w.turrets[tower] = Turret{3};
    Entity tank = w.create("tank");
    w.positions[tank] = Position{0}; w.velocities[tank] = Velocity{1}; w.turrets[tank] = Turret{3};

    for (int frame = 0; frame < 2; frame++) { movementSystem(w, 0.5); turretSystem(w); }

    std::cout << std::fixed << std::setprecision(2);
    for (Entity e = 0; e < w.names.size(); e++) {
        std::cout << w.names[e]->text << ": x=" << w.positions[e]->x;
        if (w.turrets[e]) std::cout << " ammo=" << w.turrets[e]->ammo;
        std::cout << "\n";
    }
}
crate: x=2.00
tower: x=5.00 ammo=1
tank: x=1.00 ammo=1

The tank is just "an entity with a turret and a velocity". Adding a Pickup component later touches no existing code. This toy version wastes memory (every entity has a slot in every array) and checks every entity in every system. The next two lessons fix that in the two ways real engines do: sparse sets and archetypes.

Why engines like this layout

  • Cache-friendly loops. A system touching Position and Velocity reads two tightly packed arrays instead of chasing pointers. See Cache Behavior: AoS vs SoA.
  • Composition at runtime. Designers add and remove components in data files; no recompilation.
  • Easy parallelism. A system declares what it reads and writes, so two systems that touch different components can run on different threads (see Task Graphs).
  • Serialization for free-ish. Components are plain structs, so saving a level is "write each array".

What an ECS costs

Concern Reality
Learning curve Behaviour is spread over systems; "where does the player jump?" needs a search, not a class name
Relationships Parent/child links, references between entities and "the one player" need extra patterns (handles, tags, singletons)
Small games A few hundred objects run fine with plain classes; the speed win only shows at scale
Ordering Systems run in an explicit order that you must design and document

Note: "ECS" is used loosely. Unity's GameObject + MonoBehaviour and Unreal's AActor + UActorComponent are component-based but not ECS: the components hold behaviour and are separate objects. Unity DOTS (Entities), EnTT, flecs and Bevy are ECS in the strict sense: data-only components plus systems.

Tip: You don't have to go all in. Many engines keep a classic scene of objects for gameplay and use ECS-style arrays only for the hot paths: particles, crowds, projectiles, physics bodies.

Test yourself

In a strict ECS, what is an entity?

  1. A base class all game objects inherit from
  2. An identifier that components are attached to
  3. A system that updates components
  4. A heap-allocated object with virtual methods

Which change is easiest in an ECS but awkward in a deep class hierarchy?

  1. Making a moving turret from movement and turret behaviour
  2. Calling a virtual function
  3. Storing a name string
  4. Deleting an object

Why are ECS systems usually faster over many entities?

  1. They use more threads by default
  2. They iterate tightly packed component arrays instead of chasing pointers to separate objects
  3. Components are always smaller than classes
  4. Virtual calls are free in an ECS