Event Service

Learn more about the Eventservice

Introduction

Sending events is a key feature for each gamemode. This service will help you archive this goal.

Keep in mind, our event service is a wrapper around the alt:V event system. If you want, you can use it the way alt:V provides it with alt.emit, alt.emitClient and so on. In a clean code scenario, it would be better to use the eventService to keep your code clean and structured.

Base Usage

We use the same method signature as alt:V. If you are already familiar with it, then it will be very easy for you to use this service.

@Component()
export class MyComponent {

    constructor(
        private readonly eventService: EventService
    ) {}

    // Emit event to server
    public yourMethod(): void {
        this.eventService.emit('eventName', 'data1', 'data2', 'data3')
    }

}

Special Usage

In our own gamemode we need the following feature to send data directly to the player's GUI without the annoying part of creating redundant code on clientside to pass data to GUI. This method will help you to solve this problem.

Internally, we use the client as a bridge. There is no magic, only an event that retrieves the data and send it to the GUI itself.

@Component()
export class MyComponent {

    constructor(
        private readonly eventService: EventService
    ) {}

    // Send event to all players gui
    public yourMethod(): void {
        this.eventService.emitGui(null, 'eventName', 'data1', 'data2', 'data3')
    }

    // Send event to specific player gui
    public yourMethod(): void {
        const player = Player.getByID(1);
        this.eventService.emitGui(player, 'eventName', 'data1', 'data2')
    }

}

Side Notes

The event service also has methods for retrieving events. If you're using the decorators, there is no need to use them. For the sake of completeness, they're included.

@Component()
export class YourComponent {

    constructor(
        private readonly eventService: EventService
    ) {
        this.eventService.on('event', (...args: any[]) => {
            // Do you stuff
        })
    }
}

Net to know

We provide some other methods for offServer and off Eventlistener.

// Keep in mind, offServer need the same function context as onClient to remove
// the eventListener
@Component()
export class YourComponent {

    constructor(
        private readonly eventService: EventService
    ) {
        this.eventService.offClient('event', (...args: any[]) => {
            // Do you stuff
        })
    }
}

Last updated