Can I define a string like this?
#1

I made a Gender in my player saving data, which saves: "Girl" "Female" "Boy" or "Male"

Can I use this?

pawn Код:
stock SexGender(playerid)
{
    new Sex2[128];

    if(IsPlayerConnected(playerid))
    {
        if(Player[playerid][Gender] == "Female" || "Girl")
        {
            Sex2 = "her";
        }
        else
        {
            Sex2 = "his";
        }
    }
    else
    {
        Sex2 = "Disconnected/Nothing";
    }

    return Sex2;
}
or do i have to define this different?
pawn Код:
if(Player[playerid][Gender] == "Female" || "Girl")
Reply
#2

Try with this:

Код:
stock SexGender(playerid)
{
    new Sex2[3]; //IceCube! suggestion "Another off topic tip... You have new sex2[128]; it wastes memory not getting too advanced or there are better ways again but his and her is 3 always so only use the memory needed..."

    if(IsPlayerConnected(playerid))
    {
        //i think you should use this: (1 = girl, 2 = boy)
        if(Player[playerid][Gender] == 1)
        {
            Sex2 = "her";
        }
        else
        {
            Sex2 = "his";
        }
    }

    return Sex2;
}
Reply
#3

No. To compare strings you have to use strcmp; it doesn't act like integer comparisons.

EDIT: However, you COULD make it so the "gender" corresponds with a number defined in your script. So 1 = male and 2 = female.
Reply
#4

I would save those things as IDs to make sure, that I am able to check much easier and faster and also make functions to return different kind of "names" related to gender.

Additionally I want to say, that you have to use strcmp() to compare strings in PAWN.

Valid
pawn Код:
if(!strcmp(string_1, string_2))
Invalid
pawn Код:
if(string_1 == string_2)
Reply
#5

Another off topic tip... You have new sex2[128]; it wastes memory not getting too advanced or there are better ways again but his and her is 3 always so only use the memory needed...

new sex2[3];

I know it exists for a tiny amount of time but every little helps.
Reply
#6

In MySQL you have a data type called 'enum'. You can retrieve a value from this field in both a string representation and an integer representation.
Reply
#7

pawn Код:
stock SexGender(playerid)
{
    new Sex2[128];

    if(IsPlayerConnected(playerid))
    {
        if(Player[playerid][Gender] == 2 || 4)
        {
            Sex2 = "her";
        }
        else
        {
            Sex2 = "his";
        }
    }
    else
    {
        Sex2 = "Disconnected/Nothing";
    }

    return Sex2;
}
So this would work?

2 = Female
4 = Girl, In case they RP as a teen.
Reply
#8

pawn Код:
stock SexGender(playerid)
{
    new Sex2[3];

    if(IsPlayerConnected(playerid))
    {
        if(Player[playerid][Gender] == 2)
        {
            Sex2 = "her";
        }
        else if(Player[playerid][Gender] == 4)
        {
            Sex2 = "his";
        }
    }
    else
    {
        Sex2 = "Disconnected/Nothing";
    }

    return Sex2;
}
Reply
#9

If I were you, I would ditch the whole "girl" or "female" thing as it'll just be more of a pain in the ass to deal with. This is untested code, but I think it will work:

pawn Код:
stock returnPlayerGender(playerid)
{
    if(playerid != INVALID_PLAYER_ID)
    {
        new
            szGender[7];
       
        switch(Player[playerid][Gender])
        {
            case 2, 4: szGender = "Female";
            case 1, 3: szGender = "Male";
        }
    }
    else szGender = "N/A";
    return szGender;
}
Reply
#10

Quote:
Originally Posted by RealCop228
Посмотреть сообщение
If I were you, I would ditch the whole "girl" or "female" thing as it'll just be more of a pain in the ass to deal with. This is untested code, but I think it will work:

pawn Код:
stock returnPlayerGender(playerid)
{
    if(playerid != INVALID_PLAYER_ID)
    {
        new
            szGender[7];
       
        switch(Player[playerid][Gender])
        {
            case 2, 4: szGender = "Female";
            case 1, 3: szGender = "Male";
        }
    }
    else szGender = "N/A";
    return szGender;
}
Once again you gave the fixing answer
Thank you very much. Sadly I can't rep you 1thousend times :P
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)