Skip to content

Event

const event = require("@vectarine/event")

A module for creating and subscribing to events. See newEvent for how to create and use events.

event.newEvent<T>(name: string): Event<T>

Create a new event type, for example newEvent("hit") The name should be unique and is used for debugging purposes. If you try to create newEvent with the same name, you will get the same underlying event object. You can pass any type when triggering the event and it will be forwarded to its subscribers. For example:

local hitEvent: event.Event<number> = event.newEvent("hit")
hitEvent:on("print", function(damage: number)
print("Hit with damage: " .. damage)
end)
hitEvent:dispatch(10) -- This will print "Hit with damage: 10"

Parameters

  • name (string): string The name of the event type

Returns EventType

event.getConsoleCommandEvent(): Event<string>

Clear all previous subscriptions to the console command event and return it.

event.getResourceLoadedEvent(): Event<number>

Clear all previous subscriptions to the resource loaded event and return it. This event is triggered when a resource finishes loading and is ready to be used or if it failed to load. The number is the ID of the resource loaded.

event.getTextInputEvent(): Event<string>

Returns the text input event This event is triggered when a key is pressed and the key is a printable character. The string is the character that was pressed, with modifiers like shift or option. You can use this function to implement text input.

event.getKeyUpEvent(): Event<io.Scancode>

Returns the key up event

event.getKeyDownEvent(): Event<io.Scancode>

Returns the key press event

event.getMouseDownEvent(): Event<string>

Returns the mouse down event

event.getMouseUpEvent(): Event<string>

Returns the mouse up event

export type Event<T> = ...
Event:dispatch<T>(data: T)

Dispatch an event to the bus which will call all the subscribers to that event type with the data passed.

If you unsubscribe from an event during the dispatch, all the subscribers that existed at the start of the dispatch will still be called. If you add subscribers to an event during the dispatch, they will not be called during that dispatch.

Parameters

  • event: EventType<T> The type of event
  • data (T): T Additional event data
Event:on<T>(subscriptionName: string, callback: (data: T) -> ()): SubID

Subscribe to an event type. When that event is triggered, the callback function will be called. If you resubscribe using the same subscription name, the previous subscription will be removed and replaced with the new one.

Parameters

  • event: EventType<T> The type of event
  • callback ((data: T) -> ()): fun(data: T) A function that will be called when the event is sent. The callback function will receive the event data as arguments.

Returns SubID A subscription ID that can be used to unsubscribe

Event:clear<T>()

Clear all subscriptions for a given event type

Parameters

  • event: EventType<T> The type of event
export type SubID = ...

A subscription ID that can be used to unsubscribe from an event

SubID:unsubscribe()

Stop receiving events for a given subscription ID Does nothing if you have already unsubscribed.

Parameters

  • id: SubID The subscription ID returned by event:on(...)