Getting Started with Events: Difference between revisions

From RAGE Multiplayer Wiki
No edit summary
Line 32: Line 32:
if your event will be for server-side use only then you can register it the way you want.
if your event will be for server-side use only then you can register it the way you want.


<div class="header" style="background-color: #408DAE; color: #FFFFFF; border: 2px solid #408DAE;">
{{ServersideCode|
<div style="margin: 10px 10px 10px 10px;"><b>Server-Side</b></div>
<pre>
<pre>
mp.events.add('eventName', (arg1, arg2, etc.) => {
mp.events.add('eventName', (arg1, arg2, etc.) => {
Line 39: Line 38:
});
});
</pre>
</pre>
</div>
}}


if your event will be called from client-side and you registered it in server-side. You have to add '''player''' argument as a main first argument because client-side delivers the localPlayer with your data, so you can know which player is it.
if your event will be called from client-side and you registered it in server-side. You have to add '''player''' argument as a main first argument because client-side delivers the localPlayer with your data, so you can know which player is it.


<div class="header" style="background-color: #408DAE; color: #FFFFFF; border: 2px solid #408DAE;">
{{ServersideCode|
<div style="margin: 10px 10px 10px 10px;"><b>Server-Side</b></div>
<pre>
<pre>
mp.events.add('eventName', (player, arg1, arg2, etc.) => {
mp.events.add('eventName', (player, arg1, arg2, etc.) => {
Line 50: Line 48:
});
});
</pre>
</pre>
</div>
}}


In '''CEF''' you're not allowed to register any events. You can only call the functions that are registered in your '''CEF'''.
In '''CEF''' you're not allowed to register any events. You can only call the functions that are registered in your '''CEF'''.
Line 58: Line 56:
It is pretty important to know how to call your events on each side, or you'll be totally lost in your development. We made it easier for you with the following calling methods, so you can know how the events calling system is working.
It is pretty important to know how to call your events on each side, or you'll be totally lost in your development. We made it easier for you with the following calling methods, so you can know how the events calling system is working.


<syntaxhighlight lang="javascript">
<pre>


// calls server-side local events / calls client-side local events
// calls server-side local events / calls client-side local events
Line 94: Line 92:
mp.trigger('eventName', args);
mp.trigger('eventName', args);


</syntaxhighlight>
</pre>


== Cancelling events ==
== Cancelling events ==
Line 107: Line 105:
A small example of how to use it:
A small example of how to use it:


<syntaxhighlight lang="javascript">
{{ServersideCode|
 
<pre>
let ev = new mp.Event("playerDeath", (player, reason, killer) =>
let ev = new mp.Event("playerDeath", (player, reason, killer) =>
{
{
Line 116: Line 114:
// ev.enable(); due to this line the event will be re-enabled.
// ev.enable(); due to this line the event will be re-enabled.
// ev.destroy(); due to this line the event is never going to be executed if we call this before it
// ev.destroy(); due to this line the event is never going to be executed if we call this before it
</syntaxhighlight>
</pre>
}}


=== Removing ===
=== Removing ===
Line 122: Line 121:
Here are all the methods to remove events:
Here are all the methods to remove events:


<syntaxhighlight lang="javascript">
{{SharedCode|
 
<pre>
// Remove specified handler of specified event
// Remove specified handler of specified event
function playerJoinHandler(player)
function playerJoinHandler(player)
Line 142: Line 141:
// Get all handlers of specified event
// Get all handlers of specified event
mp.events.getAllOf("playerJoin").forEach(_ev => _ev(null));
mp.events.getAllOf("playerJoin").forEach(_ev => _ev(null));
 
</pre>
</syntaxhighlight>
}}

Revision as of 10:19, 6 December 2019

Introduction

Events are the core scripting system for RAGE:MP. They are registered upon your request and triggered in a certain situation, For example, PlayerEnterCheckpoint triggers when a player enters a checkpoint, so the event is automatically triggered and returns for you the Player that entered the checkpoint and the Checkpoint that he entered.

Events system

In-order to use the events you need to register the event into the events tree using addEvent.
There are 2 types of Events:

  • Built in events
  • Custom events

Built in events

They are events provided by RAGE:MP to be auto-triggered in a certain situation as we mentioned in the beginning (a Player entering a Checkpoint).

These are listed on the following pages:

Custom events

They are events build for you to control/call them anytime you want and in any situation you create for the Player.

Using events

Registering an event

You always have to register an event to the events tree before using it anywhere.

Registering it is pretty simple, but you have to keep in mind in which area it'll be called.

if your event will be for server-side use only then you can register it the way you want.

Server-Side
mp.events.add('eventName', (arg1, arg2, etc.) => {
// code
});

if your event will be called from client-side and you registered it in server-side. You have to add player argument as a main first argument because client-side delivers the localPlayer with your data, so you can know which player is it.

Server-Side
mp.events.add('eventName', (player, arg1, arg2, etc.) => {
// code
});

In CEF you're not allowed to register any events. You can only call the functions that are registered in your CEF.

Calling methods

It is pretty important to know how to call your events on each side, or you'll be totally lost in your development. We made it easier for you with the following calling methods, so you can know how the events calling system is working.


// calls server-side local events / calls client-side local events
mp.events.call('eventName', args);

/*
* Calls client-side event from server-side.
* Notice: arguments must be inside an array.
*/
player.call('eventName', [arg1, arg2]);

/*
* Makes server's all players call their client-side.
* Notice: arguments must be inside an array.
*/
mp.players.call('eventName', [arg1, arg2]);

/*
* Makes server-side call filtered player's client-side. 
* For example this filters player with name '''WeirdNewbie''' to call his client-side.
* Notice: arguments must be inside an array.
*/
mp.players.call(mp.players.toArray().filter((_player) => _player.name == 'WeirdNewbie'), 'eventName', [args]);

// Makes client-side call server-side event
mp.events.callRemote('eventName', args);

// Makes client-side call a function registered in CEF
browser.execute(´javascriptFunction('${variable1}','${variable2}');´);

/*
* Makes CEF call a event in client-side.
* Notice: You can't communicate between CEF and server-side
*/
mp.trigger('eventName', args);

Cancelling events

There are 2 ways to cancel a event:

  • Pausing it for future use.
  • Removing it from events tree.

Pausing

In-order to pause a event you are required to use a special function to add that event (mp.Event).
A small example of how to use it:

Server-Side
let ev = new mp.Event("playerDeath", (player, reason, killer) =>
{
    mp.players.broadcast('First blood!');
    ev.destroy(); // this event handler will be not called anymore since it's destroyed
});
// ev.enable(); due to this line the event will be re-enabled.
// ev.destroy(); due to this line the event is never going to be executed if we call this before it

Removing

Here are all the methods to remove events:

Shared
// Remove specified handler of specified event
function playerJoinHandler(player)
{

}

mp.events.add("playerJoin", playerJoinHandler);
mp.events.remove("playerJoin", playerJoinHandler);

// Remove handler(s) specified event(s)
mp.events.remove("playerJoin");
mp.events.remove(["playerJoin", "playerQuit"]);

// Reset whole event tree.
mp.events.reset();

// Get all handlers of specified event
mp.events.getAllOf("playerJoin").forEach(_ev => _ev(null));