"Sub Commands" in ZCMD
#1

Hey guys, Whats up?
I'm trying to make like "Sub Commands" in ZCMD.
For example: /house [create | buy | sell]
Problem is I don't know to to make like params for the param itself.
For example: /house create [posX] [posY] [posZ]
This is my code for now:
pawn Код:
CMD:house(playerid, params[]) {
    new param[20];
    if (sscanf(params, "s", param))
    {
        if (!IsPlayerAdmin(playerid)) return SCM(playerid, COLOR_RED, "USAGE: /(h)ouse [buy | sell]");
        else return SCM(playerid, COLOR_RED, "USAGE: /(h)ouse [create | setowner | delowner | delete | buy | sell]");
    }
    if (!strcmp(param, "create", true))
    {
        if (PlayerInfo[playerid][pInHouse] == -1) return SCM(playerid, COLOR_RED, "You have to be inside an house.");
        // sscanf for other params goes here...
    }
    return 1;
}
Now, I don't know how to the for "Sub" params. Please help me
Thanks!
Reply
#2

For your create subcommand, add another sscanf check like
pawn Код:
new sub[20];
if (sscanf(params, "s[20]s[20]", param, sub))
That was a basic example, anyhow you should add length limit on the strcmp and you should add array sizes on the sscanf check for everything to function properly.

The longest subcommand you're using is setowner which contains 8 characters, for some reason if you set the array size of param to 8 it will not detect the whole word, set it to 12.
Then on the sscanf check change the [20] I've put to 12, same with the sub parameter, check your longest parameter and change its array size to about 2 or more numbers higher than its characters.

Hope I ain't making this complicated?
Reply
#3

For example, "create", "delete" are both 6 chars..
And I didn't understand how to do that :\
Could you make me an example?
Reply
#4

Код:
CMD:house(playerid, params[]) {
    new param[20], modi[20], modi2[20], modi3[20];
    if (sscanf(params, "szzz", param, modi))
    {
        if (!IsPlayerAdmin(playerid)) return SCM(playerid, COLOR_RED, "USAGE: /(h)ouse [buy | sell]");
        else return SCM(playerid, COLOR_RED, "USAGE: /(h)ouse [create | setowner | delowner | delete | buy | sell]");
    }
    if (strcmp(param, "create", true) == 0)
    {
        if(strlen(modi) == 0 || strlen(modi2) == 0 || strlen(modi3) == 0) return SCM(playerid, 0xFFFFFFFF, "You have to enter X Y Z coordinates in the "create" parameter.
        if (PlayerInfo[playerid][pInHouse] == -1) return SCM(playerid, COLOR_RED, "You have to be inside an house.");
        // sscanf for other params NOT needed anymore.
    }
    return 1;
}
You have to use the "z" value here as you only want posx, posy, posz on several commands and not on "delete" for example.
You then have to use "szzz" and ask inside your "create" part:

Код:
if(strlen(modi) == 0 || strlen(modi2) == 0 || strlen(modi3) == 0) return SCM(playerid, 0xFFFFFFFF, "You have to enter X Y Z coordinates in the "create" parameter.
I guess that's what you mean. if you use "s" instead of "z", it will force you to type "delete x y z" too.
Reply
#5

Actually you can ignore what I've said about the array sizes, you can set them to whatever makes you feel comfortable, here's an example.

pawn Код:
CMD:house(playerid, params[]) {
    new param[20];
    if (sscanf(params, "s[20]", param))
    {
        if (!IsPlayerAdmin(playerid)) return SCM(playerid, COLOR_RED, "USAGE: /(h)ouse [buy | sell]");
        else return SCM(playerid, COLOR_RED, "USAGE: /(h)ouse [create | setowner | delowner | delete | buy | sell]");
    }
    if (!strcmp(param, "create", true, 6))
    {
        new sub[20];
        if (PlayerInfo[playerid][pInHouse] == -1) return SCM(playerid, COLOR_RED, "You have to be inside an house.");
        if (sscanf(params, "s[20]s[20]", param, sub))
        {
            // Blahblah
            return 1;
        }
        // sscanf for other params goes here...
    }
    return 1;
}
Reply
#6

pawn Код:
CMD:house(playerid, params[]) {
    new param[60], param1[20], param2[20], param3[20];
    if (sscanf(params, "s[20]s[60]", param1, param)) //Get the first paramater
    {
        // Failed sscanf
    }
    if (sscanf(params, "s[20]s[60]", param2, param)) //Get the second paramater
    }
        // Failed sscanf
    {
    if (sscanf(params, "s[20]s[60]", param3, param)) //Get the third paramater
    }
        // Failed sscanf
    {
    // so on so forth
    return 1;
}
Reply
#7

Thank you all. I've used what iRage said in comment #5. I'll try that out now .
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)