Little help with variables please...
#1

Can someone tell me how to script a cmd with two variables?
For example /pay ID Amount
I' v looked in internet but havent found the way yet.. :S
for one variable it' s easy, but with two - i' v tried many different ways but none of them works correct..
Reply
#2

Sure! First off, you'll need sscanf and ZCMD.

Then put this ontop of your script:

pawn Код:
#include <ZCMD>
#include <sscanf2>
Now, you'll need to actually do some research on the sscanf topic to learn about operators and such, but it's well worth learning.


Here's a basic "shell" for a ZCMD command:
pawn Код:
COMMAND:pay(playerid, params[])
{
    return 1;
}
Now we will add the code, starting with the beginning of the sscanf "declaration" i wanna call it, but it's obviously not, i just can't think of the word at the moment considering it's 8 AM.

pawn Код:
COMMAND:pay(playerid, params[])
{
    new
        PlayerBeingPaid,
        AmountBeingPaidToPlayer;
       
    if(sscanf(params, "ui", PlayerBeingPaid, AmountBeingPaidToPlayer))
        //What happens if they don't use the correct syntax; Usage message?
        return SendClientMessage(playerid, -1, "{FFFFFF}USAGE: /Pay [Player ID] [Amount]");
       
    //The rest of your code - What happens if they do use the correct syntax?
    if(GetPlayerMoney(playerid) >= AmountBeingPaidToPlayer) //Do they have enough money? If so, continue.
    {
        GivePlayerMoney(PlayerBeingPaid, AmountBeingPaidToPlayer);
        GivePlayerMoney(playerid, -AmountBeingPaidToPlayer);
    }
    //If not - Error message
    else return SendClientMessage(playerid, -1, "{FFFFFF}ERROR: You don't have enough money!");
    return 1;
}
I hope i've actually taught you something.
Reply
#3

THANXXX!!!! For sure you are.. Now ima study this one and put in in good use..
Reply
#4

Quote:
Originally Posted by 22reiko
Посмотреть сообщение
THANXXX!!!! For sure you are.. Now ima study this one and put in in good use..
Great to hear!
Reply
#5

P.s. Can i ask where do you assign the values you input to a variable? Or it somehow understands automatically? I mean..

if (strcmp(cmd, "/skin", true) == 0)
{
tmp = strtok(cmdtext,idx);
IDT = strval(tmp); <---------- This part..
SetPlayerSkin(playerid,IDT);
return 1;
}
Reply
#6

sscanf does that

pawn Код:
if(sscanf(params, "ui", PlayerBeingPaid, AmountBeingPaidToPlayer))
sscanf alias unformat reads from the string parameter params and put the data in the variables on the right side

the format parameter "ui" tells sscanf how to unformat the string params

u - playername or playerid
i - integer

You should read the sscanf 2.0 thread, use ****** to find it (that easier)
Reply
#7

Quote:
Originally Posted by 22reiko
Посмотреть сообщение
P.s. Can i ask where do you assign the values you input to a variable? Or it somehow understands automatically? I mean..

if (strcmp(cmd, "/skin", true) == 0)
{
tmp = strtok(cmdtext,idx);
IDT = strval(tmp); <---------- This part..
SetPlayerSkin(playerid,IDT);
return 1;
}
As Nero stated above me, that's what sscanf is for. The code you posted (I recommend using [*pawn][*/pawn] tags on the forum) uses strtok, which is a very inefficient way to create your commands.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)