Events::addCommand: Difference between revisions
CocaColaBear (talk | contribs) No edit summary |
No edit summary |
||
| (2 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
__TOC__ | |||
{{ServersideJsFunction}} | |||
This function registers a command handler. | |||
{{JSContainer| | |||
==Syntax== | ==Syntax== | ||
< | <pre> | ||
mp.events.addCommand(commandName, handlerFunction); | |||
</ | </pre> | ||
====Handler function | == Required Arguments == | ||
< | *'''commandName:''' {{RageType|String}} (The name of the command you wish to attach a handler to) | ||
*''' | *'''handlerFunction:''' {{RageType|Void}} (The function that you want the command to trigger, which has to be defined before you add the handler) | ||
*'''fullText:''' All | |||
*'''arg1, arg2, ...:''' Each | ===Handler function parameter === | ||
<pre>player, fullText [, arg1, arg2, ...] </pre> | |||
*'''player:''' {{RageType|Object}} | |||
*'''fullText:''' {{RageType|Array}} (All arguments after the command name) | |||
*'''arg1, arg2, ...:''' {{RageType|Any}} (Each argument after the command name) | |||
==Example== | ==Example== | ||
This example gives a weapon to the current player with the specified number of ammo. If not specified, it will give 10000. | |||
< | {{ServersideCode| | ||
<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 32: | ||
}); | }); | ||
</ | </pre> | ||
}} | |||
< | |||
This example implements /me command | |||
{{ServersideCode| | |||
<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}`); | ||
}); | }); | ||
</ | </pre> | ||
}} | |||
}} | |||
==See Also== | ==See Also== | ||
{{Event_functions}} | {{Event_functions}} | ||
Latest revision as of 11:51, 3 December 2019
Server-Side Function
This function registers a command handler.
JavaScript Syntax
Syntax
mp.events.addCommand(commandName, handlerFunction);
Required Arguments
- commandName: String (The name of the command you wish to attach a handler to)
- handlerFunction: Void (The function that you want the command to trigger, which has to be defined before you add the handler)
Handler function parameter
player, fullText [, arg1, arg2, ...]
- player: Object
- fullText: Array (All arguments after the command name)
- arg1, arg2, ...: Any (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
- Functions
- Properties