[Tutorial] Using OnPlayerCommandText with YCMD
#1

Hello,

This is a basic tutorial about using OnPlayerCommandText with YCMD, its very easy, so if you know a little scripting there is no need to see this tutorial, its for the new learners.



Here it is how you use OnPlayerCommandText with YCMD:

pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if (strcmp("/mycommand1", cmdtext, true, 10) == 0)
    {
        // Do something here
        return 1;
    }
   
    if (strcmp("/mycommand2", cmdtext, true, 10) == 0)
    {
        // Do something here
        return 1;
    }
   
    else
    {
        new count=Command_GetPlayerCommandCount(playerid);
        for (new i; i<count;i++)
        {
            new s[30]="/";
            strcat(s,Command_GetNext(i, playerid),sizeof(s));
            if(strcmp(s,cmdtext,true)==0)
            return 1;
        }
        SendClientMessage(playerid,0xE08E1FFF,"Error: Invalid!");
        return 1;
    }
}

YCMD:help(playerid, params[], help)
{
    if (help)
    {
        SendClientMessage(playerid, 0xFF0000AA, "Displays help about your mode.");
    }
    else
    {
        if (isnull(params))
        {
            new
                str[128];
            SendClientMessage(playerid, 0xFF0000AA, "Welcome to my mode.");
            format(str, sizeof (str), "Type \"/%s [command]\" for more help on a command", Command_GetDisplayNamed("help", playerid));
            SendClientMessage(playerid, 0xFF0000AA, str);
        }
        else
        {
            Command_ReProcess(playerid, params, true);
        }
    }
    return 1;
}
YCMD:ycomtest(playerid,params[],help)
{
    if(help)
    SendClientMessage(playerid,0xE08E1FFF,"Just test, this is /help ycomtest");
    else
    SendClientMessage(playerid,0xE08E1FFF,"ELSE CONDITION, success");
}
YCMD:test(playerid,params[],help)
{
    if(help)
    SendClientMessage(playerid,0xE08E1FFF,"you typed /HELP TEST");
    else
    SendClientMessage(playerid,0xE08E1FFF,"ELSE CONDITION FOR TEST, success");
}


Only thing you have to do is, add this in else condition inside OnPlayerCommandText callback,
pawn Code:
else
    {
        new count=Command_GetPlayerCommandCount(playerid);
        for (new i; i<count;i++)
        {
            new s[30]="/";
            strcat(s,Command_GetNext(i, playerid),sizeof(s));
            if(strcmp(s,cmdtext,true)==0)
            return 1;
        }
        SendClientMessage(playerid,0xE08E1FFF,"Error: Invalid!");
        return 1;
    }

Please note if you don't have any command in your OnPlayerCommandText,
then simple do this inside it:
pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
        new count=Command_GetPlayerCommandCount(playerid);
        for (new i; i<count;i++)
        {
            new s[30]="/";
            strcat(s,Command_GetNext(i, playerid),sizeof(s));
            if(strcmp(s,cmdtext,true)==0)
            return 1;
        }
        SendClientMessage(playerid,0xE08E1FFF,"Error: Invalid!");
        return 1;
}




ADVANTAGE
This will ensure that "Server unknown command" will be customized as you like, it will check whether the command exists or not, and will give a customized error. So people who have question "how to check if command exists in y_commands" , you have your answer.



Thankyou!
Reply
#2

or you could use strcmp (command) in OnPlayerCommandReceived or OnPlayerCommandPerformed.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)