Skip to main content TerryFunggg Blog

Event System

During ECS system design . We may need to handle how systems communicate each other.

Two common ways to handle events on System.

Passive Check

cpp code snippet start


class CollisionSystem {
  void Update(){
      //.../
      if(collisionHappened) {
        eventBus->EmitEvent<CollisionEvent>();
      }
  }
}

class DamageSystem {
  void Update() {
    for (auto e: eventBus->GetEvents<CollisionEvent>() {
      //......
    }
  }
}

cpp code snippet end

Simple, keep the logic inside the System. Like CollisionSystem only handle object collision.

The order running the Systems is matter. For example: CollisionSystem should be run before DamageSystem.

It need to wait certain System check and process.

Blocking

cpp code snippet start


 class CollisionSystem {
   void Update(){
       //.../
       if(collisionHappened) {
         eventBus->EmitEvent<CollisionEvent>(a, b);
       }
   }
 }

 class DamageSystem {
   eventBus->SubscribeToEvent<CollisionEvent>(onCollision);

   void onCollision(Entity a, Entity b) {
      //....
   }
}

cpp code snippet end

Like above example: If object collison happen, fire eventBus event to any System subscribed CollisionEvent.