Events::addCommand: Difference between revisions

From RAGE Multiplayer Wiki
No edit summary
m (Cleaned up, made it more readable and understandable)
Line 1: Line 1:
This function registers command handler.
This function registers a command handler.


==Syntax==
==Syntax==
<syntaxhighlight lang="typescript">
<pre>
void events.addCommand(string commandName, function handlerFunction);
mp.events.addCommand(commandName, handlerFunction);
</syntaxhighlight>  
</pre>  
===Required Arguments===
===Required Arguments===
*'''commandName:''' The name of the command you wish to attach a handler to
*'''commandName:''' The name of the command you wish to attach a handler to
Line 10: Line 10:


====Handler function parameters====
====Handler function parameters====
<syntaxhighlight lang="typescript">Player playerSource, string fullText [, string arg1, string arg2, ...] </syntaxhighlight>
<pre>player, fullText [, arg1, arg2, ...] </pre>
*'''playerSource:''' The player who triggered the command.
*'''player:''' The player object.
*'''fullText:''' All words after command name.
*'''fullText:''' All arguments after the command name.
*'''arg1, arg2, ...:''' Each word after command name.
*'''arg1, arg2, ...:''' Each argument after the command name.


==Example==
==Example==
'''Example 1:''' This example give weapon to current player with the specified number of ammo or 10000
This example gives a weapon to the current player with the specified number of ammo. If not specified, it will give 10000.
<syntaxhighlight lang="typescript">
<div class="header" style="background-color: #408DAE; color: #FFFFFF; border: 2px solid #408DAE;">
<div style="margin: 10px 10px 10px 10px;"><b>Server-Side</b></div>
<pre>
mp.events.addCommand("weapon", (player, fullText, weapon, ammo) => {
mp.events.addCommand("weapon", (player, fullText, weapon, ammo) => {
var weaponHash = mp.joaat(weapon);
var weaponHash = mp.joaat(weapon);
Line 24: Line 26:


});
});
</syntaxhighlight>
</pre>
'''Example 2:''' This example implements /me command
</div>
<syntaxhighlight lang="typescript">
 
This example implements /me command
<div class="header" style="background-color: #408DAE; color: #FFFFFF; border: 2px solid #408DAE;">
<div style="margin: 10px 10px 10px 10px;"><b>Server-Side</b></div>
<pre>
mp.events.addCommand("me", (player, message) => {
mp.events.addCommand("me", (player, message) => {
mp.players.broadcast(`* ${player.name}: ${message}`);
mp.players.broadcast(`* ${player.name}: ${message}`);
});
});
</syntaxhighlight>
</pre>
</div>


==See Also==
==See Also==
{{Event_functions}}
{{Event_functions}}

Revision as of 02:06, 29 September 2018

This function registers a command handler.

Syntax

mp.events.addCommand(commandName, handlerFunction);

Required Arguments

  • commandName: The name of the command you wish to attach a handler to
  • handlerFunction: The function that you want the command to trigger, which has to be defined before you add the handler

Handler function parameters

player, fullText [, arg1, arg2, ...] 
  • player: The player object.
  • fullText: All arguments after the command name.
  • arg1, arg2, ...: Each argument after the command name.

Example

This example gives a weapon to the current player with the specified number of ammo. If not specified, it will give 10000.

Server-Side
mp.events.addCommand("weapon", (player, fullText, weapon, ammo) => {
	var weaponHash = mp.joaat(weapon);

	player.giveWeapon(weaponHash, parseInt(ammo) || 10000);

});

This example implements /me command

Server-Side
mp.events.addCommand("me", (player, message) => {
	mp.players.broadcast(`* ${player.name}: ${message}`);
});

See Also