Behavior in games is in many ways defined by the interaction between entities. A turret fires at an enemy, a player trades with another player, a hero

Building Games in ECS with Entity Relationships

submited by
Style Pass
2023-01-23 23:30:18

Behavior in games is in many ways defined by the interaction between entities. A turret fires at an enemy, a player trades with another player, a hero goes on a quest that in turn belongs to a faction, a spaceship docks to a space station orbiting a planet in a star system and so on.

However common it is, these relationships between entities are rarely supported as first class citizens by a game engine or framework. Sure, you can store a reference to an Actor or GameObject, but what happens if that entity is deleted? What if my game needs to find all players that I’m trading with, and are allied with one or more of my enemies?

Code that lets you do these things is not fun to write and has a high risk of being thrown out as game mechanics are scrapped or new ones are introduced. This is where ECS relationships come in (if you don’t know yet what an Entity Component System is, check out the ECS FAQ).

Entity relationships are a fast, deeply integrated mechanism for creating entity graphs and graph queries, so applications don’t need to write bespoke data structures. While they may look complex to implement at first glance, they mostly reuse and tweak features that already exist in an ECS.

Leave a Comment