When Using Position Saving
#1

When using this, after I log in it spawns me so far up in the sky. The positions are entered into the database when a user registers on my website. Positions are Montgomery coords but it spawns me in the sky in Blueberry:

pawn Код:
case DIALOG_LOGIN: //Dialog login
        {
            if(!response) //If they click the cancel button
            {
                SendClientMessage(playerid, COLOR_LIGHTRED, "Error{FFFFFF}: You have chosen to quit Domination Roleplay, please visit us again soon!");
                Kick(playerid);
            }
            if(response) //If the player clicked login
            {
                new query[210], pname[24], escapepass[100];//
                GetPlayerName(playerid, pname, 24); //Gets the players name
                mysql_real_escape_string(inputtext, escapepass); //We escape the inputtext to avoid SQL injections.
                format(query, sizeof(query), "SELECT `user` FROM playerdata WHERE user = '%s' AND password = SHA1('%s')", pname, escapepass);
                mysql_query(query);
                mysql_store_result();
                new numrows = mysql_num_rows();
                if(numrows == 1)
                {
                    new savingstring[20];
                    GetPlayerName(playerid, pname, 24);
                    format(query, sizeof(query), "SELECT Level, Money, PlayerPosX, PlayerPosY, PlayerPosZ FROM playerdata WHERE user = '%s'", pname);
                    //We only select the variables that we want to use.
                    //We don't need things like the password string or the user string.
                    mysql_query(query); //Queries the result
                    mysql_store_result();
                    while(mysql_fetch_row_format(query,"|"))
                    {
                        //We use while so that it does a single query, not multiple
                        //Especially when we have more variables. If there is more
                        //Variables, you should just split the line with sscanf. To
                        //Make it easier.
                        mysql_fetch_field_row(savingstring, "Level"); SetPlayerScore(playerid, strval(savingstring));
                        mysql_fetch_field_row(savingstring, "Money"); MoneyGiven[playerid] = strval(savingstring);
                        mysql_fetch_field_row(savingstring, "PlayerPosX"); PlayerVar[playerid][pPosX] = strval(savingstring);
                        mysql_fetch_field_row(savingstring, "PlayerPosY"); PlayerVar[playerid][pPosY] = strval(savingstring);
                        mysql_fetch_field_row(savingstring, "PlayerPosZ"); PlayerVar[playerid][pPosZ] = strval(savingstring);
                        mysql_fetch_field_row(savingstring, "InteriorID"); PlayerVar[playerid][InteriorID] = strval(savingstring);
                        mysql_fetch_field_row(savingstring, "SkinID"); PlayerVar[playerid][SkinID] = strval(savingstring);
                        //If you are wondering why I'm using savingstring instead
                        //Of a variable like using MoneyGiven right away, it's because
                        //mysql_fetch_field_row requires a string.
                    }
                    SetPlayerHealth(playerid, 100);
                    TextDrawHideForPlayer(playerid, WIDESCREEN_TOP);
                    TextDrawHideForPlayer(playerid, WIDESCREEN_BOTTOM);
                    TextDrawHideForPlayer(playerid, WelcomeTD);
                    TogglePlayerSpectating(playerid, 0);
                    SetPVarInt(playerid, "prelogin", 0);
                    SetPlayerWeapons(playerid);
                    SetPlayerWeaponsEx(playerid);
                    mysql_free_result(); //We must always free a stored result
                    SendClientMessage(playerid, -1, "You have been logged in!"); //Sends the client a message.
                    Logged[playerid] = 1; //Sets our logged in variable to one
                    SetPlayerInterior(playerid, PlayerVar[playerid][InteriorID]);
                    SetSpawnInfo(playerid, 0, PlayerVar[playerid][SkinID], PlayerVar[playerid][pPosY], PlayerVar[playerid][pPosZ], PlayerVar[playerid][pPosZ], 0, 0, 0, 0, 0, 0, 0);
                    SpawnPlayer(playerid);
                    return 1;
                }
                //This means that there is a user in the database with the same
                //password that we typed, we now proceed by using the login function.
                if(!numrows)
                {
                    //This means that the password that the player
                    //typed was incorrect and we will resend the dialog.
                    format(string, sizeof(string), "Welcome back, %s. Please enter your password below to log in.", GetName(playerid));
                    ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, "Login", string, "Confirm", "Quit");
                    SendClientMessage(playerid, COLOR_LIGHTRED, "Error{FFFFFF}: You have entered an incorrect password."); //Sends the client a error message
                }
                mysql_free_result(); //Remember to always free a result if you stored one!
            }
            return 1;
        }
Then after being spawned in the sky and falling to the ground, I log out hoping the position will save (it should save) but it then spawns me underneath the ground:

pawn Код:
if(Logged[playerid] == 1)
    {
        new Float:x, Float:y, Float:z;
        GetPlayerPos(playerid, x, y, z);
        new skin = GetPlayerSkin(playerid);
        new interior = GetPlayerInterior(playerid);
        PlayerVar[playerid][pPosX] = x;
        PlayerVar[playerid][pPosY] = y;
        PlayerVar[playerid][pPosZ] = z;
        PlayerVar[playerid][InteriorID] = interior;
        PlayerVar[playerid][SkinID] = skin;
        new money = GetPlayerMoney(playerid);
        //If the player disconnects before registering,
        //we want to make sure it doesn't try update
        //so we check if the player is logged in.
        new query[200], pname[24]; //Creates the variables
        GetPlayerName(playerid, pname, 24); //Gets the players name.
        format(query, sizeof(query), "UPDATE playerdata SET Level=%d, Money=%d, PlayerPosX=%f, PlayerPosY=%f, PlayerPosZ=%f, InteriorID=%d, SkinID=%d WHERE user='%s'",
        PlayerVar[playerid][pLevel],
        money,
        PlayerVar[playerid][pPosX],
        PlayerVar[playerid][pPosY],
        PlayerVar[playerid][pPosZ],
        PlayerVar[playerid][InteriorID],
        PlayerVar[playerid][SkinID],
        pname);
        mysql_query(query);
        //No need to store a result for a update string
    }
I have the PlayerPosX, PlayerPosY and PlayerPosZ set as float in the database, just don't understand this and it's been frustrating me all day and night for two days now, no sleep and not happy

I have re-written this code about ten times and all different methods.
Reply
#2

Before you log out next time, do a /save command and check what's in the DB with what's in the savedpositions.txt file in your Documents/GTA San Andreas User Files/SAMP/savedpositions.txt file. Do they match? Or are the coordinates not even saving in the DB?
Reply
#3

It doesn't change my player skin either and I have a SetPlayerSkin(); there, it seems like the variables are not being read.

pawn Код:
enum PlayerVariable
{
    pLevel,
    InteriorID,
    SkinID,
    Float:pPosX,
    Float:pPosY,
    Float:pPosZ,
}

new PlayerVar[MAX_PLAYERS][PlayerVariable];
The global variables are set up so I don't understand why <.<

Yes the coords are changing in the database.
Reply
#4

I think I see the problem with the coordinates not being loaded properly...

pawn Код:
mysql_fetch_field_row(savingstring, "PlayerPosX"); PlayerVar[playerid][pPosX] = strval(savingstring);
                        mysql_fetch_field_row(savingstring, "PlayerPosY"); PlayerVar[playerid][pPosY] = strval(savingstring);
                        mysql_fetch_field_row(savingstring, "PlayerPosZ"); PlayerVar[playerid][pPosZ] = strval(savingstring);
You're treating them like an integer. strval is for turning a string into an integer, not a float. Use floatstr().

Reference: https://sampwiki.blast.hk/wiki/Floats

In regards to the skin not being loaded... is "SkinID" used anywhere else? You may want to use "pSkinID" in the enum.
Reply
#5

Ohh I know what you mean! Silly me. I'll try it now, should work with that. +rep if it does.
Reply
#6

Legend. Works.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)