Skip to content

ECS Fundamentals

Entity-Component-System (ECS) is a way of structuring a game. It keeps three things apart: which thing you’re talking about (the entity), the data it holds (its components), and the rules that act on that data (systems).

An entity is just an ID for one thing in Rift, like a player, an NPC, a tree or a projectile. It has no data or logic of its own, and it does nothing until you attach components to it.

A component is a small piece of data about one trait, like Position, Equipment, Poisoned, Speaking or Skulled. Components only hold values, never logic. You attach them to an entity to make it into something.

A system is where the logic lives. It finds every entity that has a certain set of components and does something with them. All of your game’s rules live in systems.

Example: MovementSystem finds everything with both Position and Moving and advances it.

ECS won’t solve every problem, but it has some real advantages for games.

In ECS you say what you want to happen, and systems make it happen. You don’t write how an effect gets applied.

To hurt an entity you don’t reach in and write: hitpoints = hitpoints - 5

Instead, you attach a Damage(amount=5) component, and the system in charge of damage does the rest. You ask for damage instead of applying it yourself, so it works exactly the same no matter who asks.

An entity has no fixed type. It’s just the components it has. A player is anything with Position, Equipment and Hitpoints. A campfire is anything with Position and Flammable.

Any component can go on any entity, so you make new kinds of things by mixing small traits together, not by extending a class. There are no base classes, and nothing breaks when a thing needs to be two kinds of thing at once. In Rift you describe what an entity has, not what it is.

Components hold data and systems hold logic, and neither needs to know about the other. A system works on every entity that matches, no matter where it came from or what else it has.

To add a feature, you add a component and a system, and nothing that already exists has to change. PoisonSystem doesn’t need to know MovementSystem exists. They work on the same entities, but they don’t share any code. So your game grows by adding pieces, not by tangling old ones together.

Rift runs your systems in the same order every tick, on a consistent view of the world. So each tick depends only on the one before it.

World(N+1) = f(World(N))

Nothing hidden decides who went first. That makes your game easy to predict and easy to test, because the same inputs always give you the same world.

Any entity can have any component, so you only write a behaviour once and it works across the whole game. Write the rule that drains Hitpoints from anything with Poison, and everything can be poisoned: players, NPCs, objects in the world, and things you haven’t made yet.

To make something burn, freeze or bleed, give it the matching component. The system already knows what to do.

An entity is just a set of components, so a new kind of entity is data, not code.

A new monster is a list of components and values, like Hitpoints(120), Aggressive, Poisonous and Skulled. A designer can make one without writing any systems. Mix a handful of traits and you get hundreds of different monsters, without adding anything to the engine.