Some good examples of using dcmd and sscanf together?
#1

I've searched on the wiki and i didnt find it as useful as i thought it should be.

For example. I had to use "i" instead of "u" for specifying a players id. If i used "u" it would perform the command on me and not the player specified.
This happened when i copied the example command exactly as shown...

Next thing, i want to use an optional integer param, but i haven't found anywhere explaining how.

With strtok its easy, but i dont know where i can find info on it with sscanf.

For example...

i want to make a command to set a players car colour... "/pcarcolor"

If i use /pcarcolor 1 3 (black ,red) it will turn a car or bike black and red provided its able to use two colours.

if i use /pcarcolor 3 (red) it will change the car or bike to red only. Meaning that the second colour wasnt specified, so it automatically becomes the first specified colour.

Or to use a command to display information about a player, "/pkills [playerid]" now this should display their kills. But maybe i want to reset or edit them and use /pkills [playerid] [amount]

how can i do this with sscanf?
Reply
#2

Doesn't anyone use dcmd and sscanf, or everyone like me? only minimal knowledge on it?
Reply
#3

pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{/*declaring the command*/
  dcmd(pcarcolor, 9, cmdtext); // the number 9 is the number of characters in "pcarcolor"
  return 0;
}

dcmd_pcarcolor(playerid, params[])
{
  new col1, col2;
  /*params is the string, which you wrote after "/pcarcolor". Lets say you wrote "/pcarcolor 0 1"*/
  /*params is then " 0 1"*/
  /*the first parameter in sscanf is the string which we will be looking at it. It will be params*/
  /*The second parameter is what we will be looking for. 'd' means we will be looking for an integer*/
  /*Check here for other stuff you can search with sscanf*/
  /*[url]https://sampwiki.blast.hk/wiki/Fast_Commands#Data_types*/[/url]
  /*There are 2 'd' because we will be searching for 2 integers. If you would be looking for an integer and a string, it would be "ds"*/
  /*The other parameters are the places where the integers will be saved. The first integer ('0', if you typed "/pcarcolor 0 1") will be saved in 'col1' and the second (1) will be saved in 'col2'*/
  if (sscanf(params, "dd", col1, col2))
  {
    /*sscanf returns TRUE if the user imputed the text WRONG (eg, did /pcarcolor lol 2),
    and returns FALSE if the user IMPUTED THE TEXT CORRECTLY (eg, did /pcarcolor 1 2)*/

    SendClientMessage(playerid, COLOR_RED, "USAGE: /pcarcolor [color1] [color2]");
    return 1; /*he imputed it wrong, so lets end the code here*/
  }

  /*he imputed it wright, so lets continue*/
 
  /*lets change the colors*/
  ChangeVehicleColor(GetPlayerVehicleID(playerid),col1, col2); //col1 contains 0, col2 contains 1
  return 1;
Reply
#4

I have that, for changing my own vehicle colour.

I need to use /pcarcolor for changing a players car colour..

/pcarcolor [playerid] [col1] [col2]

but if col2 isnt specified then col2 = col1

I dont know how to make col2 an optional integer..

strtok seems a lot more flexable..
Reply
#5

You have everything explained right here: https://sampwiki.blast.hk/wiki/Dcmd

And it's much easier and faster working with sscanf than strtok
Reply
#6

I've already read through that...

It says that "u" would work for a players id or name... it doesnt, i had to use "i" or "d" instead...

It mentioned optional strings, but nothing about optional integer.

strtok maybe slower, by .000000000000000000000000001 of a second, but seems way more flexable for things like this... Where as sscanf seems alright for more basic commands.
Reply
#7

"u" seems to be working totally fine for me.
Make sure you have the lastest sscanf since old versions didn't have ReturnUser included.
Reply
#8

Quote:
Originally Posted by Outbreak
I've already read through that...

It says that "u" would work for a players id or name... it doesnt, i had to use "i" or "d" instead...

It mentioned optional strings, but nothing about optional integer.

strtok maybe slower, by .000000000000000000000000001 of a second, but seems way more flexable for things like this... Where as sscanf seems alright for more basic commands.
You are lying sir, with dcmd and sscanf you can easy make strings, decimals, floats etc. WAY more EASYER and FASTER!!

See this how dificult:

usage of strtok for an /announce command:
pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
    new string[128];
    new cmd[128];
    new tmp[128];
    new giveplayerid, idx;
    cmd = strtok(cmdtext, idx);

    if(strcmp(cmd, "/announce", true) == 0)
    {
        if(IsPlayerAdmin(playerid))
        {
            new length = strlen(cmdtext);
            while ((idx < length) && (cmdtext[idx] <= ' '))
            {
                idx++;
            }
            new offset = idx;
            new result[64];
            while ((idx < length) && ((idx - offset) < (sizeof(result) - 1)))
            {
                result[idx - offset] = cmdtext[idx];
                idx++;
            }
            result[idx - offset] = EOS;
            if(!strlen(result))
            {
                SendClientMessage(playerid, COLOR_GREY, "USAGE: /announce [text]");
                return 1;
            }
            GameTextForAll(string, 5000, 4);
            return 1;
        }
        else
        {
            SendClientMessage(playerid, COLOR_GREY, "You are not an admin!");
        }
        return 1;
    }
    return 0;
}

strtok(const string[], &index)
{
    new length = strlen(string);
    while ((index < length) && (string[index] <= ' '))
    {
        index++;
    }

    new offset = index;
    new result[20];
    while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
    {
        result[index - offset] = string[index];
        index++;
    }
    result[index - offset] = EOS;
    return result;
}
usage of sscanf for an /announce command:
pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
    dcmd(announce, 8, cmdtext);
    return 0;
}

dcmd_announce(playerid, params[])
{
    new text[128];
    if(sscanf(params, "s", text))
    {
        SendClientMessage(playerid, COLOR_GREY, "USAGE: /announce [text]");
        return 1;
    }
    if(IsPlayerAdmin(playerid))
    {
        GameTextForAll(text, 5000, 4);
    }
    else
    {
        SendClientMessage(playerid, COLOR_GREY, "You are not an admin!");
    }
    return 1;
}
Reply
#9

Actually, the one you gave can be done even better

Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
  dcmd(announce, 8, cmdtext);
  return 0;
}

dcmd_announce(playerid, params[])
{
  if(params[0]==0) {SendClientMessage(playerid, COLOR_GREY, "USAGE: /announce [text]"); return 1;}

  if(IsPlayerAdmin(playerid)) GameTextForAll(params, 5000, 4);
  else SendClientMessage(playerid, COLOR_GREY, "You are not an admin!");
  return 1;
}
You can see an example over here: http://h-rp.pl/wiki/funkcja/sscanf
Reply
#10

Quote:
Originally Posted by Outbreak
I have that, for changing my own vehicle colour.

I need to use /pcarcolor for changing a players car colour..

/pcarcolor [playerid] [col1] [col2]

but if col2 isnt specified then col2 = col1

I dont know how to make col2 an optional integer..

strtok seems a lot more flexable..
pawn Код:
dcmd_pcarcolor(palyerid,params[])
{
    new id,pName[MAX_PLAYER_NAME],idName[MAX_PLAYER_NAME],string[256],string2[256],color1,color2;
    if(sscanf(params,"uzz",id,color1,color2)) return SendClientMessage(playerid,0xA9A9A9AA,".: Usage: /pcarcolor [playerid/partofname] [color1] [color2] :.");
    if(id == INVALID_PLAYER_ID) return SendClientMessage(playerid,0xAA3333AA,".: Info: Player not found :.");
    GetPlayerName(playerid,pName,256);
    GetPlayerName(id,idName,256);
    new vehicle = GetPlayerVehicleID(playerid);
    ChangeVehicleColor(vehicle,color1,color2);
    format(string,256,".: Info: %s has changed your car color :.",pName);
    format(string2,256,".: Info: You've changed %s's car color :.",idName);
    SendClientMessage(playerid,0x9ACD32AA,string2);
    SendClientMessage(id,0x9ACD32AA,string);
    return 1;
}
Not tested but it should work.

if(sscanf(params,"uzz",id,color1,color2))

In normal case I would put "udd" at the sscanf. but you said you would like them as optional...

"z" is optional things...
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)