SA-MP Forums Archive
[Tutorial] Using Zcmd and 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)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Using Zcmd and Sscanf (/showthread.php?tid=244841)

Pages: 1 2


Re: Using Zcmd and Sscanf - ricardo178 - 03.04.2011

Nice


Re: Using Zcmd and Sscanf - omer5198 - 03.04.2011

Quote:
Originally Posted by Mr_Scripter
View Post
ok thanks in strcmp it's only playerid so i don't understand it now i know thanks
in strcmp there is other ids... with strtok... like:
...
...
...
new tmp[256];
tmp = strtock(cmdtext, idx);
...
...
...
new id;
id = strval(tmp);
...
i put the ... because there are more between, before and after what i wrote in the commands...


Re: Using Zcmd and Sscanf - bestr32 - 03.04.2011

Wan't givecash command? it's strcmp()
pawn Code:
if(strcmp(cmd, "/givecash", true) == 0)
     {
        new tmp[256];
        new string[128];
        new playermoney;
        new sendername[MAX_PLAYER_NAME];
        new giveplayer[MAX_PLAYER_NAME];
        new giveplayerid, moneys;
        tmp = strtok(cmdtext, idx);

        if(!strlen(tmp))
        {
            SendClientMessage(playerid, red, "USAGE: /givecash [playerid] [amount]");
            return 1;
        }
        giveplayerid = strval(tmp);

        tmp = strtok(cmdtext, idx);
        if(!strlen(tmp))
        {
            SendClientMessage(playerid, red, "USAGE: /givecash [playerid] [amount]");
            return 1;
        }
        moneys = strval(tmp);

        //printf("givecash_command: %d %d",giveplayerid,moneys);

        if (IsPlayerConnected(giveplayerid))
        {
            GetPlayerName(giveplayerid, giveplayer, sizeof(giveplayer));
            GetPlayerName(playerid, sendername, sizeof(sendername));
            playermoney = GetPlayerMoney(playerid);

            if (moneys > 0 && playermoney >= moneys)
            {
                GivePlayerMoney(playerid, (0 - moneys));
                GivePlayerMoney(giveplayerid, moneys);
                format(string, sizeof(string), "You gave %s(player: %d), $%d.", giveplayer,giveplayerid, moneys);
                SendClientMessage(playerid, green, string);
                format(string, sizeof(string), "You got $%d from %s(player: %d).", moneys, sendername, playerid);
                SendClientMessage(giveplayerid, green, string);
                PlayerPlaySound(playerid,1084,0.0,0.0,0.0);
                printf("%s(playerid:%d) has transfered %d to %s(playerid:%d)",sendername, playerid, moneys, giveplayer, giveplayerid);
                }
            else
            {
                SendClientMessage(playerid, red, "You dont have so much ;)");
            }
        }
        else
        {
            format(string, sizeof(string), "%d not found.", giveplayerid);
            SendClientMessage(playerid, red, string);
        }
        return 1;
    }
At OnPlayerCommandText add:
pawn Code:
new cmd[256];
      cmd = strtok(cmdtext, idx);
And it will look like:
pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
       
        new cmd[256];
        cmd = strtok(cmdtext, idx);
    if(strcmp(cmd, "/givecash", true) == 0)
     {
        new tmp[256];
        new string[128];
        new playermoney;
        new sendername[MAX_PLAYER_NAME];
        new giveplayer[MAX_PLAYER_NAME];
        new giveplayerid, moneys;
        tmp = strtok(cmdtext, idx);

        if(!strlen(tmp))
        {
            SendClientMessage(playerid, red, "USAGE: /givecash [playerid] [amount]");
            return 1;
        }
        giveplayerid = strval(tmp);

        tmp = strtok(cmdtext, idx);
        if(!strlen(tmp))
        {
            SendClientMessage(playerid, red, "USAGE: /givecash [playerid] [amount]");
            return 1;
        }
        moneys = strval(tmp);

        //printf("givecash_command: %d %d",giveplayerid,moneys);

        if (IsPlayerConnected(giveplayerid))
        {
            GetPlayerName(giveplayerid, giveplayer, sizeof(giveplayer));
            GetPlayerName(playerid, sendername, sizeof(sendername));
            playermoney = GetPlayerMoney(playerid);

            if (moneys > 0 && playermoney >= moneys)
            {
                GivePlayerMoney(playerid, (0 - moneys));
                GivePlayerMoney(giveplayerid, moneys);
                format(string, sizeof(string), "You gave %s(player: %d), $%d.", giveplayer,giveplayerid, moneys);
                SendClientMessage(playerid, green, string);
                format(string, sizeof(string), "You got $%d from %s(player: %d).", moneys, sendername, playerid);
                SendClientMessage(giveplayerid, green, string);
                PlayerPlaySound(playerid,1084,0.0,0.0,0.0);
                printf("%s(playerid:%d) has transfered %d to %s(playerid:%d)",sendername, playerid, moneys, giveplayer, giveplayerid);
                }
            else
            {
                SendClientMessage(playerid, red, "You dont have so much ;)");
            }
        }
        else
        {
            format(string, sizeof(string), "%d not found.", giveplayerid);
            SendClientMessage(playerid, red, string);
        }
        return 1;
    } // And if u have return 0; here. Than the bracket



Re: Using Zcmd and Sscanf - Markx - 03.04.2011

Use sscanf and Zcmd..

pawn Code:
COMMAND:givemoney(playerid, params[])
{
    if (IsPlayerAdmin(playerid))
    {
        new
          toplayerid, // the player we want to give money to
          amount;
        // extracting player's ID and amount from params
        if (!sscanf(params, "ui", toplayerid, amount))
        {
          if (toplayerid != INVALID_PLAYER_ID)
          {
            new
              message[40];
            GivePlayerMoney(toplayerid, amount);
            format(message, sizeof(message), "You got $%d from admin!", amount);
            SendClientMessage(toplayerid, 0x00FF00FF, message);
          }
          else SendClientMessage(playerid, 0xFF0000FF, "That player is not connected");
        }
        else SendClientMessage(playerid, 0xFFFFFFFF, "Usage: /givemoney <playerid> <amount>");
    }
    else SendClientMessage(playerid, 0xFF0000FF, "Only admins can use this command!");
    return 1;
}



Re: Using Zcmd and Sscanf - Arnoldas - 27.04.2011

I very need it, thanks


Re: Using Zcmd and Sscanf - Joe Staff - 27.04.2011

When the command only has 1 parameter, there's absolutely no need to use sscanf. Running sscanf is like running multiple conditionals based on the parameters given and the parameters requested.

For example, if you're looking for an integer, you would use something like this:
pawn Code:
//Command
COMMAND:roll(playerid, params[])
{
    new value=strval(params);

    if(value<=0) return SendClientMessage(playerid,0xFF0000FF,"You must put in a value between 1 and 99");
    if(value>=100)return SendClientMessage(playerid,0xFF0000FF,"You must put in a value between 1 and 99");

    new tmpstr[32];

    GetPlayerName(playerid,tmpstr,24);
    format(tmpstr,32,"%s rolled a dice (1-%02d) - Dice landed on %d",tmpstr,value,random(value));

    return SendClientMessageToAll(0xFFFF55FF,tmpstr);
}
As you can see, performing only 2 conditionals is a lot faster than running sscanf, which has to do more work for the same affect.


Re: Using Zcmd and Sscanf - Markx - 28.04.2011

Quote:
Originally Posted by SilentHuntR
View Post
When the command only has 1 parameter, there's absolutely no need to use sscanf. Running sscanf is like running multiple conditionals based on the parameters given and the parameters requested.

For example, if you're looking for an integer, you would use something like this:
pawn Code:
//Command
COMMAND:roll(playerid, params[])
{
    new value=strval(params);

    if(value<=0) return SendClientMessage(playerid,0xFF0000FF,"You must put in a value between 1 and 99");
    if(value>=100)return SendClientMessage(playerid,0xFF0000FF,"You must put in a value between 1 and 99");

    new tmpstr[32];

    GetPlayerName(playerid,tmpstr,24);
    format(tmpstr,32,"%s rolled a dice (1-%02d) - Dice landed on %d",tmpstr,value,random(value));

    return SendClientMessageToAll(0xFFFF55FF,tmpstr);
}
As you can see, performing only 2 conditionals is a lot faster than running sscanf, which has to do more work for the same affect.
Ummm i dont think so, sscanf is faster always, ask Y_Less.


Re: Using Zcmd and Sscanf - xRyder - 28.04.2011

Quote:
Originally Posted by Markx
View Post
Ummm i dont think so, sscanf is faster always, ask Y_Less.
No, actually SilentHuntR is right. When you're creating command that uses only one parameter there is no need at all to use sscanf. It will just use memory for nothing.


Re: Using Zcmd and Sscanf - Markx - 29.04.2011

Quote:
Originally Posted by xRyder
View Post
No, actually SilentHuntR is right. When you're creating command that uses only one parameter there is no need at all to use sscanf. It will just use memory for nothing.
Yeah i guess so, but sscanf is better for me :P


Re: Using Zcmd and Sscanf - Shubham - 08.05.2011

Nice tutorial, good for learners.


Re: Using Zcmd and Sscanf - Markx - 11.05.2011

Quote:
Originally Posted by Shubham
View Post
Nice tutorial, good for learners.
Thanks bro ^^


Re: Using Zcmd and Sscanf - roadless - 12.05.2011

Hello and thanks for the tutorial!

I do have a question about subcommands. Is there any posibillity that I can do commands like: /phone call <number>?

What I mean is that the /phone commands should have some subcommands like: call, toggle, text and so on.


Re: Using Zcmd and Sscanf - Mr.Carson Clay - 07.02.2012

Can you make a TUT on how to convert strcmp zcmd+sscanf?


Re: Using Zcmd and Sscanf - lsfmd - 07.02.2012

I prefer strcmp.