SA-MP Forums Archive
Can I define a string like this? - 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: Can I define a string like this? (/showthread.php?tid=436050)



Can I define a string like this? - Stefand - 09.05.2013

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")



Respuesta: Can I define a string like this? - LoLeRo - 09.05.2013

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;
}



Re: Can I define a string like this? - Scenario - 09.05.2013

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.


AW: Can I define a string like this? - BigETI - 09.05.2013

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)



Re: Can I define a string like this? - IceCube! - 09.05.2013

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.


Re: Can I define a string like this? - Vince - 09.05.2013

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.


Re: Can I define a string like this? - Stefand - 09.05.2013

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.


Respuesta: Can I define a string like this? - LoLeRo - 09.05.2013

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;
}



Re: Can I define a string like this? - Scenario - 09.05.2013

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;
}



Re: Can I define a string like this? - Stefand - 09.05.2013

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