Command Conversion

From SA-MP Wiki

Jump to: navigation, search
Image:32px-Ambox_warning_orange.png This article is currently a Work In Progress. It is therefore possible that the content of this article is currently incomplete or incorrect.


Contents


There are 4 main command processing systems. Here's a list, in order of efficiency and/or ease of use:

  • strcmp - Native
  • dcmd - Macro
  • zcmd - by Zeex
  • y_cmd - by Y_Less

strcmp is the worst. dcmd is essentially the same, but easier to write the code for. y_cmd and zcmd are MUCH faster alternatives.


From strcmp

strcmp works by comparing the command the player entered with every single command that is possible on your server until it finds the right one. If the command they typed is the last one in the code and you have 500 commands, it's not going to be terribly efficient. The code is also (arguable) the most confusing to write, as there is no support for parameters etc. It is highly recommended that you do NOT use strcmp, and instead opt for a more efficient command processing system such as ZCMD or Y_CMD.

strcmp to dcmd

1. strcmp commands are all held in the OnPlayerCommandText callback, and will look like so:

if(strcmp(cmdtext, "/command", true) == 0)
{
    // Code for command
    return 1;
}


2. Firstly, put the code for the command in a function with a dcmd_ prefix, like so:

dcmd_command(playerid, params[])
{
    // Code for command
    return 1;
}

This should be OUTSIDE of all callbacks. NOT under OnPlayerCommandText. It may be wise to put this UNDER/ABOVE (NOT INSIDE!) your OnPlayerCommandText callback.


3. Then, replace the code in OnPlayerCommandText (part 1) with this line:

dcmd(command, 7, cmdtext); // 7 is the length of 'command' - important!


strcmp to y_cmd

1. strcmp commands are all held in the OnPlayerCommandText callback, and will look like so:

if(strcmp(cmdtext, "/command", true) == 0)
{
    // Code for command
    return 1;
}


2. Take the code from part 1 and place it in a function like so, outside of all callbacks:

YCMD:command(playerid, params[], help)
{
    if(help)
    {
        SendClientMessage(playerid, 0xFF0000AA, "Write a short help message here.");
    }
    else
    {
        // Code for command
    }
    return 1;
}

Then delete all the code from part 1.


strcmp to z_cmd

1. strcmp commands are all held in the OnPlayerCommandText callback, and will look like so:

if(strcmp(cmdtext, "/command", true) == 0)
{
    // Code for command
    return 1;
}


2. Take the code from part 1 and place it in a function like so, outside of all callbacks:

CMD:command(playerid, params[])
{
    // Code for command
    return 1;
}

Then delete all the code from part 1.


From dcmd

dcmd is simply a 'macro' ('shortcut') for strcmp. It's the exact same code, just easier to write. Also not recommended.

dcmd to strcmp

dcmd to y_cmd

dcmd to zcmd


From zcmd

ZCMD is a very efficient command processor. It does not search through every command, but instead directly calls the command function if it exists. It is MUCH more efficient than strcmp/dcmd and is therefore highly recommended. It does not have as many features as y_cmd, but is widely used due to it's simplicity. See topic for details.

zcmd to strcmp

zcmd to dcmd

zcmd to y_cmd


From y_cmd

y_cmd is a very efficient command processor. It is MUCH more efficient than strcmp/dcmd, is faster than zcmd and is therefore highly recommended. It supports a 'help' system, command permissions and a lot more. See topic for details.

y_cmd to strcmp

y_cmd to dcmd

y_cmd to zcmd

Personal tools