Using String on "OnPlayerText"!!
#1

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

Use IsNumeric

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

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

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

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.");
Reply
#6

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

Thank you all! Everything is fixed.
Reply
#8

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


Forum Jump:


Users browsing this thread: