Getting Started with Commands

From GTA Network Wiki
Jump to: navigation, search

Getting Started with Commands

So you want some commands in your server huh?

First of all, how can you create a command? It's as simple as writing this:

[Command("hi")]

There you go, but that command is boring because it doesn't do nothing at all, in order to let the command do something useful, you have to define a method right under it, like this

[Command("hi")]
public void sayHi(Client player)
{
    //Do stuff
}

That way, whenever a user types /hi, that method should be called, no matter its name, but it has to be placed right after the command.

But hey, what if you want to pass some arguments? No problem

[Command("hi")]
public void sayHi(Client player, int arg1, float arg2)
{
    //Do stuff
}

But maybe that parameters are strictly necessary, and the command shouldn't be executed without those parameters, thats why GreedyArg was introduced:

[Command("hi", GreedyArg = true)]
public void sayHi(Client player, int arg1, float arg2)
{
    //Do stuff
}

This way, if the player doesn't type any parameters, the method won't be executed and the user will be greeted with this message:

CMD Ex1.png

You can customize your command even further, let's say you want two different commands to trigger the same method, then we'll use Alias

[Command("hi", Alias = "hello")]
public void sayHi(Client player, int arg1, float arg2)
{
    //Do stuff
}

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

Now let's move to some 'real world' examples, let's say you are working on a Roleplay server:

[Command("me", GreedyArg = true)]
public void Me_CMD(Client player, String Action)
    { 
        String Msg = "* " + player.name + " " + Action;
        var players = API.getPlayersInRadiusOfPlayer(30, player);

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

Maybe you prefer a racing gamemode? Say no more:

[Command("spawnveh", GreedyArg = true)]
public void SpawnVeh_CMD(Client player, string vName)
{
    API.createVehicle(API.vehicleNameToModel(vName), API.getEntityPosition(Player), new Vector3(), 0, 0);
}