Event system
The event system is at the core of MTA scripting. Events work closely in conjunction with the element tree. Events are triggered when something happens - a player enters a marker, an element is clicked on etc. Each event has a source element, this is the element that performed the action.
Event handlers
To use the event system, you attach event handlers to elements in the element tree using addEventHandler. When you do this, your function will get triggered for all the events triggered on that element, it's parents (and their parents, etc.) and it's children (and their children). As such, an event handler attached to the root element will be triggered when an event occurs for any element. As a consequence you should generally use as specific a handler as you can. If you wish to just see when the player enters a specific marker, just attach the event handler to that marker.
Each event handler has two 'hidden' variables:
- source: This is the element that the event originated from.
- this: This is the element that the handler is being triggered on (i.e. the one you attached it to with addEventHandler).
The source variable is the most important one for most handlers. You almost always will want to reference this variable to tell what element triggered the event. The this variable has some uses for ensuring that an event was emitted by the element that you attached the handler to.
It is important to note that events follow the element hierachy. All events are initially triggered on the source element, followed by all the parent and children elements. This has few important implications:
- An event triggered on the root element will be triggered on every element in the element tree. This should be avoided where possible.
- All events anywhere in the element tree will be triggered on the root element. This means you can easily catch every event of a type by attaching a handler to the root element. Only do this if you genuinely want every event of that type, otherwise attach it somewhere more specific in the element tree.
- You can attach an event handler to your resource's root element to get all the events triggered by elements your resource contains.
Built in events
MTA has a number of built in events. These are listed on the pages Client Scripting Events and Scripting Events.
Custom events
You can create your own events that can be triggered across all resources. This is an important way to communicate with other resources and allow them to hook into your code. To add your own custom event, just call the addEvent function. You can then use the triggerEvent function to trigger that event any time you want - either using a timer, or based on a more general event.
For example, you could be making a Capture the Flag game mode and want to trigger an event when a player captures the flag. You could do this by attaching a event handler to the standard MTA onMarkerHit event and checking that the player entering the marker has the flag. if they do, you can then trigger your more specific onFlagCaptured event and other resources could handle this as they please.
Canceling
Events can be canceled with cancelEvent. This can have a variety of effects, but in general this means that the server will not perform whatever action it would usually do. For example, canceling onPickupUse would prevent a player being given what they tried to pick up, canceling onVehicleStartEnter would prevent the player entering the vehicle. You can check if the currently active event has been canceled using wasEventCanceled. It's important to note that canceling event does not prevent other event handlers being triggered.