GetPlayerName

From SA-MP Wiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 15:01, 18 March 2017
OstGot (Talk | contribs)

← Previous diff
Revision as of 00:43, 27 June 2018
Freaksken (Talk | contribs)
(Account for null terminator)
Next diff →
Line 6: Line 6:
{{Param|playerid|The ID of the player to get the name of.}} {{Param|playerid|The ID of the player to get the name of.}}
{{Param|const name[]|An array into which to store the name, passed by reference.}} {{Param|const name[]|An array into which to store the name, passed by reference.}}
-{{Param|len|The length of the string that should be stored. Recommended to be 'MAX_PLAYER_NAME'.}}+{{Param|len|The length of the string that should be stored. Recommended to be '''MAX_PLAYER_NAME + 1'''. The + 1 is necessary to account for the null terminator.}}
{{Returns|The length of the player's name. 0 if player specified doesn't exist. {{Returns|The length of the player's name. 0 if player specified doesn't exist.
Line 23: Line 23:
// Get the name of the player that connected and display a join message to other players // Get the name of the player that connected and display a join message to other players
- new name[MAX_PLAYER_NAME], string[24+MAX_PLAYER_NAME];+ new name[MAX_PLAYER_NAME + 1];
GetPlayerName(playerid, name, sizeof(name)); GetPlayerName(playerid, name, sizeof(name));
 + new string[MAX_PLAYER_NAME + 23 + 1];
format(string, sizeof(string), "%s has joined the server.", name); format(string, sizeof(string), "%s has joined the server.", name);
SendClientMessageToAll(0xC4C4C4FF, string); SendClientMessageToAll(0xC4C4C4FF, string);

Revision as of 00:43, 27 June 2018



Description:

Get a player's name.


Parameters:
(playerid, const name[], len)
playeridThe ID of the player to get the name of.
const name[]An array into which to store the name, passed by reference.
lenThe length of the string that should be stored. Recommended to be MAX_PLAYER_NAME + 1. The + 1 is necessary to account for the null terminator.


Return Values:

The length of the player's name. 0 if player specified doesn't exist.
  • The player's name is stored in the specified array.


Image:32px-Ambox_warning_orange.png

Note

A player's name can be up to 24 characters long (as of 0.3d R2) by using SetPlayerName. This is defined in a_samp.inc as MAX_PLAYER_NAME. However, the client can only join with a nickname between 3 and 20 characters, otherwise the connection will be rejected and the player has to quit to choose a valid name.


Example Usage:

public OnPlayerConnect(playerid)
{
    // Get the name of the player that connected and display a join message to other players
 
    new name[MAX_PLAYER_NAME + 1];
    GetPlayerName(playerid, name, sizeof(name));
 
    new string[MAX_PLAYER_NAME + 23 + 1];
    format(string, sizeof(string), "%s has joined the server.", name);
    SendClientMessageToAll(0xC4C4C4FF, string);
 
    return 1;
}

Related Functions

The following functions may be useful, as they are related to this function in one way or another.

Personal tools
In other languages