Player positions not loading properly
#1

I've got another problem. Basically on my load player, it doesn't load the players position properly. It always spawns them at the blueberry farm instead of their most recent position before leaving. Here are the codes that could be affecting this:
PHP код:
Of course on the dialog login it allows them to access the request class, and with or without the logged in statement on requestclass does not affect results
 
public OnPlayerRequestClass(playeridclassid)
{
    if(
playerInfo[playerid][LoggedIn] == true)
    {
        new 
Float:posXFloat:posYFloat:posZ;
        
cache_get_value_name_float(0"playerX"posX);
        
cache_get_value_name_float(0"playerY"posY);
        
cache_get_value_name_float(0"playerZ",posZ);
        
SetSpawnInfoplayerid00posXposYposZ000000);
        
SpawnPlayer(playerid);
    }
    return 
1;
}
 
stock LoadPlayerStats(playerid)
{
    if(
playerInfo[playerid][LoggedIn] ==  true)
    {
        new 
query[200], name[24];
        
GetPlayerName(playeridname24);
        
format(querysizeof(query), "SELECT * FROM `accounts` WHERE `playerName` = '%s'"name);
        
mysql_tquery(Databasequery);
        
playerInfo[playerid][playerAdmin] = cache_get_value_int(0"playerAdmin"playerInfo[playerid][playerAdmin]);
        
playerInfo[playerid][playerMod] = cache_get_value_int(0"playerMod"playerInfo[playerid][playerMod]);
        
playerInfo[playerid][playerHelper] = cache_get_value_int(0"playerHelper"playerInfo[playerid][playerHelper]);
        
playerInfo[playerid][playerLevel] = cache_get_value_int(0"playerLevel"playerInfo[playerid][playerLevel]);
        
playerInfo[playerid][playerCash] = cache_get_value_int(0"playerCash"playerInfo[playerid][playerCash]);
        
SetPlayerScore(playeridplayerInfo[playerid][playerLevel]);
        
GivePlayerMoney(playeridplayerInfo[playerid][playerCash]);
        
printf("player loaded.");
    }

Reply
#2

You must save player position on OnPlayerDisconnect! then it will work.
Reply
#3

Perhaps we can get to see how you save the positions as well?
Reply
#4

This is wrong. Do not use cache functions in random places, there must be active cache in order to use them or else will give warning in mysql logs.

mysql_tquery uses another callback, see example: https://sampwiki.blast.hk/wiki/MySQL#mysql_tquery
Reply
#5

They're being saved on player disconnect.

@Calisthenics, should I use the format function aswell to get the info?
Reply
#6

It's a good practice as you need to escape necessary strings.

When a player connects:
pawn Код:
mysql_format(...);
mysql_tquery(Database, query, "OnPlayerDataLoad", "d", playerid);
Check if player is registered or not:
pawn Код:
forward OnPlayerDataLoad(playerid);
public OnPlayerDataLoad(playerid)
{
    if (cache_num_rows())
    {
        // player is register, show "login" dialog
    }
    else
    {
        // player is not registered, show "register" dialog
    }
}
In OnDialogResponse, you check if passwords match and then you call all cache functions to retrieve data.

This is also wrong:
pawn Код:
playerInfo[playerid][playerAdmin] = cache_get_value_int(0, "playerAdmin", playerInfo[playerid][playerAdmin]);
function returns 0 or 1 (failure or success) so just:
pawn Код:
cache_get_value_int(0, "playerAdmin", playerInfo[playerid][playerAdmin]);
But this is rather basic. Mysql race is a condition that can happen even in a samp server, you do not want random players get the data of previous player and their administrator status. For an example script, look at here: https://raw.githubusercontent.com/pB...stem-cache.pwn
Reply
#7

Heres how it goes.

Under OnPlayerConnect, via MySQL, player's data is fetched using SELECT statement(Make sure to use tquery), now the command is passed to the threaded function. Under That function, get the float values from the cache and store them inside the player variables. Now, under OnPlayerRequestSpawn, use SetSpawnInfo() with the values you previously got from DB and then Spawn him.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)