Getting Started with Commands

From GTA Network Wiki
Jump to: navigation, search

Getting Started with Commands

Intro

Commands are simple to make and use, and also easy to understand. Here is what's possible to do: [Command("cmd", "Custom error text if wanted", GreedyArg = bool, SensitiveInfo = bool, Alias = "", ..)] Let's have a look at a simple example below:

[Command("hi")]
public void SayHi(Client player)
{
    API.sendChatMessageToPlayer(player, "Hello!");
}

Let's have a closer look at this. Our command part is defined within the [] brackets as "Command()". In our case above, we made a command /hi. When the code is executed, it will look for a function just below it (no matter which name). The functions that's called is SayHi(...), and this contains what the command should do. Here, it prints "Hello!" to our chat.

Optional parameters

Optional parameters can use C#'s default value-assignment. In this example we set the second argument optional. You can also make both optional. (Please note it's useless to assign the first argument as optional, if the last one is required.)

[Command("hi")]
public void SayHi(Client player, int arg1, float arg2 = 1.5f)
{
    //Do stuff with arg1 (required) and arg2 (optional)
}


Commands with parameters

GreedyArg

Parameters are passed through entering the command. Let's use /me [text] as an example-

[Command("me", GreedyArg = true)]
public void MeCommand(Client player, string action)
{ 
    var msg = "* " + player.name + " " + action;
    var players = API.getPlayersInRadiusOfPlayer(30, player);

    foreach (Client c in players)
    {
        API.sendChatMessageToPlayer(c, msg);
    }
}

Note that GreedyArg is used here to allow for spaces in the command, such as /me is awesome, where "is awesome" would be in the parameter. Without GreedyArg, only the word "is" will be in the parameter because "awesome" isn't expected.

Aliases

Attribute Alias allows you to have another command doing the same action as the original one. Let's say we want to add a command /hello, that does the exact same thing as /hi? For that we use Alias = "/hello":

[Command("hi", Alias = "hello")]
public void SayHi(Client player)
{
    API.sendChatMessageToPlayer(player, "Hello!");
}

Now /hi and /hello would trigger the same action.

If you want to have multiple aliases for the same command, then you can append aliases to the Alias parameter, delimited by a comma. In the following example, both /hey and /howdy can additionally be used to run the /hi command:

[Command("hi", Alias="hello,hey,howdy")]
public void SayHi(Client player)
{
    API.sendChatMessageToPlayer(player, "Hello!");
}

SensitiveInfo

This attribute prevents sensitive info being logged to the console. SensitiveInfo attribute hides your command params/input when an exception happens, while the exception itself will still show up. If you don't use this attribute, the sensitive data (e.g. parameter that's sensitive such as f.ex. a pass) will be visible.

[Command("login", "Usage: /login [password]", SensitiveInfo = true, GreedyArg = true)]
public void SayHi(Client player, string password)
{
    //code
}