PDA

View Full Version : Firstname lastname


CalvinC
25/12/2014, 01:11 PM
If a player has ethically this name: Super_Man
How do you get the words specifically on each side of the line?
Like instead of doing:

format(string, sizeof(string), "{33AA33}Full name: {FFFFFF}%s", RPN(playerid));
SendClientMessage(playerb, COLOR_WHITE, string);

You could do:

format(string, sizeof(string), "{33AA33}Firstname: {FFFFFF}%s", ??(playerid));
SendClientMessage(playerb, COLOR_WHITE, string);
format(string, sizeof(string), "{33AA33}Lastname: {FFFFFF}%s", ??(playerid));
SendClientMessage(playerb, COLOR_WHITE, string);

Flori
25/12/2014, 01:19 PM
You can check with strfind where the '_' is.

Then you can use the first letters till the output of strfind for the first name. Same with the lastname.

http://wiki.sa-mp.com/wiki/Strfind

CalvinC
25/12/2014, 01:23 PM
I don't really know how to do that though.

I have a code to remove the underscore, could it be worked out from that?
stock RemoveUnderScore(playerid)
{
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
for(new i = 0; i < MAX_PLAYER_NAME; i++)
{
if(name[i] == '_') name[i] = ' ';
}
return name;
}

Flori
25/12/2014, 01:38 PM
I don't understand why removing that _ now. :P

It's easy to detect it in a string(the playername) and then you can make 2 strings out of this one.

Read 2 times how strfind works then you will understand how this comes in handy. It's the output that strfind gives.

CalvinC
25/12/2014, 02:32 PM
It's easy to detect it in a string(the playername) and then you can make 2 strings out of this one.
It's the output that strfind gives.

First of all, how am i supposed to make 2 strings out of a strfind line, and make them work giving firstname lastname to put somewhere else, and i don't even understand what the output is?

(const string[],const sub[],bool:ignorecase=false,pos=0)
const string[] The string you want to search in (haystack).
const sub[] The string you want to search for (needle).
ignorecase (optional) When set to true, the case doesn't matter - HeLLo is the same as Hello. When false, they're not the same.
Position (optional)

This is the wiki page for strfind, i don't see how that's gonna divide firstname and lastname from each other, and them make a parameter(think its called) to put it in another string.

Flori
25/12/2014, 02:42 PM
I make a a very short example: :)



strfind("hello_lalala"/*get the name from a player, just an example here*/,"_",false)
//now it will give "5" as output. So you can create a code now in which you use the first 5 letters for the "firstname and the 6+ letters for the lastname

CalvinC
25/12/2014, 07:08 PM
I make a a very short example: :)



strfind("hello_lalala"/*get the name from a player, just an example here*/,"_",false)
//now it will give "5" as output. So you can create a code now in which you use the first 5 letters for the "firstname and the 6+ letters for the lastname



I kind of understand it, but not everyone has a specific number of characters as first/last name. And i don't know how to make a code to get the first/last name and convert it so that it can be used in a string as Firstname(playerid) and Lastname(playerid) instead of RPN(playerid).

CalvinC
26/12/2014, 05:25 PM
Bump.

jackx3rx
26/12/2014, 05:35 PM
stock GetPlayerFirstName(playerid)
{
new namestring[2][MAX_PLAYER_NAME], name[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,MAX_PLAYER_NAME);
split(name, namestring, '_');
return namestring[0];
}


or


new name[MAX_PLAYER_NAME], find, length;
GetPlayerName(playerid,name,sizeof(name));
find = strfind(name,"_",false);
length = strlen(name);
strdel(name,find,length);

// Players first name is stored in 'name'.

MrCallum
26/12/2014, 05:35 PM
Couldn't you do this?

new string[128];
format(STRING, "[SERVER]: %s has joined the server!", GetPlayerName(playerid));
SendClientMessageToAll(COLOR_YELLOW, string);

Or something like that to show a person has joined with _ in the name?

CalvinC
26/12/2014, 06:09 PM
Couldn't you do this?

new string[128];
format(STRING, "[SERVER]: %s has joined the server!", GetPlayerName(playerid));
SendClientMessageToAll(COLOR_YELLOW, string);

Or something like that to show a person has joined with _ in the name?

Uhh, sure i can?
Can you read the original post to make sure you didn't misunderstand me?

And @Jack, thanks for trying to help me, but i don't know how to use that piece of code. Can you tell me how it works or explain something instead of just giving a code alone please?

jackx3rx
26/12/2014, 06:17 PM
new name[MAX_PLAYER_NAME], find, length, shortname[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
find = strfind(name,"_",false);
length = strlen(name);
shortname = strdel(name,find,length);

// Players first name is stored in 'shortname'.

Oh sure, yes, I will help.

[strfind] We're setting 'find' to the position of '_' so for example, if the players name is Benjamin_Marcus, the position would be 9 (because its the 9th character).

[strlen] Next we set 'length' to the total length of the name, so Benjamin_Marcus would be 15.

[strdel] Finally we delete from characters 9 to 15 with 'strdel', so that means it deletes _ and onwards. So in our case it deletes '_Marcus'. Leaving us with 'Benjamin'.

Hope I helped.

EDIT: I changed the pawn script since when I tested it didn't work properly, you must make a new string called 'shortname[MAX_PLAYER_NAME]' and set that to the strdel. It's in the code up ^

PS: Are you Calvin Catt from SARP?

CalvinC
26/12/2014, 06:36 PM
Alright thanks, so then i use

stock GetPlayerFirstName(playerid)
{
new name[MAX_PLAYER_NAME], find, length;
GetPlayerName(playerid,name,sizeof(name));
find = strfind(name,"_",false);
length = strlen(name);
strdel(name,find,length);
}

format(string, sizeof(string), "{33AA33}Firstname: {FFFFFF}%s", GetPlayerFirstName(playerid));
SendClientMessage(playerb, COLOR_WHITE, string);
I think that's what you meant?

And no im not Calvin Catt, i don't play SARP. :P

jackx3rx
26/12/2014, 06:40 PM
Use this:


stock GetPlayerFirstName(playerid)
{
new name[MAX_PLAYER_NAME], find, length, shortname[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
find = strfind(name,"_",false);
length = strlen(name);
shortname = strdel(name,find,length);
return shortname;
}



format(string, sizeof(string), "{33AA33}Firstname: {FFFFFF}%s", GetPlayerFirstName(playerid));
SendClientMessage(playerb, COLOR_WHITE, string);

CalvinC
26/12/2014, 06:43 PM
Alright, so how do i get the last name now?
Might sound noobish, but do i just turn this around?
find = strfind(name,"_",false);
to
find = strfind(false,"_",name);

jackx3rx
26/12/2014, 06:49 PM
Alright, so how do i get the last name now?
Might sound noobish, but do i just turn this around?
find = strfind(name,"_",false);
to
find = strfind(false,"_",name);

Negative, do not change strfind. I will write both out for you:


stock GetPlayerFirstName(playerid)
{
new name[MAX_PLAYER_NAME], find, length, shortname[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
find = strfind(name,"_",false);
length = strlen(name);
format(shortname,sizeof(shortname),"%s",strdel(name,find,length));
return shortname;
}

stock GetPlayerLastName(playerid)
{
new name[MAX_PLAYER_NAME], find, shortname[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
find = strfind(name,"_",false);
format(shortname,sizeof(shortname),"%s",strdel(name,0,find));
return shortname;
}

I changed "shortname = strdel.." etc to format, since it gave off errors again.

Dignity
26/12/2014, 07:34 PM
While you can use both those functions ^, I took the time to make a command which does the same but a little bit more efficiently. (I made it before actually noticing the new replies, and I don't want the effort to go to waste):

(Using ZCMD, and working as it has been tested):


CMD:splitname(playerid, params[])
{
new name[MAX_PLAYER_NAME], tempstr[MAX_PLAYER_NAME / 2];
GetPlayerName(playerid, name, sizeof(name));

new loc = strfind(name, "_", true);

if(loc == -1)
return SendClientMessage(playerid, -1, "You don't have an underscore in your name!");

strmid(tempstr, name, loc, strlen(name));
strdel(name, loc, strlen(name));

strdel(tempstr, 0, 1);

format(name, sizeof(name), "Firstname: %s", name);
SendClientMessage(playerid, -1, name);

format(name, sizeof(name), "Lastname: %s", tempstr);
SendClientMessage(playerid, -1, name);

return true;
}

CalvinC
28/12/2014, 10:50 PM
format(string, sizeof(string), "%s", Lastname(playerid));
SendClientMessage(playerb, COLOR_WHITE, string);
format(string, sizeof(string), "%s", Firstname(playerid));
SendClientMessage(playerb, COLOR_WHITE, string);


stock Firstname(playerid)
{
new name[MAX_PLAYER_NAME], find, length, shortname[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
find = strfind(name,"_",false);
length = strlen(name);
format(shortname,sizeof(shortname),"%s",strdel(name,find,length));
return shortname;
}

stock Lastname(playerid)
{
new name[MAX_PLAYER_NAME], find, shortname[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
find = strfind(name,"_",false);
format(shortname,sizeof(shortname),"%s",strdel(name,0,find));
return shortname;
}

Is this how im supposed to do it? Because it shows the whole name, not last and first.

CalvinC
30/12/2014, 03:50 PM
Bump.