Player::call: Difference between revisions

From RAGE Multiplayer Wiki
m (informed anyone reading that you cant pass vector3 type variables through this call)
No edit summary
 
(8 intermediate revisions by 8 users not shown)
Line 1: Line 1:
This function call added client-side event for selected player.
{{JSContainer|
'''FROM SERVER''':
*This function calls a client-side event for the selected player.


'''FROM CLIENT''':
*For Peer 2 Peer connections. The current client can call an event on another client's scriptside and that other client can handle that event.
===Required Params===
*'''eventName:''' {{RageType|string}} - the event name that will be called
*'''args:''' {{RageType|Array[]}} - Any arguments, what should be sent to client. Supports entities, strings, numbers and booleans. (Objects and Arrays should be packed to JSON format.)
===Return value===
*''' {{RageType|void}} '''
'''Note:''' payload is limited to 8192 bytes.
==Syntax==
==Syntax==
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
player.call(String eventName [, ...args]);
player.call(eventName, ...args);
</syntaxhighlight>  
</syntaxhighlight>
===Required Arguments===
*'''eventName:''' Event name, what will be called.
*'''args:''' Any arguments, what should be sended to client. Supports entities, strings, numbers and booleans. (Objects and Arrays should be packed to JSON format.)
 
==Example==
That's example will call event added on client side for player with ID 1337, disable regeneration health and send number current player health.


<div class="header" style="background-color: #AE4040; color: #FFFFFF; border: 2px solid #AE4040;">
==Example==
<div style="margin: 10px 10px 10px 10px;"><b>Client-Side</b></div>
Register the event that you want to call in client-side using [[Events::add|mp.events.add]]
{{ClientsideCode|
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
let disableRegeneration = (currentHealth) => { //currentHealth - value, what we send from server.
mp.events.add('setPlayerConfigFlag', (flag, enable) => {
mp.game.player.setHealthRechargeMultiplier(0); //Disable regeneration
    mp.players.local.setConfigFlag(flag, enable);
let text = `Regeneration disabled. Current health: ${currentHealth}`;
})
mp.gui.chat.push(text); //Output text to default chatbox
};
 
mp.events.add('disablePlayerRegeneration', disableRegeneration);
</syntaxhighlight>
</syntaxhighlight>
</div>
}}


Here we create a function that will call 'setPlayerConfigFlag' event and it will set the given player a given config flag.
{{ServersideCode|
<syntaxhighlight lang="javascript">
function setPlayerConfigFlag(id, flag, enable) {
    const player = mp.players.at(id); //Find player with ID 1337;
    if (!player || !mp.players.exists(player)) return; //Do nothing if no player was found
    player.call("setPlayerConfigFlag", [flag, enable]);
}


<div class="header" style="background-color: #408DAE; color: #FFFFFF; border: 2px solid #408DAE;">
setPlayerConfigFlag(1337, 18, true); //enable can punch flag for player id 1337
<div style="margin: 10px 10px 10px 10px;"><b>Server-Side</b></div>
<syntaxhighlight lang="javascript" highlight="4">
let player = mp.player.at(1337); //Get player by ID
if (player) {
let playerHealth = player.health;
player.call(`disablePlayerRegeneration`, playerHealth);
};
</syntaxhighlight>
</syntaxhighlight>
</div>
}}
 
 
 
<b> You cannot pass Vector3 types </b> ''(send them through x,y,z and put them in a Vector3 type variable in the client function)''


==See Also==
}}
==See also==
{{Player_block}}
{{Player_block}}

Latest revision as of 19:52, 1 November 2025

JavaScript Syntax

FROM SERVER:

  • This function calls a client-side event for the selected player.

FROM CLIENT:

  • For Peer 2 Peer connections. The current client can call an event on another client's scriptside and that other client can handle that event.

Required Params

  • eventName: string - the event name that will be called
  • args: Array[] - Any arguments, what should be sent to client. Supports entities, strings, numbers and booleans. (Objects and Arrays should be packed to JSON format.)

Return value

  • void

Note: payload is limited to 8192 bytes.

Syntax

player.call(eventName, ...args);

Example

Register the event that you want to call in client-side using mp.events.add

Client-Side
mp.events.add('setPlayerConfigFlag', (flag, enable) => {
    mp.players.local.setConfigFlag(flag, enable);
})

Here we create a function that will call 'setPlayerConfigFlag' event and it will set the given player a given config flag.

Server-Side
function setPlayerConfigFlag(id, flag, enable) {
    const player = mp.players.at(id); //Find player with ID 1337;
    if (!player || !mp.players.exists(player)) return; //Do nothing if no player was found
    player.call("setPlayerConfigFlag", [flag, enable]);
}

setPlayerConfigFlag(1337, 18, true); //enable can punch flag for player id 1337



See also