[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
#2

I'd rather zCMD lol.
Reply
#3

Quote:
Originally Posted by Darnell
View Post
I'd rather zCMD lol.
Yes, we all would, but the point of his tutorial is to make it easy for you to change your script (ones that use strcmp, with hundreds of commands) in to using zCMD.
Reply
#4

Nice Tutorial..
Reply
#5

wow,, nice tut!
Reply
#6

Thanks.
Reply
#7

pawn Code:
#define CMD:%1(%2)          \   //line 88
          forward cmd_%1(%2); \  //line 89
          public cmd_%1(%2)        //line 90
pawn Code:
C:\Users\Tab\Desktop\True Roleplay [0.3c]\True Roleplay [0.3c]\gamemodes\TRP.pwn(90) : warning 201: redefinition of constant/macro (symbol "CMD:%1(%2)")
Pawn compiler 3.2.3664          Copyright (c) 1997-2006, ITB CompuPhase


1 Warning.
the only redefinition i see is in the code i used from here, but if i delete any of the code or comment it out, pawn crashes, whats the problem with it or what did i do wrong?
Reply
#8

Useful Tutorial
Thanks
Reply
#9

Excellent information & tutorial, Thank you.
Reply
#10

That's very great
Reply
#11

Loving this tutorial thanks!
Reply
#12

Quote:
Originally Posted by JordanMaddox
View Post
Loving this tutorial thanks!
same
Reply
#13

Awesome thanks alot .
Reply
#14

Quote:
Originally Posted by MadeMan
View Post
There is actually an easier way of doing this.

All you have to do is change your OnPlayerCommandText to

pawn Code:
public OnPlayerCommandPerformed(playerid, cmdtext[], success)
{
    if(success)
    {
        return 1;
    }
   
    if(strcmp(cmdtext, "/help", true) == 0)
    {
        SendClientMessage(playerid, -1, "Available commands: ");
        return 1;
    }
   
    if(strcmp(cmdtext, "/kill", true) == 0)
    {
        SetPlayerHealth(playerid, 0);
        return 1;
    }
   
    return 0;
}
You also need the #include line

pawn Code:
#include <zcmd>
I got thoose errors:

Quote:

C:\Users\Antoine\Desktop\SAMP\Serveur 0.3d\filterscripts\voed.pwn(670 : error 004: function "zcmd_OnPlayerCommandText" is not implemented
C:\Users\Antoine\Desktop\SAMP\Serveur 0.3d\filterscripts\voed.pwn(677 : error 004: function "zcmd_OnPlayerCommandText" is not implemented
C:\Users\Antoine\Desktop\SAMP\Serveur 0.3d\filterscripts\voed.pwn(6785) : error 004: function "zcmd_OnPlayerCommandText" is not implemented
C:\Users\Antoine\Desktop\SAMP\Serveur 0.3d\filterscripts\voed.pwn(6792) : error 004: function "zcmd_OnPlayerCommandText" is not implemented
C:\Users\Antoine\Desktop\SAMP\Serveur 0.3d\filterscripts\voed.pwn(6799) : error 004: function "zcmd_OnPlayerCommandText" is not implemented
C:\Users\Antoine\Desktop\SAMP\Serveur 0.3d\filterscripts\voed.pwn(6806) : error 004: function "zcmd_OnPlayerCommandText" is not implemented
C:\Users\Antoine\Desktop\SAMP\Serveur 0.3d\filterscripts\voed.pwn(6816) : error 004: function "zcmd_OnPlayerCommandText" is not implemented
C:\Users\Antoine\Desktop\SAMP\Serveur 0.3d\filterscripts\voed.pwn(6820) : error 004: function "zcmd_OnPlayerCommandText" is not implemented
C:\Users\Antoine\Desktop\SAMP\Serveur 0.3d\filterscripts\voed.pwn(6846) : error 004: function "zcmd_OnPlayerCommandText" is not implemented
C:\Users\Antoine\Desktop\SAMP\Serveur 0.3d\filterscripts\voed.pwn(6855) : error 004: function "zcmd_OnPlayerCommandText" is not implemented
C:\Users\Antoine\Desktop\SAMP\Serveur 0.3d\filterscripts\voed.pwn(7306) : error 004: function "zcmd_OnPlayerCommandText" is not implemented
Pawn compiler 3.2.3664 Copyright © 1997-2006, ITB CompuPhase


11 Errors.

Reply
#15

Nice Tutorial But Why We Type Extra Just Use #include <ZCMD> :P
Reply
#16

Lol,I'd say this tutorial is useless since strcmp is a fucking scripting way and the slowest one when you can make a command with ZCMD for twice faster.
Example:
pawn Code:
#include <zcmd>

#define COLOR WHITE 0xFFFFFFAA

CMD:help(playerid,params[])
{
       SendClientMessage(playerid,COLOR_WHITE,"/enter /exit /order /buy /buyhouse /sellhouse");
       SendClientMessage(playerid,COLOR_WHITE,"/gedit /gnext /gstatus /ddnext /ddedit /ddname");
       return 1;
}
See? It's twice faster than strcmp...
Reply
#17

its better then we use zcmd xD
Reply
#18

nice work
Reply
#19

Nice Tutorial man
Reply
#20

That's what a need friends thansk alot .
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)