Quick question
#1

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

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

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

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
Reply
#5

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

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

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

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


Forum Jump:


Users browsing this thread: 1 Guest(s)