[GT-MP API] Writing Commands

Writing Commands for GT-MP using the GT-MP API

I primarily learned to program by using the GTA:Network API. GTA:Network is no longer a solid API as the developers have moved over to Rage:MP. In the meantime GT-MP exists and I'll be writing a handful of tutorials about using GT-MP.

You can visit the official wiki to figure out how to setup your Development Environment and after that come back to this tutorial. Make sure that you get your first GameMode script running as we'll be adding to an existing gamemode.

You should have something similar to this:

class MyGameMode : Script  
{
     public MyGameMode() {
          Console.WriteLine("Server Started...");
     }
}

When writing commands you need to create public void functions. A public void function means that the function itself will never return a value. Void means after the function is initiated it simply has nothing else that will occur after it.

Here's a public void function:

public void HelloWorld() {  
     Console.Write("Hello World!");
}

Here's a public bool function, it must return true or false.

public bool HelloWorld() {  
     Console.Write("Hello Boolean!");
     return true;
}

Here's another function that returns a string. public string function, it must return a string.

public string GetHelloWorld() {  
     return "Hello World";
}

Basically the way functions work in C# is that once they're created they can either return a specific type of data or they'll just go into the void; nothing returns from the void. Just imagine a black hole when you say void.

In order to initiate a command we need to tell the server that our function is a command. We do so by adding a line just above it.

[Command("myCommandName")]
public void cmdMyCommand() {  
     Console.WriteLine("This is a command!");
}

Now if we go in-game and we type /myCommandName it will print This is a command! into the console. Now let's talk about parameters that we can add to our function.

[Command("myCommandName")]
public void cmdMyCommand(Client player) {  
     API.SendChatMessageToPlayer(player, "Hello Player!");
}

When we're handling data types in comes in a specific form:
DataType VariableName

In the instance above we have.
Client player

Client is the DataType and player is our variable.

So essentially when we initiate this command, the Client data type is assigned based on whomever initiated the command. We can then use this data to send the player who initiated the command a message.

API.sendChatMessageToPlayer(player, "You initiated a command!");  
/// See where we used the player variable?

Commands can also take additional parameters in their code. We can use the GreedyArg attribute to accomplish this.

[Command("me", GreedyArg = true)]
public void cmdMeCommand(Client player, string message) {  
     API.sendChatMessageToAll(player.name, message);
}

GreedyArg basically allows any additional text after the basic command to be consumed into a string data type.

If I were to say /me opens the door.

What would the string data type of the message variable contain?

It would contain:
"opens the door."

That about wraps up the basic usage of Commands. You can read more about commands on the wiki.