Last active
September 28, 2017 14:08
-
-
Save Rene-Sackers/167c227743d83e877b8a4cd8f2e8658f to your computer and use it in GitHub Desktop.
GT-MP Easy clientside events.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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