PlayerCommand: Difference between revisions

From RAGE Multiplayer Wiki
No edit summary
No edit summary
 
(6 intermediate revisions by 5 users not shown)
Line 1: Line 1:
This event is triggered when a player sends a command.


This event is triggered when player send command .
{{ServersideJsEvent}}


==Parameters==
{{JSContainer|
* '''player''' - player, who send command.  
{{Parameters}}
* '''command''' - string with arguments.  
* '''player''': {{RageType|Player}} - [[:Category:Player API|player]], who send command.  
* '''command''': {{RageType|String}} - command (without slash) with arguments.  


==Example #1==
{{Example}}
This example will write "Hello!" to player, who will enter command /meetme into chatbox.s
This example will write "Hello!" to player, who will enter command /meetme into chatbox.s
<div class="header" style="background-color: #408DAE; color: #FFFFFF; border: 2px solid #408DAE;">
<pre>
<div style="margin: 10px 10px 10px 10px;"><b>Server-Side</b></div>
<syntaxhighlight lang="javascript">
mp.events.add("playerCommand", (player, command) => {
mp.events.add("playerCommand", (player, command) => {
const args = command.split(/[ ]+/);
const args = command.split(/[ ]+/);
Line 17: Line 17:
if (commandName === "meetme") {
if (commandName === "meetme") {
player.outputChatBox("Hello!");
player.outputChatBox("Hello!");
};
}
});
});
</pre>
{{Example}}
'''Detecting Invalid Commands: '''
<pre>
mp.events.add('playerCommand', (player, command) => {       
    player.outputChatBox(`${command} is not a valid command. Use /help to find a list of commands.`);
});
</pre>
}}
{{ClientsideCsJsEvent}}
{{CSharpContainer|
<syntaxhighlight lang="csharp">
public delegate void OnPlayerCommandDelegate(string cmd, CancelEventArgs cancel);
</syntaxhighlight>
</syntaxhighlight>
</div>


==Example #2==
{{Parameters}}
* '''cmd''': colshape, expects '''System.String''' type.
* '''cancel''': cancel, expects '''RAGE.Events.CancelEventArgs''' type.
 
{{Example}}
This example will set player restore player health to max when command '''/heal''' is used, or set players armour to max when command '''/armour''' is used.
<syntaxhighlight lang="c#">
Events.OnPlayerCommand += OnPlayerCommand;
</syntaxhighlight>
<syntaxhighlight lang="c#">
public void OnPlayerCommand(string cmd, RAGE.Events.CancelEventArgs cancel)
{
    if (cmd == "heal")
    {
        RAGE.Elements.Player.LocalPlayer.SetHealth(100);
    }
    else if (cmd == "armour")
    {
        RAGE.Elements.Player.LocalPlayer.SetArmour(100);
    }
}
</syntaxhighlight>
}}
 
{{JSContainer|
{{Parameters}}
* '''command''': {{RageType|String}} - command (without slash) with arguments.
 
{{Example}}
This example will output info about entered command into client chatbox.
This example will output info about entered command into client chatbox.


if you enter "/blah 123 566", it will output next message:
if you enter "/blah 123 566", it will output next message:
"You enter command a "blah", and all arguments it a [123,566]"
"You enter command a "blah", and all arguments it a [123,566]"
<div class="header" style="background-color: #AE4040; color: #FFFFFF; border: 2px solid #AE4040;">
<pre>
<div style="margin: 10px 10px 10px 10px;"><b>Client-Side</b></div>
<syntaxhighlight lang="javascript">
mp.events.add("playerCommand", (command) => {
mp.events.add("playerCommand", (command) => {
const args = command.split(/[ ]+/);
const args = command.split(/[ ]+/);
Line 37: Line 78:
if (commandName === "blah") {
if (commandName === "blah") {
mp.gui.chat.push(`You enter command a "${commandName}", and all arguments it a [${args.join(",")}]`)
mp.gui.chat.push(`You enter command a "${commandName}", and all arguments it a [${args.join(",")}]`);
};
}
});
});
</syntaxhighlight>
</pre>
</div>
}}


==See also==
==See also==
{{Player_events}}
{{Player_events}}
[[Category:Player]]
[[Category:Server-side Event]]
[[Category:Client-side Event]]

Latest revision as of 14:53, 23 May 2019

This event is triggered when a player sends a command.

Server-Side
Event

 JavaScript



JavaScript Syntax

Parameters

  • player: Player - player, who send command.
  • command: String - command (without slash) with arguments.

Example

This example will write "Hello!" to player, who will enter command /meetme into chatbox.s

mp.events.add("playerCommand", (player, command) => {
	const args = command.split(/[ ]+/);
	const commandName = args.splice(0, 1)[0];
		
	if (commandName === "meetme") {
		player.outputChatBox("Hello!");
	}
});

Example

Detecting Invalid Commands:

mp.events.add('playerCommand', (player, command) => {        
    player.outputChatBox(`${command} is not a valid command. Use /help to find a list of commands.`);
});


Client-Side Event

 C#  JavaScript



C# Syntax

public delegate void OnPlayerCommandDelegate(string cmd, CancelEventArgs cancel);

Parameters

  • cmd: colshape, expects System.String type.
  • cancel: cancel, expects RAGE.Events.CancelEventArgs type.

Example

This example will set player restore player health to max when command /heal is used, or set players armour to max when command /armour is used.

Events.OnPlayerCommand += OnPlayerCommand;
public void OnPlayerCommand(string cmd, RAGE.Events.CancelEventArgs cancel)
{
    if (cmd == "heal")
    {
         RAGE.Elements.Player.LocalPlayer.SetHealth(100);
    }
    else if (cmd == "armour")
    {
         RAGE.Elements.Player.LocalPlayer.SetArmour(100);
    }
}


JavaScript Syntax

Parameters

  • command: String - command (without slash) with arguments.

Example

This example will output info about entered command into client chatbox.

if you enter "/blah 123 566", it will output next message: "You enter command a "blah", and all arguments it a [123,566]"

mp.events.add("playerCommand", (command) => {
	const args = command.split(/[ ]+/);
	const commandName = args[0];

	args.shift();
		
	if (commandName === "blah") {
		mp.gui.chat.push(`You enter command a "${commandName}", and all arguments it a [${args.join(",")}]`);
	}
});


See also

Checkpoint

Colshape

Entity

Player

Streaming

Vehicle

Waypoint