[Tutorial] How to use OnPlayerCommandText with ZCMD
#1

ZCMD is a good way to process commands, but there is a problem using it with scripts that have many commands defined already, using other methods that require the OnPlayerCommandText callback (such as strcmp or dcmd). If you want to use ZCMD there, you would have to convert ALL the commands into ZCMD. But if you have many commands, it could take a lot of time. Luckily, there is a fix for that problem.

You can put the contents of zcmd.inc inside your gamemode/filterscript.

EDIT: There is an easier way --> CLICK HERE

Define

First choose how you define your commands.


If you are using COMMAND:mycmd(playerid, params[])

put this into your script

pawn Code:
#define COMMAND:%1(%2)          \
            forward cmd_%1(%2); \
            public cmd_%1(%2)
If you are using CMD:mycmd(playerid, params[])

put this into your script

pawn Code:
#define CMD:%1(%2)          \
            forward cmd_%1(%2); \
            public cmd_%1(%2)
If you are using command(mycmd, playerid, params[])

put this into your script

pawn Code:
#define command(%1,%2)          \
            forward cmd_%1(%2); \
            public cmd_%1(%2)
If you are using cmd(mycmd, playerid, params[])

put this into your script

pawn Code:
#define cmd(%1,%2)          \
            forward cmd_%1(%2); \
            public cmd_%1(%2)
OnPlayerCommandText

Now to the OnPlayerCommandText callback.

pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
    new
        pos,
        funcname[32];
    while (cmdtext[++pos] > ' ')
    {
        funcname[pos-1] = tolower(cmdtext[pos]);
    }
    format(funcname, sizeof(funcname), "cmd_%s", funcname);
    while (cmdtext[pos] == ' ') pos++;
    if (!cmdtext[pos])
    {
        return CallLocalFunction(funcname, "is", playerid, "\1");
    }
    return CallLocalFunction(funcname, "is", playerid, cmdtext[pos]);
}
If you want to send your own message when command was not found instead of default "SERVER: Unknown command."

NOTE: Use it ONLY in the GAMEMODE script, because returning 1 in a filterscript will disable commands in other scripts.

pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
    new
        pos,
        funcname[32];
    while (cmdtext[++pos] > ' ')
    {
        funcname[pos-1] = tolower(cmdtext[pos]);
    }
    format(funcname, sizeof(funcname), "cmd_%s", funcname);
    while (cmdtext[pos] == ' ') pos++;
    if (!cmdtext[pos]) cmdtext[pos] = '\1';
    if(!CallLocalFunction(funcname, "is", playerid, cmdtext[pos]))
    {
        SendClientMessage(playerid, -1, "Invalid command. Use /help");
    }
    return 1;
}
And you are done.

Example

Now you can use ZCMD with strcmp for example

pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if(strcmp(cmdtext, "/help", true) == 0)
    {
        SendClientMessage(playerid, -1, "Available commands: ");
        return 1;
    }
   
    if(strcmp(cmdtext, "/kill", true) == 0)
    {
        SetPlayerHealth(playerid, 0);
        return 1;
    }
   
    new
        pos,
        funcname[32];
    while (cmdtext[++pos] > ' ')
    {
        funcname[pos-1] = tolower(cmdtext[pos]);
    }
    format(funcname, sizeof(funcname), "cmd_%s", funcname);
    while (cmdtext[pos] == ' ') pos++;
    if (!cmdtext[pos])
    {
        return CallLocalFunction(funcname, "is", playerid, "\1");
    }
    return CallLocalFunction(funcname, "is", playerid, cmdtext[pos]);
}
NOTE: You don't need the #include <zcmd> line anymore.


Credits to ZeeX for the original ZCMD include.
Reply


Messages In This Thread
How to use OnPlayerCommandText with ZCMD - by MadeMan - 12.08.2011, 10:10
Re: How to use OnPlayerCommandText with ZCMD - by Darnell - 12.08.2011, 10:18
Re: How to use OnPlayerCommandText with ZCMD - by Antonio [G-RP] - 12.08.2011, 10:20
Re: How to use OnPlayerCommandText with ZCMD - by HyperZ - 12.08.2011, 10:46
Re: How to use OnPlayerCommandText with ZCMD - by emokidx - 12.08.2011, 11:05
Re: How to use OnPlayerCommandText with ZCMD - by AztreX - 29.08.2011, 00:49
Re: How to use OnPlayerCommandText with ZCMD - by SantarioLeone - 06.10.2011, 17:13
Re: How to use OnPlayerCommandText with ZCMD - by Tigerkiller - 06.10.2011, 17:30
Re: How to use OnPlayerCommandText with ZCMD - by Mr. Random - 07.10.2011, 06:07
Respuesta: How to use OnPlayerCommandText with ZCMD - by SuperMarioRol - 01.11.2011, 12:15
Re: How to use OnPlayerCommandText with ZCMD - by JordanMaddox - 09.12.2011, 11:05
Re: How to use OnPlayerCommandText with ZCMD - by BoyDenker - 31.01.2012, 11:12
Re: How to use OnPlayerCommandText with ZCMD - by thedark222 - 31.01.2012, 11:14
Re : Re: How to use OnPlayerCommandText with ZCMD - by Stefano.R - 10.03.2012, 08:48
Re: How to use OnPlayerCommandText with ZCMD - by VIPAwesome - 10.03.2012, 08:54
Re: How to use OnPlayerCommandText with ZCMD - by Tom Kingston - 08.06.2012, 17:15
Re: How to use OnPlayerCommandText with ZCMD - by AliHaider - 17.06.2012, 12:54
Re: How to use OnPlayerCommandText with ZCMD - by Rudy_ - 19.06.2012, 14:54
Re: How to use OnPlayerCommandText with ZCMD - by [CwS]TonyFortebracci - 19.10.2012, 12:43
Re: How to use OnPlayerCommandText with ZCMD - by M3mPHi$_S3 - 19.10.2012, 13:18
Re: How to use OnPlayerCommandText with ZCMD - by Plovix - 02.12.2012, 17:03
Re: How to use OnPlayerCommandText with ZCMD - by iGetty - 02.12.2012, 20:34
Re: How to use OnPlayerCommandText with ZCMD - by Anglesman - 05.02.2014, 06:33

Forum Jump:


Users browsing this thread: 3 Guest(s)