Dev's NoxDev's Nox

Event API

Publish/subscribe event system for cross-mod communication.

The IEventAPI is a pub/sub event bus. Mods emit events and other mods subscribe to react. Access it via api.EventAPI.

Concepts

ConceptRole
Event nameA string key identifying the event (e.g., "user.login").
EmitFire an event with optional data.
SubscribeRegister a callback for an event name.
WeightExecution order — lower weights run first.
ChannelsContext filter (Main, Client, Editor, etc.).

Emitting Events

Simple emit

Emit(string eventName, params object[] data) — the quick form for most cases:

// Broadcast without reply
api.EventAPI.Emit("player.jump", 2.5f, Vector3.up);

// Broadcast with a reply callback as last argument
api.EventAPI.Emit("player.jump", 2.5f, Vector3.up,
    new Action<object[]>(reply => api.LoggerAPI.Log($"Replied with {reply[0]}")));

Internally, this is converted to an EventContext with default values:

Simple parameter→ EventContext fieldDefault
eventNameEventContext.EventName—
data (all but last)EventContext.Data—
last Action<object[]> argEventContext.Callbacknull if absent
(implicit)EventContext.ChannelEventEntryFlags.Internal
(implicit)EventContext.Destinationnull (broadcast)

If the last positional argument is an Action<object[]>, it is treated as the reply callback, not data.

Full context emit

Emit(EventContext context) — for advanced control over routing and replies:

var ctx = new EventContext {
    EventName = "user.login",
    Data = new object[] { userId, timestamp },
    Channel = EventEntryFlags.Main | EventEntryFlags.Client,  // only these contexts
    Destination = "nox.discord",                               // target a specific mod
    Callback = reply => api.LoggerAPI.Log("Acknowledged")      // get a response
};
api.EventAPI.Emit(ctx);

Use simple emit for broadcasts. Use full context when you need to filter by channel, target a specific mod, or handle a reply.


Subscribing to Events

Subscribe(string eventName, EventCallback callback)

The most common form. Returns an EventSubscription.

var sub = api.EventAPI.Subscribe("player.jump", data => {
    if (data.TryGet<float>(0, out var height))
        Debug.Log($"Player jumped {height}m");
});

Callback signature

public delegate void EventCallback(EventData context);

EventData gives you:

MemberDescription
EventNameThe event that triggered this callback.
Dataobject[] passed by the emitter.
SourceThe IMod that emitted the event.
SourceChannelWhich EventEntryFlags channel it came from.
TryGet<T>(index, out value)Type-safe access to Data[index].
Callback(params object[] args)Invoke a nested callback.

Unsubscribing

// By subscription object
api.EventAPI.Unsubscribe(sub);

// By UID
api.EventAPI.Unsubscribe(12345u);

// All subscriptions for an event name
api.EventAPI.UnsubscribeAll("player.jump");

// All subscriptions from this mod
api.EventAPI.UnsubscribeAll();

Event Channels (EventEntryFlags)

Filter events by context:

FlagValue
Main1
Client2
Instance4
Editor8
Server16
Custom32
InternalMain | Client | Editor | Server
AllEverything

Events are not networked by default. Use channels to control which context receives them.

Cross-Mod Communication

Events are the recommended way to communicate between mods without hard references:

// In Mod A — emit an event with data
api.EventAPI.Emit("player.jump", 2.5f, Vector3.up);

// In Mod B — listen for it
var sub = api.EventAPI.Subscribe("player.jump", data => {
    if (data.TryGet<float>(0, out var height))
        Debug.Log($"Player jumped {height}m");
});

// Always unsubscribe in OnDispose
public void OnDispose() {
    api.EventAPI.Unsubscribe(sub);
}

Prefer events over direct references. Events keep mods loosely coupled and independently testable.

On this page