Skip to content

Instantly share code, notes, and snippets.

@Rene-Sackers
Last active September 28, 2017 14:08
Show Gist options
  • Save Rene-Sackers/167c227743d83e877b8a4cd8f2e8658f to your computer and use it in GitHub Desktop.
Save Rene-Sackers/167c227743d83e877b8a4cd8f2e8658f to your computer and use it in GitHub Desktop.
GT-MP Easy clientside events.
class EventDispatcher {
public static eventHandlers: { [key: string]: (args: any[]) => void } = {};
constructor() {
API.onServerEventTrigger.connect(<any>this.onEventTrigger);
}
private onEventTrigger = (eventName: string, args: System.Array<any>) => {
var argsArray: any[] = [];
for (var i = args.Length - 1; i > 0; i--)
argsArray[i] = (<any>args)[i];
API.sendChatMessage("Event: " + eventName + ", args: " + JSON.stringify(argsArray));
var handler = EventDispatcher.eventHandlers[eventName];
if (typeof (handler) == undefined || handler == null) {
API.sendChatMessage("~r~No handler for event: " + eventName);
return;
}
handler(argsArray);
};
}
API.onResourceStart.connect(() => {
new EventDispatcher();
});
EventDispatcher.eventHandlers["EventName"] = (args: any[]) => {
// Handle event
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment