GetPVarString

From SA-MP Wiki

Revision as of 03:29, 9 May 2019; view current revision
←Older revision | Newer revision→
Jump to: navigation, search


Description:

Gets a player variable as a string.


Image:32px-Circle-style-warning.png

Important
Note

This function may return invalid data in the string (negative values) when using special characters (àáâãäéèêíìîóòôõúùûç), causing crash in the strcmp function and showing other characters in the functions SendClientMessage and SendClientMessageToAll.


Image:32px-Ambox_warning_orange.png

Note

Variables aren't reset until after OnPlayerDisconnect is called, so the values are still accessible in OnPlayerDisconnect.


Parameters:
(playerid, varname[], string_return[], len)
playeridThe ID of the player whose player variable to get.
varnameThe name of the player variable, set by SetPVarString.
&string_returnThe array in which to store the string value in, passed by reference.
lenThe maximum length of the returned string.


Return Values:

The length of the string.


Image:32px-Ambox_warning_orange.png

Note

If length of string is zero (value not set), string_return text will not be updated or set to anything and will remain with old data, neccesying that you clear the variable to blank value if GetPVarString returns 0 if that behavior is undesired


Example: Save the player's name in a pVar

public OnPlayerConnect(playerid,reason)
{
    new playerName[MAX_PLAYER_NAME+1];
    GetPlayerName(playerid, playerName, MAX_PLAYER_NAME);
    SetPVarString(playerid, "PlayerName", playerName);
    return 1;
}
 
public OnPlayerDeath(playerid, killerid, reason)
{
    new playerName[MAX_PLAYER_NAME+1];
    GetPVarString(playerid, "PlayerName", playerName, sizeof(playerName));
 
    printf("%s died.", playerName);
}

Bug example:

new string[20];
GetPVarString(playerid, "test", string, sizeof string);
 
for(new i; i < 20; i++){
    printf("string[%d] value: %d char: '%c'", i, string[i], string[i]);
}
/*
Output:
string[0] value: -32 char: 'à'
string[1] value: -31 char: 'á'
string[2] value: -30 char: 'â'
string[3] value: -29 char: 'ã'
string[4] value: -28 char: 'ä'
string[5] value: -23 char: 'é'
string[6] value: -24 char: 'è'
string[7] value: -22 char: 'ê'
string[8] value: -19 char: 'í'
string[9] value: -20 char: 'ì'
string[10] value: -18 char: 'î'
string[11] value: -13 char: 'ó'
string[12] value: -14 char: 'ò'
string[13] value: -12 char: 'ô'
string[14] value: -11 char: 'õ'
string[15] value: -6 char: 'ú'
string[16] value: -7 char: 'ù'
string[17] value: -5 char: 'û'
string[18] value: -25 char: 'ç'
string[19] value: 0 char: '
*/
 
///crash
if(!strcmp(string, "áà")){
    print("true");
}
else{
    print("false");
}

Fix:

SetPVarString(playerid, "teste", "àáâãäéèêíìîóòôõúùûç");
 
 
new string[20];
GetPVarString(playerid, "test", string, sizeof string);
 
for(new i; i < 20; i++){
    if(string[i] < 0){
        string[i] += 256;
    }
    printf("string[%d] value: %d char: '%c'", i, string[i], string[i]);
}
 
/*
string[0] value: 224 char: 'à'
string[1] value: 225 char: 'á'
string[2] value: 226 char: 'â'
string[3] value: 227 char: 'ã'
string[4] value: 228 char: 'ä'
string[5] value: 233 char: 'é'
string[6] value: 232 char: 'è'
string[7] value: 234 char: 'ê'
string[8] value: 237 char: 'í'
string[9] value: 236 char: 'ì'
string[10] value: 238 char: 'î'
string[11] value: 243 char: 'ó'
string[12] value: 242 char: 'ò'
string[13] value: 244 char: 'ô'
string[14] value: 245 char: 'õ'
string[15] value: 250 char: 'ú'
string[16] value: 249 char: 'ù'
string[17] value: 251 char: 'û'
string[18] value: 231 char: 'ç'
string[19] value: 0 char: '
*/

Related Functions

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

  • SetPVarInt: Set an integer for a player variable.
  • GetPVarInt: Get the previously set integer from a player variable.
  • GetPVarFloat: Get the previously set float from a player variable.
Personal tools