Skip to content

Adding Custom Events

Aaron Wilson edited this page Nov 29, 2022 · 1 revision

Adding Custom Events

The built-in game event handler specifies a range of events. However, it's nearly guaranteed that an extension of URF will need custom events. Here's a pattern that you can use to implement them.

First, create your custom events:

public class FooEvent : IGameEvent {
  // event data here...
}

public class BarEvent : IGameEvent {
  // event data here...
}

Then, create an interface to extend IEventHandler to handle your new events:

public interface ICustomEventHandler : IEventHandler {
  void HandleFooEvent(FooEvent foo);
  void HandleBarEvent(BarEvent bar);
}

Third, override IGameEvent.Visit in your new events to accept an ICustomEventHandler:

public class FooEvent : IGameEvent {
  // ...
  public void Visit(ICustomEventHandler handler) {
    handler.HandleFooEvent(this);
  }
}

public class BarEvent : IGameEvent {
  // ...
  public void Visit(ICustomEventHandler handler) {
    handler.HandleBarEvent(this);
  }
}

By default, these custom events will be ignored by the base handler.

Clone this wiki locally