Skip to content

Event Bus

The event bus is the easiest way to attach behavior to CampAPI hooks.

Use ModuleScope for automatic cleanup

ModuleScope groups listeners together. When the scope goes away, its listeners are removed automatically.

cpp
CampAPI::ModuleScope scope;

Add a listener

cpp
scope.AddListener<BeforeRenderScreenEvent>([](BeforeRenderScreenEvent& event) {
    event.Cancel();
});

Listener order

Listeners run in the order they were registered. If you need the opposite order, ReverseInvoke() is available on the bus.

What this is useful for

  • blocking a render hook
  • consuming input so another handler does not use it
  • observing client lifecycle events
  • wiring up startup and shutdown behavior for a mod

Example: stop a screen from rendering

cpp
CampAPI::ModuleScope scope;

scope.AddListener<BeforeRenderScreenEvent>([](BeforeRenderScreenEvent& event) {
    if (event.isDebugOrToastScreen) {
        return;
    }

    event.Cancel();
});

Example: handle a key press

cpp
CampAPI::ModuleScope scope;

scope.AddListener<OnKeyEvent>([](OnKeyEvent& event) {
    if (event.isKeyDown && event.keyCode == 66) {
        event.Consume();
    }
});

When to use cancellable events

Use cancellable events when your code should be able to stop the default action.

cpp
scope.AddListener<CancelableClientInstanceEvent>([](CancelableClientInstanceEvent& event) {
    event.Cancel();
});