SA-MP Forums Archive
strcmp with sscanf - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: strcmp with sscanf (/showthread.php?tid=310815)



strcmp with sscanf - $hЂЂp - 13.01.2012

Can i use sscanf for basic strcmp commands?

I mean, if(sscanf(cmdtext...instead of if(sscanf(params...?

Or any way for it?



Sorry for bad english.


Re: strcmp with sscanf - iGetty - 14.01.2012

At the top of your script:

pawn Code:
#include <sscanf2>
This is OnPlayerCommandText:

pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if (strcmp("/test", cmdtext, true, 10) == 0)
    {
        new id;
        if(sscanf(cmdtext, "i", id)) return 1;
        {
            return 1;
        }
    }
    if(strcmp("/nextcommand", cmdtext, true, 10) == 0)
    {
        return 1;
    }
    return 1;
}
Untested.


Re: strcmp with sscanf - Kyosaur - 14.01.2012

Quote:
Originally Posted by iGetty
View Post
At the top of your script:

pawn Code:
#include <sscanf2>
This is OnPlayerCommandText:

pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if (strcmp("/test", cmdtext, true, 10) == 0)
    {
        new id;
        if(sscanf(cmdtext, "i", id)) return 1;
        {
            return 1;
        }
    }
    if(strcmp("/nextcommand", cmdtext, true, 10) == 0)
    {
        return 1;
    }
    return 1;
}
Untested.
If your code isnt tested, you shouldnt post it. Those commands wont work correctly! Not only are specifying the wrong length to the strcmp function (10 for both, which neither equal 10!), but your passing the entire cmdtext string to sscanf, which includes the original command.

pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if(!strcmp(cmdtext, "/Example", true, 8))
    {
        new
            number,
            string[16];

        if(!sscanf(cmdtext[9], "is[15]", number, string))
        {
            printf("number = %d, string = %s.", number, string);
            return 1;
        }
        SendClientMessage(playerid, 0xFF0000FF, "Syntax error: /Example <number> <string>");
        return 1;
    }

    return 0;
}
The above should work without any problems, but it's highly recommended to just use a command processor like zcmd/ycmd with sscanf. This not only is faster, but its much safer due to how easy it is to make mistakes using the above methodology.

Remember to always update the strcmp length parameter (should be the lenght of the command) and the index for cmdtext (should be the length of the command + 1) if you change the name of the command.


Re: strcmp with sscanf - iGetty - 14.01.2012

Sorry Kyosaur, just trying to help.

No problem, at least I learnt something. :P