register: (EventBus, objectOrClass: any, ((any) -> ())?, priority: number?) -> EventSubscriber?
register(object): every on<EventClass> method of the object subscribes; register(EventClass, fn, priority) returns the subscriber. An object's eventFilters table, keyed by handler name, gives a handler the filter eventBus:subscribe takes as its fourth argument (p.eventFilters = { onScriptPostFired = { scriptId = { 750 } } }); read once, when the object is registered (the plugin object: set it before the chunk returns). onScriptPreFired and onScriptPostFired need one with scriptId.
post: (EventBus, event: any) -> ()
Every subscriber in every plugin, higher priority first, then by plugin, synchronously; a subscriber's field writes are seen by later ones and by the poster. The event is an instance of a class the plugin named (class(Base, "LootReceived")); another plugin's VM gets its plain-data fields (functions and handles are left out), as its own class of that name or as an object with a getter per field (getName() for name). Posting one of the client's own event classes (a fake VarbitChanged) is refused: the client raises those itself.
subscribe: (EventBus, eventClass: any, (any) -> (), priority: number?, filter: { actor: "local"?, varbit: { number }?, varp: { number }?, eventName: Strings?, scriptId: ScriptIds? }?) -> EventSubscriber
Subscribe fn to an event class: subscribe(EventClass, fn, priority), higher priority first. Alternative to a handler's first-line early return (if event:getActor() ~= client:getLocalPlayer() then return end, if event:getVarbitId() ~= X then return end): a fourth argument filter, { actor = "local" } (AnimationChanged, InteractingChanged (its source), HitsplatApplied, GraphicChanged, OverheadTextChanged, ActorDeath, SpotAnimationChanged) or { varbit = { ids }, varp = { ids } } (VarbitChanged), is tested in Rust when the event is raised: a raise it refuses never enters the plugin (about 0.07 µs where the early return costs a Luau entry, about 0.8 µs), and what it admits reaches fn as before. A raise it cannot judge is admitted. A filter the event cannot test is refused at the call. { eventName = "name" } or { eventName = { names } } (ScriptCallbackEvent) is the alternative to testing event:getEventName() first in the handler, for a subscriber that acts on a few of the many callbacks the client's scripts raise: a callback no plugin's subscriber names is not raised at all (the site's lookup, no Luau entry). { scriptId = id }, { scriptId = { ids } } or { scriptId = "all" } (ScriptPreFired, ScriptPostFired) names the scripts a subscription takes, and is required for those two events: a script no subscriber named is not raised at all, and a subscription without it is refused, since every script the client runs would reach it. "all" takes every script, on purpose (a script inspector).
- Example
local subscription: EventSubscriber? = nil
function plugin:startUp()
-- Only this varbit's changes reach the handler: the client tests the filter.
subscription = eventBus:subscribe(VarbitChanged, onProtect, 0, { varbit = { PRAYER_VARBIT } })
end
function plugin:shutDown()
if subscription then
eventBus:unregister(subscription)
subscription = nil
end
end