Las Venturas Playground
Tutorial: Adding a new command - Printable Version

+- Las Venturas Playground (https://forum.sa-mp.nl)
+-- Forum: Main Talk (https://forum.sa-mp.nl/forum-3.html)
+--- Forum: Development (https://forum.sa-mp.nl/forum-16.html)
+--- Thread: Tutorial: Adding a new command (/thread-30546.html)



Tutorial: Adding a new command - Russell - 12-31-2012

Many features will need commands to interact with the players. Of course, start of by asking yourself whether you really need a command -- maybe the feature can be discoverable somewhere on the Grand Theft Auto: San Andreas map instead?

Ok, I really want to add a new command.
Good news. Starting with version 5.0 of the LVP PreCompiler, you can use the @command annotation to make this happen. Look at the following piece of code:

Code:
class MyFeature {
    @command("mycommand")
    public onMyCommand(playerId, params[]) {
        SendClientMessage(playerId, 0x00FF0000, "Hi, you typed my command!");
    }
};

That all you need for adding the command /mycommand.

How do I deal with parameters?
The command function will receive two parameters: playerId and params. When you don't need them, be sure to mark them as unused with "#pragma unused" at the end of the function.

The playerId speaks for itself. This is the Id of the player who typed the command. The params is a string with the text typed after the command itself. This means that when a player types "/mycommand foo bar", the contents of params[] will be "foo bar".

The Command class provides a few utility methods to easily get access to the parameters as typed by the player, in the format you'd like to access them. See the documentation for more information:
http://development.sa-mp.nl/documentation.php#command

That's all!