parameters with strcmp
#1

Can someone show me how to do multi parameters in STRCMP?

For example, playerid1,playerid2,amount...

or:

playerid,price,amount...?

Rep+ for helpers

don't say use sscanf,y_ini what ever I want with strcmp,thanks.
Reply
#2

I don't see what do you wanna do.

If you wanna put variable parameters in strcmp, use format.

PHP код:

new string[256];
format(stringsizeof(string), "%d %d %d"playerid1playerid2amount);
if(!
strcmp(string"0 8 9999"false)) // if the string corresponds to playerid1 = 0; playerid 2 = 8 and amount = 9999, do : 
Reply
#3

Quote:

don't say use sscanf,y_ini what ever I want with strcmp,thanks.

I am going to say it. Why? It's incredibly bad practice to use string compare for commands. Just save yourself the effort and do it right the first time. Trust me, you'll thank yourself.
Reply
#4

I haven't understood, forgive me.

So yeah, using sscanf is an incredibely better way than using "strcmp".
But if you really don't want to use either anything from ******'s work or any plugin for your server, use strtok which will return as a string everything between the "index" and the nearest space after the index. (index stands for the last space found I think)
Reply
#5

Sorry if I wasn't clear enough ^^
What I want to do is the fallowing command:

/selldrugs (playerid) (drugname) (amount) (price)

I was trying like 4 hours to make it on my own but I can add MAX 2 "parameters" or what ever is this I added only "playerid" and "drugname" whenever I tried to add "amount" and "price" command didn't worked and it was whole mess, and now I just fucked up whole command and it's not working.. what have I made:

It's not working it keep saying Invalid player's ID and so on.. it's really messy so I had to ask here what the hell is going on and how can I fix that and add rest 2 things "amount" and "price"

Yeah I know those new command processors are MUCH better and easier to script but seriously I'm scripting for server that is pretty old and oldschool script - strcmp, I know how to script with zcmd+sscanf but I can't mix this with strcmp.. so if you can help me with this I would be really glad

pawn Код:
if(strcmp(cmd, "/selldrugs", true) == 0 || strcmp(cmd, "/sd", true) == 0)
    {
        new string[256];
        new tmp[256];
       
        new giveplayer[MAX_PLAYER_NAME];
        new sendername[MAX_PLAYER_NAME];
       
        new gpid = ReturnUser(tmp);
        new price;
        new needed;
       
        new x_nr[256];
        x_nr = strtok(cmdtext, idx);
        tmp = strtok(cmdtext, idx);
       
        GetPlayerName(gpid, giveplayer, sizeof(giveplayer));
        GetPlayerName(playerid, sendername, sizeof(sendername));

        if(IsPlayerConnected(gpid))
        {
            if(!strlen(tmp))
            {
                SendClientMessage(playerid, -1, "{FF6A22}INFO: {FFFFFF}/selldrugs (/sd) [playerid] [drugname] [amount] [price]");
                return 1;
            }
            if(!strlen(x_nr))
            {
                SendClientMessage(playerid, -1, "{FF6A22}USAGE: {FFFFFF}/selldrugs (/sd) [playerid] [drugname] [amount] [price]");
                SendClientMessage(playerid, -1, "{FF6A22}Available names: {FFFFFF}Weed, Cocaine, Crack, Meth, Ectasy, Heroin");
                return 1;
            }
            if(gpid != INVALID_PLAYER_ID)
            {
                if(ProxDetectorS(8.0, playerid, gpid))
                {
                    if(strcmp(x_nr,"weed",true) == 0)
                    {
                        format(string, sizeof(string), "* You offered to sell %d grams of weed to %s for $%d, Wait for a reply.", needed, giveplayer, price);
                        SendClientMessage(playerid, 0x33CCFFAA, string);
                        //
                        format(string, sizeof(string), "* %s wants to sell %d grams of weed to you for $%d, (type /acceptdrugs cocaine) to buy.", sendername, needed, price);
                        SendClientMessage(gpid, 0x33CCFFAA, string);
                    }
                }
                else
                {
                    SendClientMessage(playerid, -1, "{FF6A22}INFO:{FFFFFF} That player isn't enough close");
                }
            } // invalid player ID
        }
        else
        {
            SendClientMessage(playerid, -1, "{FF6A22}INFO:{FFFFFF} Invalid player ID.");
        }
        return 1;
    }
    // rest of Commands
    return 0;
}
Reply
#6

Acutally, sscanf is a better way than strtok, your command will become like that using it

pawn Код:
#include <sscanf2>

public OnPlayerCommandText(playerid, cmdtext[])
{
    new cmd[33], params[128];
    sscanf(cmdtext, "'/'s[33]s[128]", cmd, params);
    if(!strcmp(cmd, "selldrugs", true) || !strcmp(cmd, "sd", true))
    {
        new gpid, drugname[21], amount, price;
        if(sscanf(params, "us[21]dd", gpid, drugname, amount, price))
        {
            SendClientMessage(playerid, -1, "{FF6A22}USAGE: {FFFFFF}/selldrugs (/sd) [playerid] [drugname] [amount] [price]");
            return SendClientMessage(playerid, -1, "{FF6A22}Available names: {FFFFFF}Weed, Cocaine, Crack, Meth, Ectasy, Heroin");
        }
        if(!IsPlayerConnected(gpid)) return SendClientMessage(playerid, -1, "The player isn't connected !");
        if(ProxDetectorS(8.0, playerid, gpid))
        {
            if(!strcmp(drugname,"weed",true))
            {
                format(string, sizeof(string), "* You offered to sell %d grams of weed to %s for $%d, Wait for a reply.", needed, giveplayer, price);
                SendClientMessage(playerid, 0x33CCFFAA, string);
                //
                format(string, sizeof(string), "* %s wants to sell %d grams of weed to you for $%d, (type /acceptdrugs cocaine) to buy.", sendername, needed, price);
                SendClientMessage(gpid, 0x33CCFFAA, string);
            }
        }
        else
        {
            SendClientMessage(playerid, -1, "{FF6A22}INFO:{FFFFFF} That player isn't enough close");
        }
        return 1;
    }
}
(If you wanna keep using the '/' in the command name, simply remove the " '/' " when you use the "sscanf" function on cmdtext)
Reply
#7

And here we go Server unknown command.. That's why I don't like sscanf2.. and nothing connected with this, cuz nothing isn't working.

pawn Код:
new string[256];
    new params[128];
    new sendername[MAX_PLAYER_NAME];
    new giveplayer[MAX_PLAYER_NAME];
    sscanf(cmdtext, "'/'s[33]s[128]", cmd, params);
    if(!strcmp(cmd, "selldrugs", true) || !strcmp(cmd, "sd", true))
    {
        new gpid, drugname[21], amount, price;
        if(sscanf(params, "us[21]dd", gpid, drugname, amount, price))
        {
            SendClientMessage(playerid, -1, "{FF6A22}USAGE: {FFFFFF}/selldrugs (/sd) [playerid] [drugname] [amount] [price]");
            return SendClientMessage(playerid, -1, "{FF6A22}Available names: {FFFFFF}Weed, Cocaine, Crack, Meth, Ectasy, Heroin");
        }
        if(!IsPlayerConnected(gpid)) return SendClientMessage(playerid, -1, "The player isn't connected !");
        if(ProxDetectorS(8.0, playerid, gpid))
        {
            if(!strcmp(drugname,"weed",true))
            {
                format(string, sizeof(string), "* You offered to sell %d grams of weed to %s for $%d, Wait for a reply.", amount, giveplayer, price);
                SendClientMessage(playerid, 0x33CCFFAA, string);
                //
                format(string, sizeof(string), "* %s wants to sell %d grams of weed to you for $%d, (type /acceptdrugs cocaine) to buy.", sendername, amount, price);
                SendClientMessage(gpid, 0x33CCFFAA, string);
            }
        }
        else
        {
            SendClientMessage(playerid, -1, "{FF6A22}INFO:{FFFFFF} That player isn't enough close");
        }
        return 1;
    }
    return 0;
}
Reply
#8

Anyone? can someone help with that ? - Strcmp
Reply
#9

pawn Код:
if(strcmp(cmd, "/selldrugs", true) == 0 || strcmp(cmd, "/sd", true) == 0)
    {
        new drug_name[10],amount,price;
        tmp = strtok(cmdtext, idx); // [playerid]

        new gpid = ReturnUser(tmp);
        if(gpid == INVALID_PLAYER_ID) return SendClientMessage(playerid, -1, "{FF6A22}INFO:{FFFFFF} Invalid player ID.");

        strcat(drug_name, strtok(cmdtext, idx)); // [drugname]
        if(!drug_name[0])
        {
            SendClientMessage(playerid, -1, "{FF6A22}USAGE: {FFFFFF}/selldrugs (/sd) [playerid] [drugname] [amount] [price]");
            SendClientMessage(playerid, -1, "{FF6A22}Available names: {FFFFFF}Weed, Cocaine, Crack, Meth, Ectasy, Heroin");
            return 1;
        }

        tmp = strtok(cmdtext, idx); // [amount]
        if(!strlen(tmp))
        {
            // some info here
            return 1;
        }
        amount = strval(tmp);

        tmp = strtok(cmdtext, idx); // [price]
        if(!strlen(tmp))
        {
            // some info here
            return 1;
        }
        price = strval(tmp);

        if(ProxDetectorS(8.0, playerid, gpid))
        {
            new giveplayer[MAX_PLAYER_NAME];
            new sendername[MAX_PLAYER_NAME]
            GetPlayerName(gpid, giveplayer, MAX_PLAYER_NAME);
            GetPlayerName(playerid, sendername, MAX_PLAYER_NAME);
            if(strcmp(drug_name,"weed",true) == 0)
            {
                format(string, sizeof(string), "* You offered to sell %d grams of weed to %s for $%d, Wait for a reply.", amount, giveplayer, price);
                SendClientMessage(playerid, 0x33CCFFAA, string);
                //
                format(string, sizeof(string), "* %s wants to sell %d grams of weed to you for $%d, (type /acceptdrugs cocaine) to buy.", sendername, amount, price);
                SendClientMessage(gpid, 0x33CCFFAA, string);
            }
        }
        else
            SendClientMessage(playerid, -1, "{FF6A22}INFO:{FFFFFF} That player isn't enough close");

        return 1;
    }
    // rest of Commands
    return 0;
}
Reply
#10

Jeff i will test code later im not at home right now.

***** i already wanted to convert whole gamemode of dcmd+sscanf + strcmp into zcmd+sscanf but commands wasnt working thats why i left it as it is. I prefer to script with zcmd + sscanf or yini but server has only oldschool cmd procesors in scrip and i failed with upgrading the code but i will try again can i leave dcmd+sscanf and mix it with zcmd or yini? What is better zcmd or yini? And how to convert in new cmd procesor if every cmd has strtok involved i will need to rescript all cmds?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)