Skip to content

Create Custom Game Events

Jason Stallings edited this page Dec 6, 2022 · 11 revisions

Create a new custom event

Creating a new event emitter will automatically add the event to LuaEventManager.

import { EventEmitter } from '@asledgehammer/pipewrench-events'

// Define/Export a listener type
export type MusicTrackUpdateListener = (
    track: string,
    volume: number
) => void

// Create/Export the event
export const MusicTrackUpdateEvent = new EventEmitter<MusicTrackUpdateListener>('MusicTrackUpdate')

Use an event

import { MusicTrackUpdateListener, MusicTrackUpdateEvent } from "./CustomEvents/OnCustomEvent"

// Create an event listener
const onMusicTrackUpdate: MusicTrackUpdateListener = (track, volume) => {
    print(`Playing Track: ${track} (Volume: ${volume})`)
}

// Add event listener
MusicTrackUpdateEvent.addListener(onMusicTrackUpdate)

// Trigger the event
MusicTrackUpdateEvent.trigger("track01", 1.0)

// Remove event listener
MusicTrackUpdateEvent.removeListener(onMusicTrackUpdate)
Clone this wiki locally