Weird variable output.
#1

Hello! I have a weird problem, take a look at this code:

PHP код:
enum P {
    
name,
    
password,
    
ip,
    
activated,
    
banned,
    
score,
    
totalMatches,
    
kills,
    
deaths
};
new 
P_DATA[MAX_PLAYERS][P];
public 
OnPlayerSpawn(playerid)
{
    
//(Name, Password, IP, Activated, Banned, Score, TotalMatches, Kills, Deaths)
    
new Query[256], DBResultResultpName[MAX_PLAYER_NAME], IP[16], Field[30];
    
GetPlayerName(playeridpNamesizeof(pName));
    
format(Querysizeof(Query), "SELECT * FROM Accounts WHERE Name = '%s';"pName);
    
    
Result db_query(DatabaseQuery);
    
SetTimer("ConnectTimer"2000false);
    
    
db_get_field_assoc(Result"Name"Field30);
    
P_DATA[playerid][name] = GetPlayerName(playeridpNamesizeof(pName));
    
    
db_get_field_assoc(Result"IP"Field30);
    
P_DATA[playerid][ip] = GetPlayerIp(playeridIPsizeof(IP));
    
    
db_get_field_assoc(Result"Banned"Field30);
    
P_DATA[playerid][banned] = strval(Field);
    
    
db_get_field_assoc(Result"Score"Field30);
    
P_DATA[playerid][score] = strval(Field);
    
    
db_get_field_assoc(Result"TotalMatches"Field30);
    
P_DATA[playerid][totalMatches] = strval(Field);
    
    
db_get_field_assoc(Result"Kills"Field30);
    
P_DATA[playerid][kills] = strval(Field);
    
    
db_get_field_assoc(Result"Deaths"Field30);
    
P_DATA[playerid][deaths] = strval(Field);
    
    
db_free_result(Result);
    return 
1;
}
public 
OnPlayerClickTextDraw(playeridText:clickedid)
{
    if(
clickedid == TDEditor_TD[8])
    {
        
//(Name, Password, IP, Activated, Banned, Score, TotalMatches, Kills, Deaths)
        
new str[1000];
        
format(strsizeof(str), "{FFFFFF}Name:{FF9900} %s\n{FFFFFF}IP:{FF9900} %s\n{FFFFFF}Kills:{FF9900} %d\n{FFFFFF}Deaths:{FF9900} %d\n{FFFFFF}Total matches played:{FF9900} %d\n{FFFFFF}Score:{FF9900} %d\n",
         
P_DATA[playerid][name],
         
P_DATA[playerid][ip],
         
P_DATA[playerid][kills],
         
P_DATA[playerid][deaths],
         
P_DATA[playerid][totalMatches],
         
P_DATA[playerid][score]
        );
        
printf("%s"P_DATA[playerid][name]);
        
        
ShowPlayerDialog(playeridDIALOG_STATS_1DIALOG_STYLE_MSGBOX"{19a8ea}[Stats]"str"OK""");
    }
    return 
1;

But this is what it outputs:


What can I do?
Reply
#2

Bump
Reply
#3

What makes you think that you're important enough that you can think you bump your thread within ten minutes, when the rules say you must wait at least 24 hours? I know for a fact what the solution - or rather the problem - is and hadn't it been for this completely egocentric bump I would've given it to you right now. But now you'll have to wait 24 hours. If I remember to check this thread again.
Reply
#4

Well, sorry.. I forgot this rule. I must wait 24 hours..
Reply
#5

GetPlayerName returns retrieved name's length.
GetPlayerIp return true/false, not a char array (string).

Also, I think that you thought that they are also returning a char array with the name/ip. As a side note, setting char arrays variables to another char array variable isn't working often (it works only if you have something like <<new variable[ 32 ]; variable = "test";>>, or even making it equal to another vector of chars [not sure about this, but I guess it works, but it the second variable needs exactly the same size]), you have to use strcat for that.

Why are you getting the database fields "Name" and "IP" in the variable "Field" if you will set them to the values got from GetPlayerName and GetPlayerIp ?

Change this:
PHP код:
db_get_field_assoc(Result"Name"Field30); 
P_DATA[playerid][name] = GetPlayerName(playeridpNamesizeof(pName)); 
 
db_get_field_assoc(Result"IP"Field30); 
P_DATA[playerid][ip] = GetPlayerIp(playeridIPsizeof(IP)); 
to this (just sets the important variables to what you need):
PHP код:
GetPlayerName(playeridP_DATA[playerid][name], MAX_PLAYER_NAME+1); 
GetPlayerName(playeridP_DATA[playerid][ip], 16); 
or to this, if you want to set those values with the ones from the database, first writing the database value in the "Field" variable (I guess you have no reason to do this):
PHP код:
db_get_field_assoc(Result"Name"FieldMAX_PLAYER_NAME+1); // player's name can be a maximum of MAX_PLAYER_NAME (24) characters + the null terminator (+1)
P_DATA[playerid][name][0] = '\0'// clearing the string if it contains something
strcatP_DATA[playerid][name], FieldMAX_PLAYER_NAME+1); // appending Field to that variable, we also need to specify the maximum size of P_DATA[playerid][name] (which is MAX_PLAYER_NAME+1, the last parameter) because Pawn has a bug with sizeof an array variable from an enum, as strcat 3rd parameter's default value is sizeof(dest) [dest being the first parameter]
 
db_get_field_assoc(Result"IP"Field16); // an IP can be a maximum of 15 characters + the null terminator (+1)
P_DATA[playerid][ip][0] = '\0';
strcatP_DATA[playerid][ip], Field16); 
the better version for this, without writing in the temporary "Field" variable) is
PHP код:
db_get_field_assoc(Result"Name"P_DATA[playerid][name], MAX_PLAYER_NAME+1);
db_get_field_assoc(Result"IP"P_DATA[playerid][ip], 16); 
Additionally, solutions two common mistakes of variables sizes for char arrays (strings):
- Make sure you have "P_DATA[playerid][name]" variable with the length MAX_PLAYER_NAME+1, so it won't overflow eventually
- Make sure you have "P_DATA[playerid][ip]" variable with the length 15+1, so it won't overflow eventually.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)