[Tutorial] Using Zcmd and Sscanf
#21

Nice
Reply
#22

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...
Reply
#23

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
Reply
#24

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;
}
Reply
#25

I very need it, thanks
Reply
#26

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.
Reply
#27

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.
Reply
#28

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.
Reply
#29

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
Reply
#30

Nice tutorial, good for learners.
Reply
#31

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

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.
Reply
#33

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

I prefer strcmp.
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)