Entity Component Systems: Performance isn't the only benefit

Many companies, like Unity and Apple are using Entity Component Systems (ECS) to make games because having tightly packed data leads to cache efficiency and great performance. ECS isn’t just great for performance though: it leads to good code-decoupling, easy composition, and lends itself to TDD and automated testing.

Entity Component Systems are Easy to Compose

In the standard Object Oriented Programming (OOP) approach to writing games, you often create extremely complex and deep inheritance trees; a Player class might inherit from a SceneObject, and that SceneObject might inherit from a Transform class, and that Transform class might rely on a PlayerManager class to even be instantiated. This can be quite complex to understand and write tests for.

In ECS this is modeled by using composition. The entity (E) is the ID of an entity, components (C) are data associated with that entity, and then systems (S) operate on and modify groups of components. You then build up functionality by adding multiple components to an entity, and creating systems that look for groups of components.

Example pseudocode:

1PositionComponent {
2 x, y, z
3}
4
5WindComponent {
6 //No data - just used as a tag
7}
8
9WindSystem {
10 update() {
11 world.each(position: PositionComponent, wind: WindComponent) {
12 position.x++;
13 }
14 }
15}
16
17//Add entities to the world
18playerEntity = world.addEntity(PositionComponent());
19enemyEntity = world.addEntity(PositionComponent(), WindComponent());
20
21//Add systems
22world.addSystem(WindSystem());
23
24while(true) {
25 //Update all the systems each frame
26 world.systems(system) {
27 system.update();
28 }
29}

In this example code above the enemy entity will continuously move every frame while the player entity will not. This is because the WindSystem is looking for entities that have both a PositionComponent and a WindComponent. The great part about using composition is it's easy to add and remove functionality from entities.

So, if something happened that caused us to want the player to be affected by wind as well, we could simply add the WindComponent to the player entity.

Example pseudocode:

1world.addComponent(playerEntity, WindComponent());

We can also make the system itself slightly more complex by removing the WindComponent if a component reaches the side of the screen.

Example pseudocode:

1WindSystem {
2 update() {
3 world.each(position: PositionComponent, wind: WindComponent) {
4 if (position.x > 100) {
5 world.removeComponent(wind)
6 } else {
7 position.x++;
8 }
9 }
10 }
11}

These examples are slightly simplified, however even real systems tend to be small and focused because they only have one job to perform. This makes them easy to understand and reason about.

Entity Component Systems are Easy to Test

ECS also helps when it comes to automated testing. In OOP our class might have a model, animation, and all sorts of other data attached to it that we don’t necessarily want to load for every test. Whereas with composition we can just create the components and systems we are currently testing.

  • Example tests that we could write for our WindSystem include:
  • Entities with PositionComponent and WindComponent move the expected amount per update
  • Entities without the WindComponent don't move
  • Entities lose the WindComponent after x > 100

These tests are easy to write and fast to run since they don’t load or run any unnecessary logic.

Example pseudocode:

1test(‘Entities without the WindComponent dont move’) {
2 position = PositionComponent();
3 world.addEntity(position);
4 world.addSystem(WindSystem());
5
6 startingPosition = position;
7 world.update();
8 expect(position == startingPosition);
9}

Conclusion

ECS has a lot of benefits outside of performance that can really help create better code that is easier to reason about and modify. We’ve started using it at ClassDojo for some projects and are really enjoying it.

Does this sound like a better way to create games? Come join us in ClassDojo Engineering, we’re hiring!

Michael Graves

Michael is a game engineer at ClassDojo and a lifelong gamer. He enjoys solving problems, improving code quality, and figuring out complex systems.

    Next Post
    Previous Post