Nexus implements an event system, allowing you to subscribe to both Nexus's internal events, as well as user-defined events from both your own addon and other loaded addons.
Subscribing to an event
Subscribing to an event is as simple as defining a callback and passing it to the AddonAPI.
EventHandlers.hpp
#pragma once
namespace EventHandlers {
void MumbleIdentityUpdate(void*);
void Register();
void Cleanup();
}
EventHandlers.cpp
#include "shared.h"
void EventHandlers::MumbleIdentityUpdate(void* eventArgs) {
char buffer[50];
sprintf(buffer, (size_t)50, "Changed character to %s\n", id->Name);
Addon_API->Log(LOGL_INFO, "example-addon", buffer);
}
void EventHandlers::Register() {
Addon_API->Events_Subscribe("EV_MUMBLE_IDENTITY_UPDATED", *EventHandlers::MumbleIdentityUpdate);
}
void EventHandlers::Cleanup() {
Addon_API->Events_Unsubscribe("EV_MUMBLE_IDENTITY_UPDATED", *EventHandlers::MumbleIdentityUpdate);
}
ModuleMain.cpp
#include "EventHandlers.hpp"
EventHandlers::Register();
}
void AddonUnload() {
EventHandlers::Cleanup();
}
Raising Events
Nexus also provides an API for raising events to be consumed either by your own code, or by other developers. An example of raising an event is provided below.
#define EXAMPLE_EVENT_IDENTIFIER "EV_EXAMPLEADDON_EXAMPLE"
void RaiseExampleEvent() {
const wchar_t* message = "This is an example event";
Addon_API->Events_Raise(EXAMPLE_EVENT_IDENTIFIER, (void*)message);
Addon_API->Events_RaiseNotification(EXAMPLE_EVENT_IDENTIFIER);
}