Events::addCommand: Difference between revisions

From RAGE Multiplayer Wiki
No edit summary
(Change syntaxhighlight to typescript (better for types))
Line 2: Line 2:


==Syntax==
==Syntax==
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="typescript">
void events.addCommand(string commandName, function handlerFunction);
void events.addCommand(string commandName, function handlerFunction);
</syntaxhighlight>  
</syntaxhighlight>  
Line 10: Line 10:


====Handler function parameters====
====Handler function parameters====
<syntaxhighlight lang="javascript">player playerSource, string fullText [, string arg1, string arg2, ...] </syntaxhighlight>
<syntaxhighlight lang="typescript">player playerSource, string fullText [, string arg1, string arg2, ...] </syntaxhighlight>
*'''playerSource:''' The player who triggered the command.
*'''playerSource:''' The player who triggered the command.
*'''fullText:''' All words after command name.
*'''fullText:''' All words after command name.
Line 17: Line 17:
==Example==
==Example==
'''Example 1:''' This example give weapon to current player with the specified number of ammo or 10000
'''Example 1:''' This example give weapon to current player with the specified number of ammo or 10000
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="typescript">
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 26: Line 26:
</syntaxhighlight>
</syntaxhighlight>
'''Example 2:''' This example implements /me command
'''Example 2:''' This example implements /me command
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="typescript">
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>
</syntaxhighlight>

Revision as of 12:21, 17 April 2017

This function registers command handler.

Syntax

void events.addCommand(string commandName, function 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 playerSource, string fullText [, string arg1, string arg2, ...]
  • playerSource: The player who triggered the command.
  • fullText: All words after command name.
  • arg1, arg2, ...: Each word after command name.

Example

Example 1: This example give weapon to current player with the specified number of ammo or 10000

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

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

});

Example 2: This example implements /me command

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