📝(ECS) Cross-system communication via event entities
Entity Component System (ECS)
One of the approaches to cross-system communication in Entity Component System (ECS) is creating event entities. When something happens, a system may spawn an event entity into the world and other systems may detect and react to it.
Depending on the nature of communication, event entities can be collected:
by the emitting system on the next tick. This gives all systems the chance to see and react to the event. The emitting system doesn’t need to know about consumers, and is therefore decoupled from them.
by the consuming system when entity is processed. This creates tight coupling between systems and adding new consumers might be problematic.
by an independent system. Allows the flexibility to decide when to collect events.
This may be considered a pure ECS approach as it does not introduce any new concepts.
Events are represented in ECS, so the whole game state can be saved/loaded by saving/loading entities and components.
If you have tools to debug ECS, this makes events debuggable as well.
Because a system needs to run to detect new event entities, this may result in some systems processing events on the next tick. If these systems produce events of their own, this may lead to event chaining that spans multiple ticks.
This can be alleviated by careful system scheduling (scheduling consuming systems after producing ones).