SA-MP Forums Archive
Using String on "OnPlayerText"!! - 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)
+--- Thread: Using String on "OnPlayerText"!! (/showthread.php?tid=539308)



Using String on "OnPlayerText"!! - Juvanii - 27.09.2014

- How to check if the player is writing an integer or normal text??

Hello Guys! I wanna know how to check if the player is writing an integer to save this integer in a specific player's variable.

Example: (I know the code is kinda wrong a lil bit, but it's just an explanation!)
pawn Код:
new Amount[MAX_PLAYERS];
new bool: IsWritingAmount[MAX_PLAYERS];

public OnPlayerText(playerid, text[])
{
    if(IsWritingAmount[playerid] == true)
    {
        if(strval(text) == text)
        {
            new string[50];
            Amount[playerid] = text; // Consider the text was "300"
            format(string, sizeof(string), "You have purchased %i of this thing for some money.", text); //%i would be 300
            SendClientMessage(playerid, -1, string);
        }
        return 0;
    }
    else // if the player writes normal text
    {
        SendClientMessage(playerid, -1, "You should only write an integer at the moment to purchase an amount.")
        return 0;
    }
    return 1;
}



AW: Using String on "OnPlayerText"!! - Nero_3D - 27.09.2014

Use IsNumeric

This is not a native, you will find the download link on the wiki page near the top


Re: Using String on "OnPlayerText"!! - SickAttack - 27.09.2014

pawn Код:
if(strcmp(text, "0", true) == 0 || strval(text) < 0 || strval(text) > 0) // String is numeric.
else // String isn't numeric.
pawn Код:
new Amount[MAX_PLAYERS];

public OnPlayerText(playerid, text[])
{
    if(strcmp(text, "0", true) == 0 || strval(text) < 0 || strval(text) > 0)
    {
        new string[144];
        Amount[playerid] = text;
        format(string, sizeof(string), "You have purchased %d of this thing for some money.", strval(text));
        SendClientMessage(playerid, -1, string);
        return 0;
    }
    else
    {
        SendClientMessage(playerid, -1, "You should only write an integer at the moment to purchase an amount.")
        return 0;
    }
    return 1;
}



Re: Using String on "OnPlayerText"!! - Juvanii - 27.09.2014

Would you please explain this part?..
Why did you compare the text with zero?
pawn Код:
if(strcmp(text, "0", true) == 0)



Re: Using String on "OnPlayerText"!! - Pottus - 27.09.2014

I guess you could also use sscanf as well.

pawn Код:
new amount;
if(sscanf(text, "i", amount)) return SendClientMessage(playerid, -1, "You should only write an integer at the moment to purchase an amount.");
Or strval()

pawn Код:
new amount;
amount = strval(text);
if(amount < 1) return SendClientMessage(playerid, -1, "You should only write an integer at the moment to purchase an amount.");



Re: Using String on "OnPlayerText"!! - Ox1gEN - 27.09.2014

Because zero is an integer.
That's why he compared the text with zero.


Re: Using String on "OnPlayerText"!! - Juvanii - 27.09.2014

Thank you all! Everything is fixed.


Re: Using String on "OnPlayerText"!! - SickAttack - 28.09.2014

Quote:
Originally Posted by Juvanii
Посмотреть сообщение
Would you please explain this part?..
Why did you compare the text with zero?
pawn Код:
if(strcmp(text, "0", true) == 0)
Becuase 'strval' returns '0' if the string isn't numerical.