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;
}
GetPartOfName(playerid, 0);
Returns: "Stephen"
GetPartOfName(playerid, 1);
Returns: "Hawking"
new blabla[2][MAX_PLAYER_NAME]; GetPartOfName("Stephen_Hawking", blabla);
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; }
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
|
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;
}
Or I could just change namepart to be 3 slots big, and change my if to be larger than 3.
|