[SOLVED \o/]sscanf and multiple optional parameters
#11

Here's a snippet I made some time ago for a script
pawn Код:
new
        car[20],
        col1 = -1,
        col2 = -1,
        spaces = 0;
    if (sscanf(params, "sz", car, msg4))
    {
        SendClientMessage(playerid, COLOR_SYSTEM, "*** USAGE: /gangcar [carname] [custom name] [color1] [color2]");
    SendClientMessage(playerid, COLOR_SYSTEM, "*** You need to be in a turf you own to use this command.");
    SendClientMessage(playerid, COLOR_SYSTEM, "*** Color1 and Color2 are optional.");
    return 1;
    }
  new lenght, idx;
    for(lenght = strlen(msg4), idx = 0; idx < lenght; idx++) //checks for spaces (name_SPACE_color1_SPACE_color2)
    {
      if(msg4[idx] == ' ')
      {
        spaces++;
      }
    }

    if(spaces > 2) //more then just 3 words, cancel the process
    {
        SendClientMessage(playerid, COLOR_SYSTEM, "*** USAGE: /gangcar [carname] [custom name] [color1] [color2]");
        SendClientMessage(playerid, COLOR_SYSTEM, "*** You need to be in a turf you own to use this command.");
        SendClientMessage(playerid, COLOR_SYSTEM, "*** Color1 and Color2 are optional.");
        return 1;
    }
    else if(spaces == 0) //just the car name, store in the global variable
    {
      format(CarName[ turf ], MAX_PLAYER_NAME, "%s", msg4);
    }
    else if (spaces == 1) //one parameter to much, the last one will be ignored
    {
        if (sscanf(msg4, "sz", CarName[ turf ], msg1))
        {
            SendClientMessage(playerid, COLOR_SYSTEM, "*** USAGE: /gangcar [carname] [custom name] [color1] [color2]");
            SendClientMessage(playerid, COLOR_SYSTEM, "*** You need to be in a turf you own to use this command.");
            SendClientMessage(playerid, COLOR_SYSTEM, "*** Color1 and Color2 are optional.");
            return 1;
        }
    }
    else if (spaces == 2)//car name, col1, col2 = perfect
    {
        if (sscanf(msg4, "sdd", CarName[ turf ], col1, col2))
        {
            SendClientMessage(playerid, COLOR_SYSTEM, "*** USAGE: /gangcar [carname] [custom name] [color1] [color2]");
            SendClientMessage(playerid, COLOR_SYSTEM, "*** You need to be in a turf you own to use this command.");
            SendClientMessage(playerid, COLOR_SYSTEM, "*** Color1 and Color2 are optional.");
            return 1;
        }
    }
It can be vastly improved, but you'll get the idea. Or you could try
pawn Код:
new id, col1, col2, string[128];
sscanf(cmdtext, "dz", id, string);

if(sscanf(string, "dd", col1, col2))
{
    //use random colors
}
Reply


Messages In This Thread
[SOLVED \o/]sscanf and multiple optional parameters - by dcmd_crash - 22.04.2010, 00:03
Re: sscanf and multiple optional parameters - by cessil - 22.04.2010, 04:34
Re: sscanf and multiple optional parameters - by dcmd_crash - 22.04.2010, 18:09
Re: sscanf and multiple optional parameters - by ¤Adas¤ - 22.04.2010, 18:23
Re: sscanf and multiple optional parameters - by dcmd_crash - 22.04.2010, 18:30
Re: sscanf and multiple optional parameters - by dcmd_crash - 23.04.2010, 14:33
Re: sscanf and multiple optional parameters - by woot - 23.04.2010, 17:04
Re: sscanf and multiple optional parameters - by dcmd_crash - 23.04.2010, 17:10
Re: sscanf and multiple optional parameters - by biltong - 23.04.2010, 17:31
Re: sscanf and multiple optional parameters - by Calgon - 23.04.2010, 17:39
Re: sscanf and multiple optional parameters - by dice7 - 23.04.2010, 17:40
Re: sscanf and multiple optional parameters - by dcmd_crash - 23.04.2010, 17:54
Re: sscanf and multiple optional parameters - by dcmd_crash - 24.04.2010, 21:31
Re: sscanf and multiple optional parameters - by woot - 24.04.2010, 21:41
Re: sscanf and multiple optional parameters - by dcmd_crash - 24.04.2010, 21:48
Re: sscanf and multiple optional parameters - by woot - 24.04.2010, 21:54
Re: sscanf and multiple optional parameters - by dcmd_crash - 24.04.2010, 22:10
Re: sscanf and multiple optional parameters - by biltong - 24.04.2010, 22:16
Re: sscanf and multiple optional parameters - by dcmd_crash - 24.04.2010, 22:25
Re: sscanf and multiple optional parameters - by Virtual1ty - 24.04.2010, 22:32
Re: sscanf and multiple optional parameters - by dcmd_crash - 24.04.2010, 23:20
Re: sscanf and multiple optional parameters - by Dabombber - 25.04.2010, 02:38
Re: sscanf and multiple optional parameters - by woot - 25.04.2010, 09:00
Re: sscanf and multiple optional parameters - by Dabombber - 25.04.2010, 11:32
Re: sscanf and multiple optional parameters - by dcmd_crash - 25.04.2010, 21:06
Re: sscanf and multiple optional parameters - by Dabombber - 25.04.2010, 23:34
Re: sscanf and multiple optional parameters - by dcmd_crash - 25.04.2010, 23:41
Re: sscanf and multiple optional parameters - by dcmd_crash - 25.04.2010, 23:51
Re: sscanf and multiple optional parameters - by Calgon - 26.04.2010, 00:32
Re: sscanf and multiple optional parameters - by dcmd_crash - 26.04.2010, 00:36

Forum Jump:


Users browsing this thread: 1 Guest(s)