SA-MP Forums Archive
Quick question - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Quick question (/showthread.php?tid=268556)



Quick question - Skylar Paul - 12.07.2011

Well, i'm trying to combine two stocks, and just create a single one for no particular reason other than practice.

My question to you would be, would this work?

pawn Код:
stock GetPartOfName(playerid, part[2])
{
    new
        namepart[2][MAX_PLAYER_NAME],
        PlayerName[MAX_PLAYER_NAME];

    if(part == 0) {
        GetPlayerName(playerid, PlayerName, sizeof(PlayerName));
        split(PlayerName, namepart, '_');
        return namepart[0];
    }

    else if(part == 1) {
        GetPlayerName(playerid, PlayerName, sizeof(PlayerName));
        split(PlayerName, namepart, '_');
        return namepart[1];
    }

    else return null;
}
Player name: Stephen_Hawking

Usage:
pawn Код:
GetPartOfName(playerid, 0);
Returns: "Stephen"
Or, using the other aspect of it:

Usage:
pawn Код:
GetPartOfName(playerid, 1);
Returns: "Hawking"
It compiles without errors, but i'm not sure; Oh, and before you tell me to go test it myself, I don't have GTA installed.


Re : Quick question - sasuke78200 - 13.07.2011

OnGameModeInit copy that
Код:
new blabla[2][MAX_PLAYER_NAME];
GetPartOfName("Stephen_Hawking", blabla);
and you can test without GTA
Код:
stock GetPartOfName(/*playerid*/PlayerName[], part[2])
{
    new
        namepart[2][MAX_PLAYER_NAME];/*,
        PlayerName[MAX_PLAYER_NAME];*/

    if(part == 0) {
//        GetPlayerName(playerid, PlayerName, sizeof(PlayerName));
        split(PlayerName, namepart, '_');
        return namepart[0];
    }

    else if(part == 1) {
 //       GetPlayerName(playerid, PlayerName, sizeof(PlayerName));
        split(PlayerName, namepart, '_');
        return namepart[1];
    }

    else return null;
}



Re: Quick question - Daren_Jacobson - 13.07.2011

Boom, Roasted.
pawn Код:
stock GetPartOfName(playerid, part)
{
    new
        namepart[2][MAX_PLAYER_NAME],
        PlayerName[MAX_PLAYER_NAME];
    if (part > 2 || part < 1) return null; // supposing null is a null string.

    GetPlayerName(playerid, PlayerName, sizeof(PlayerName));
    split(PlayerName, namepart, '_');
    return namepart[part - 1];
}
Note that only handles up to two parts, so you could not retrieve a name such as Eryn_Martin_Miller


AW: Re: Quick question - Nero_3D - 13.07.2011

Quote:
Originally Posted by Daren_Jacobson
Посмотреть сообщение
Note that only handles up to two parts, so you could not retrieve a name such as Eryn_Martin_Miller
Yeah that is one of the main problems of the split function

pawn Код:
stock GetPartOfName(playerid, part) {
    new
        name[MAX_PLAYER_NAME];
    if(GetPlayerName(playerid, name, sizeof name)) {
        new
            i = 0,
            idx = 0;
        for( ; name[i]; ++i) {
            if(name[i] == '_') {
                if((--part) == 0) {
                    name[i] = EOS;
                    return name[idx];
                }
                idx = i + 1;
            }
        }
        if((--part) == 0) {
            name[i] = EOS;
            return name[idx];
        }
        name[0] = EOS;
    }
    return name;
}
this will return an empty string if the part isnt available


Re: AW: Re: Quick question - Daren_Jacobson - 13.07.2011

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
Yeah that is one of the main problems of the split function
Or I could just change namepart to be 3 slots big, and change my if to be larger than 3.


AW: Re: AW: Re: Quick question - Nero_3D - 13.07.2011

Quote:
Originally Posted by Daren_Jacobson
Посмотреть сообщение
Or I could just change namepart to be 3 slots big, and change my if to be larger than 3.
Than what about Karl_II_the_Bald
Hope you get what I mean


Re: Quick question - Roko_foko - 13.07.2011

EOS stands for End Of String and it is same as '\0' and 0, right?


Re: AW: Re: AW: Re: Quick question - Daren_Jacobson - 13.07.2011

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
Than what about Karl_II_the_Bald
Hope you get what I mean
Then change it to 4, or MAX_PLAYER_NAME/2 for all I care.