-
Notifications
You must be signed in to change notification settings - Fork 5
Script events
Learn how to use script events
The same event.On & event.Once logic from alt:V Events applies for custom events.
⚠️ See what argument types are supported in the Serialization Guide
Custom event listeners are registered via event.On.ServerEvent(eventName, callback) for serverside events
and event.On.ClientEvent(eventName, callback) for events sent by a client.
// Triggers as often as the client calls it
event.On.ClientEvent("event_name", func (ctx *event.Ctx) {
// do something
})// Triggers only 1 time
event.Once.ClientEvent("event_name", func (ctx *event.Ctx) {
// do something
})Here is a very basic sample that expects just a single string from a client event named myMessage.
We register it via alt.On.ClientEvent.
alt.On.ClientEvent("myMessage", MyMessageHandler)And the method is defined as this.
// TODO: update example
func MyMessageHandler(ctx *event.Ctx)
{
var msg =
alt.LogInfo()
}The method will be called like this from client
alt.emitServer("myMessage", "my test message");The method name doesn't really matter in this case and i just named it similar to the event name.
Any js object send via alt.emitServer will be a map.
alt.On.ClientEvent("myBigObject", MyBigObjectHandler)
// ...
// TODO: update example
func MyBigObjectHandler(ctx *event.Ctx)
{
myMap, ok := myBigObject.(map[string]string)
if !ok {
alt.LogError("myBigObject can not be converted to type map[string]string")
return
}
eyeColor := myMap["eyeColor"]
if eyeColor == nil {
alt.LogError("myBigObject has no property called eyeColor")
return
}
alt.LogInfo(fmt.Sprintf("EyeColor: %v", eyeColor));
}And this is the client code.
const myBigObject = { firstName: "John", lastName: "Doe", eyeColor: "blue" };
alt.emitServer("myBigObject", myBigObject);You encountered a problem 💥? Check out the Troubleshooting Guide!
If you can't find a solution there file a new issue in this repository or contact me on Discord.