[Tutorial] Saving position and skin. [using Y_INI] [For newbies]
#1

Before we start, i assume that you already have any registration & login system, in case you don't feel free to use this awesome tutorial made by Kush. (CLICK ME)

Getting started:
Before we start we need to understand few things about the system,
○ It can be avoided if the player forces crash instead of disconnecting.
○ gmx will not save the player stats.

Lets get started:
First we need to make some variables to save the player position and skin.
Find:
pawn Code:
enum pInfo
{
Add in the bottom:
pawn Code:
Skin,
    Float:Xpos,
    Float:Ypos,
    Float:Zpos,
    NewReg,
So it would look like this:
pawn Code:
enum pInfo
{
    Pass,
    Money,
    Admin,
    Deaths,
    Skin, // Saving the player skin into his INI file.
    Float:Xpos, // Saving the player X position into his INI file.
    Float:Ypos, // Saving the player Y position into his INI file.
    Float:Zpos, // Saving the player Z position into his INI file.
    NewReg, // To tell the difference between new registers and people who already played.
}
REMEMBER: Add the new 5 variables into all your player data loader/saver for example:
pawn Code:
forward LoadUser_data(playerid,name[],value[]);
public LoadUser_data(playerid,name[],value[])
{
    INI_Int("Password",PlayerInfo[playerid][Pass]);
    INI_Int("Money",PlayerInfo[playerid][Money]);
    INI_Int("Admin",PlayerInfo[playerid][Admin]);
    INI_Int("Deaths",PlayerInfo[playerid][Deaths]);
    INI_Int("Skin",PlayerInfo[playerid][Skin]);
    INI_Float("Xpos",PlayerInfo[playerid][Xpos]); // Make sure they are Float here.
    INI_Float("Ypos",PlayerInfo[playerid][Ypos]); // Make sure they are Float here.
    INI_Float("ZXpos",PlayerInfo[playerid][Zpos]); // Make sure they are Float here.
    INI_Int("NewReg",PlayerInfo[playerid][NewReg]);
    return 1;
}
// My registration system YOU might have a different one.
        case DIALOG_REGISTER:
        {
            if (!response) return Kick(playerid);
            if(response)
            {
                if(!strlen(inputtext)) return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, ""COL_WHITE"Registering...",""COL_RED"You have entered an invalid password.\n"COL_WHITE"Type your password below to register a new account.","Register","Quit");
                new INI:File = INI_Open(UserPath(playerid));
                INI_SetTag(File,"data");
                INI_WriteInt(File,"Password",udb_hash(inputtext));
                INI_WriteInt(File,"Money",0);
                INI_WriteInt(File,"Admin",0);
                INI_WriteInt(File,"Deaths",0);
                INI_WriteInt(File,"Skin",0);
                INI_WriteInt(File,"Xpos",0); // It doesn't matter they are Int here because we change them to Float later.
                INI_WriteInt(File,"Ypos",0);// It doesn't matter they are Int here because we change them to Float later.
                INI_WriteInt(File,"Zpos",0);// It doesn't matter they are Int here because we change them to Float later.
                INI_WriteInt(File,"NewReg",0);
                INI_Close(File);
                SetSpawnInfo(playerid, 0, 0, 1958.33, 1343.12, 15.36, 269.15, 0, 0, 0, 0, 0, 0);
                SpawnPlayer(playerid);
            }
        }
REMEMBER: Make sure the X,Y,Z have Float: beforehand, or else it won't load their position correctly.

Next, we going to find your login Dialog/Textdraw/System and add a
SpawnPlayer(playerid);
At the bottom there.
For example:
pawn Code:
case DIALOG_LOGIN:
        {
            if ( !response ) return Kick ( playerid );
            if( response )
            {
                if(udb_hash(inputtext) == PlayerInfo[playerid][Pass])
                {
                    INI_ParseFile(UserPath(playerid), "LoadUser_%s", .bExtra = true, .extra = playerid);
                    GivePlayerMoney(playerid, PlayerInfo[playerid][Money]);
                    SpawnPlayer(playerid); // We added this at the bottom.
                }
                else
                {
                    ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT,""COL_WHITE"Login",""COL_RED"You have entered an incorrect password.\n"COL_WHITE"Type your password below to login.","Login","Quit");
                }
                return 1;
            }
        }
This will call the OnPlayerSpawn and there we will add the player information.

Next we going to find OnPlayerSpawn and add few lines there:
Usually it will look like this:
pawn Code:
public OnPlayerSpawn(playerid)
{
    return 1;
}
We going to add those things into it:

pawn Code:
SetPlayerSkin(playerid, ); // This sets the player skin to his skin.
SetPlayerPos(playerid, ); // This will set the player position.
So we gonna dd those to OnPlayerSpawn, but before we do we have to make sure he is NOT a new registrator so we gonna check for it at the beginning.

pawn Code:
public OnPlayerSpawn(playerid)
{
    if(PlayerInfo[playerid][NewReg] == 0) // Checking if the player is a new registrator.
    {
        SetPlayerSkin(playerid, PlayerInfo[playerid][Skin]); // Sets the player skin.
        SetPlayerPos(playerid, PlayerInfo[playerid][Xpos], PlayerInfo[playerid][Ypos], PlayerInfo[playerid][Zpos]+0.3); // Sets the player position with extra 0.3 of Z position to make him not fall off the map when he spawns.
    }
    return 1;
}
Next we going to go to OnPlayerDisconnect and set the variables there:

pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
    new Float:Xlog, Float:Ylog, Float:Zlog;
    GetPlayerPos(playerid, Xlog, Ylog, Zlog); // Getting the player position on disconnect.
    PlayerInfo[playerid][Xpos] = Xlog;
    PlayerInfo[playerid][Ypos] = Ylog;
    PlayerInfo[playerid][Zpos] = Zlog;
    new playerskin = GetPlayerSkin(playerid); // getting the player skin on disconnect.
    new INI:File = INI_Open(UserPath(playerid));
    INI_SetTag(File,"data");
    INI_WriteInt(File,"Money",GetPlayerMoney(playerid));
    INI_WriteInt(File,"Admin",PlayerInfo[playerid][Admin]);
    INI_WriteInt(File,"Deaths",PlayerInfo[playerid][Deaths]);
    INI_WriteInt(File,"Skin",playerskin); // his skin.
    INI_WriteFloat(File,"Xpos",PlayerInfo[playerid][Xpos]); // Making sure we save it as a FLOAT
    INI_WriteFloat(File,"Ypos",PlayerInfo[playerid][Ypos]); // Making sure we save it as a FLOAT
    INI_WriteFloat(File,"Zpos",PlayerInfo[playerid][Zpos]); // Making sure we save it as a FLOAT
    INI_WriteInt(File,"NewReg",0); // When he disconnects he is no longer a new registrator.
    INI_Close(File);
    return 1;
}
Last thing is going to our Registration system and adding a little thing at the bottom of it:
pawn Code:
case DIALOG_REGISTER:
        {
            if (!response) return Kick(playerid);
            if(response)
            {
                if(!strlen(inputtext)) return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, ""COL_WHITE"Registering...",""COL_RED"You have entered an invalid password.\n"COL_WHITE"Type your password below to register a new account.","Register","Quit");
                new INI:File = INI_Open(UserPath(playerid));
                INI_SetTag(File,"data");
                INI_WriteInt(File,"Password",udb_hash(inputtext));
                INI_WriteInt(File,"Money",0);
                INI_WriteInt(File,"Admin",0);
                INI_WriteInt(File,"Deaths",0);
                INI_WriteInt(File,"Skin",0);
                INI_WriteInt(File,"Xpos",0);
                INI_WriteInt(File,"Ypos",0);
                INI_WriteInt(File,"Zpos",0);
                INI_WriteInt(File,"NewReg",0);
                INI_Close(File);
                SetSpawnInfo(playerid, 0, 0, 1958.33, 1343.12, 15.36, 269.15, 0, 0, 0, 0, 0, 0);
                SpawnPlayer(playerid);
                PlayerInfo[playerid][NewReg] = 1; // Adding a variable that this is a NEW registrator and he doesn't have an old saved position and skin.
            }
}
Hope it helped few players, even though i am not good at writing tutorials if it even helped one person, that makes this whole thread as helpful .
Reply
#2

Why do you integrate it with registration and login systems when the topic title says about saving Position and skin?
Reply
#3

Because thats how people join the server? They register? they login?
And then when they disconnect and connect after a certain time you need to load this data for them.
I am not sure what is your question actually when this makes a perfect logic for me.
Isn't that simple logic?
Reply
#4

Nice...And Lordz is right. You must have completely stated the saving of position/skin etc only. Just tell them where to add this and etc
Reply
#5

Just wanted to help new scripters.
Reply
#6

nvm DELETED
Reply
#7

Nice one I really needed this tut! + REP
But I have a problem... When I register the first time, everything is good. But when I login again, my game crashes when it tries to spawn me on my last position. :/ I tried to change probably everything but it's still crashing.
Can you help me?

Here is everithing connected with the login/register system I use and spawning:

PHP Code:
#define COL_WHITE "{FFFFFF}"
#define COL_RED "{F81414}"
#define COL_GREEN "{00FF22}"
#define COL_LIGHTBLUE "{00CED1}"
#define DIALOG_REGISTER 1
#define DIALOG_LOGIN 2
#define DIALOG_SUCCESS_1 3
#define DIALOG_SUCCESS_2 4
#define PATH "/Users/%s.ini"
enum pInfo
{
    
pPass,
    
pCash,
    
pBank,
    
pKills,
    
pDeaths,
    
pSkin,
    
NewReg,
    
Float:Xpos,
    
Float:Ypos,
    
Float:Zpos,
}
new 
PlayerInfo[MAX_PLAYERS][pInfo];
stock UserPath(playerid)
{
    new 
string[128],playername[MAX_PLAYER_NAME];
    
GetPlayerName(playerid,playername,sizeof(playername));
    
format(string,sizeof(string),PATH,playername);
    return 
string;
}
stock udb_hash(buf[]) {
    new 
length=strlen(buf);
    new 
s1 1;
    new 
s2 0;
    new 
n;
    for (
n=0n<lengthn++)
    {
       
s1 = (s1 buf[n]) % 65521;
       
s2 = (s2 s1)     % 65521;
    }
    return (
s2 << 16) + s1;
}
public 
OnGameModeInit()
{
    
AddPlayerClass(159, -198615727.70000076293990150411002515);
}
public 
OnPlayerConnect(playerid)
{
    if(
fexist(UserPath(playerid)))
    {
        
INI_ParseFile(UserPath(playerid), "LoadUser_%s", .bExtra true, .extra playerid);
        
ShowPlayerDialog(playeridDIALOG_LOGINDIALOG_STYLE_INPUT,""COL_WHITE"Login",""COL_WHITE"Type your password below to login.","Login","Quit");
    }
    else
    {
        
ShowPlayerDialog(playeridDIALOG_REGISTERDIALOG_STYLE_INPUT,""COL_WHITE"Registering...",""COL_WHITE"Type your password below to register a new account.","Register","Quit");
    }
    return 
1;
}
public 
OnPlayerDisconnect(playeridreason)
{
    new 
Float:XlogFloat:YlogFloat:Zlog;
    
GetPlayerPos(playeridXlogYlogZlog);
    
PlayerInfo[playerid][Xpos] = Xlog;
    
PlayerInfo[playerid][Ypos] = Ylog;
    
PlayerInfo[playerid][Zpos] = Zlog;
    new 
playerskin GetPlayerSkin(playerid);
    new 
INI:File INI_Open(UserPath(playerid));
    
INI_SetTag(File,"data");
    
INI_WriteInt(File,"Cash",GetPlayerMoney(playerid));
    
INI_WriteInt(File,"Bank",PlayerInfo[playerid][pBank]);
    
INI_WriteInt(File,"Kills",PlayerInfo[playerid][pKills]);
    
INI_WriteInt(File,"Deaths",PlayerInfo[playerid][pDeaths]);
    
INI_WriteInt(File,"Skin",playerskin);
    
INI_WriteFloat(File,"Xpos",PlayerInfo[playerid][Xpos]);
    
INI_WriteFloat(File,"Ypos",PlayerInfo[playerid][Ypos]);
    
INI_WriteFloat(File,"Zpos",PlayerInfo[playerid][Zpos]);
    
INI_WriteInt(File,"NewReg",0);
    
INI_Close(File);
    return 
1;
}
public 
OnPlayerRequestClass(playeridclassid)
{
    
SetPlayerPos(playerid1958.37831343.157215.3746);
    
SetPlayerCameraPos(playerid1958.37831343.157215.3746);
    
SetPlayerCameraLookAt(playerid1958.37831343.157215.3746);
    return 
1;
}
public 
OnPlayerSpawn(playerid)
{
    if(
PlayerInfo[playerid][NewReg] == 0)
    {
        
SetPlayerSkin(playeridPlayerInfo[playerid][pSkin]);
        
SetPlayerPos(playeridPlayerInfo[playerid][Xpos], PlayerInfo[playerid][Ypos], PlayerInfo[playerid][Zpos]+0.3);    
    }
    return 
1;
}
public 
OnDialogResponse(playeriddialogidresponselistiteminputtext[])
{
    switch( 
dialogid )
    {
        case 
DIALOG_REGISTER:
        {
            if (!
response) return Kick(playerid);
            if(
response)
            {
                if(!
strlen(inputtext)) return ShowPlayerDialog(playeridDIALOG_REGISTERDIALOG_STYLE_INPUT""COL_WHITE"Registering...",""COL_RED"You have entered an invalid password.\n"COL_WHITE"Type your password below to register a new account.","Register","Quit");
                new 
INI:File INI_Open(UserPath(playerid));
                
INI_SetTag(File,"data");
                
INI_WriteInt(File,"Password",udb_hash(inputtext));
                
INI_WriteInt(File,"Cash",0);
                
INI_WriteInt(File,"Bank",0);
                
INI_WriteInt(File,"Kills",0);
                
INI_WriteInt(File,"Deaths",0);
                
INI_WriteInt(File,"Skin",159);
                
INI_WriteInt(File,"NewReg",0);
                
INI_WriteInt(File,"Xpos",0);
                
INI_WriteInt(File,"Ypos",0);
                
INI_WriteInt(File,"Zpos",0);
                
INI_Close(File);
                
SetPlayerSkin(playerid159); // Sets the player skin
                
SetPlayerPos(playeridPlayerInfo[playerid][Xpos], PlayerInfo[playerid][Ypos], PlayerInfo[playerid][Zpos]+0.3);    
                
SpawnPlayer(playerid);
                
PlayerInfo[playerid][NewReg] = 1;
            }
        }
        case 
DIALOG_LOGIN:
        {
            if ( !
response ) return Kick playerid );
            if( 
response )
            {
                if(
udb_hash(inputtext) == PlayerInfo[playerid][pPass])
                {
                    
INI_ParseFile(UserPath(playerid), "LoadUser_%s", .bExtra true, .extra playerid);
                    
GivePlayerMoney(playeridPlayerInfo[playerid][pCash]);
                    
SpawnPlayer(playerid);
                }
                else
                {
                    
ShowPlayerDialog(playeridDIALOG_LOGINDIALOG_STYLE_INPUT,""COL_WHITE"Login",""COL_RED"You have entered an incorrect password.\n"COL_WHITE"Type your password below to login.","Login","Quit");
                }
                return 
1;
            }
        }
    }
    return 
1;

Thanks for your time
Reply
#8

You will have to "debug" the code, add print("stage X"); at every line of registering and spawning and change X with a number increasing all the time, then go to your console and look at what stage it stopped when you spawned and give me the line.
Reply
#9

So... it's strange. I can't say where or what is the problem so here are pictures of the console.
http://s15.postimg.org/u4z83fv1n/console1.png
http://s13.postimg.org/rpgh180x3/console2.png

Here is the Spawning part with printing "Stage X":
PHP Code:
public OnPlayerSpawn(playerid)
{
    if(
PlayerInfo[playerid][NewReg] == 0)
    {
        
SetPlayerSkin(playeridPlayerInfo[playerid][pSkin]); print("Stage 40");
        
SetPlayerPos(playeridPlayerInfo[playerid][Xpos], PlayerInfo[playerid][Ypos], PlayerInfo[playerid][Zpos]+0.3);    print("Stage 41");
    }
    print(
"Stage 42");
    return 
1;

Loading User Data (NOTICE, that the Stage 10 wasn't at the console at all):
PHP Code:
forward LoadUser_data(playerid,name[],value[]);
public 
LoadUser_data(playerid,name[],value[])
{
    
INI_Int("Password",PlayerInfo[playerid][pPass]); print("Stage 1");
    
INI_Int("Cash",PlayerInfo[playerid][pCash]); print("Stage 2");
    
INI_Int("Bank",PlayerInfo[playerid][pBank]); print("Stage 3");
    
INI_Int("Kills",PlayerInfo[playerid][pKills]); print("Stage 4");
    
INI_Int("Deaths",PlayerInfo[playerid][pDeaths]); print("Stage 5");
    
INI_Int("Skin",PlayerInfo[playerid][pSkin]); print("Stage 6");
    
INI_Int("NewReg",PlayerInfo[playerid][NewReg]); print("Stage 7");
    
INI_Float("Xpos",PlayerInfo[playerid][Xpos]); print("Stage 8");
    
INI_Float("Ypos",PlayerInfo[playerid][Ypos]); print("Stage 9");
    
INI_Float("Zpos",PlayerInfo[playerid][Zpos]); print("Stage 10");
    return 
1;

And here is the disconnecting:
PHP Code:
public OnPlayerDisconnect(playeridreason)
{
    new 
Float:XlogFloat:YlogFloat:Zlog; print("Stage 19");
    
GetPlayerPos(playeridXlogYlogZlog); print("Stage 20");
    
PlayerInfo[playerid][Xpos] = Xlog; print("Stage 21");
    
PlayerInfo[playerid][Ypos] = Ylog; print("Stage 22");
    
PlayerInfo[playerid][Zpos] = Zlog; print("Stage 23");
    new 
playerskin GetPlayerSkin(playerid);  print("Stage 24");
    new 
INI:File INI_Open(UserPath(playerid)); print("Stage 25");
    
INI_SetTag(File,"data"); print("Stage 26");
    
INI_WriteInt(File,"Cash",GetPlayerMoney(playerid)); print("Stage 27");
    
INI_WriteInt(File,"Bank",PlayerInfo[playerid][pBank]); print("Stage 28");
    
INI_WriteInt(File,"Kills",PlayerInfo[playerid][pKills]); print("Stage 29");
    
INI_WriteInt(File,"Deaths",PlayerInfo[playerid][pDeaths]); print("Stage 30");
    
INI_WriteInt(File,"Skin",playerskin); print("Stage 31");
    
INI_WriteFloat(File,"Xpos",PlayerInfo[playerid][Xpos]);  print("Stage 32");
    
INI_WriteFloat(File,"Ypos",PlayerInfo[playerid][Ypos]); print("Stage 33");
    
INI_WriteFloat(File,"Zpos",PlayerInfo[playerid][Zpos]); print("Stage 34");
    
INI_WriteInt(File,"NewReg",0); print("Stage 35");
    
INI_Close(File); print("Stage 36");
    return 
1;

Thanks for your help.

Any Ideas?
Reply
#10

Took me some time but found it:

pawn Code:
case DIALOG_REGISTER:
        {
            if (!response) return Kick(playerid);
            if(response)
            {
                if(!strlen(inputtext)) return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, ""COL_WHITE"Registering...",""COL_RED"You have entered an invalid password.\n"COL_WHITE"Type your password below to register a new account.","Register","Quit");
                new INI:File = INI_Open(UserPath(playerid));
                INI_SetTag(File,"data");
                INI_WriteInt(File,"Password",udb_hash(inputtext));
                INI_WriteInt(File,"Cash",0);
                INI_WriteInt(File,"Bank",0);
                INI_WriteInt(File,"Kills",0);
                INI_WriteInt(File,"Deaths",0);
                INI_WriteInt(File,"Skin",159);
                INI_WriteInt(File,"NewReg",0);
                INI_WriteInt(File,"Xpos",0);
                INI_WriteInt(File,"Ypos",0);
                INI_WriteInt(File,"Zpos",0);
                INI_Close(File);

                SetPlayerSkin(playerid, 159); // Sets the player skin
                SpawnPlayer(playerid);
                PlayerInfo[playerid][NewReg] = 1;
            }
        }
Then change this:

pawn Code:
public OnPlayerSpawn(playerid)
{
    if(PlayerInfo[playerid][NewReg] == 0)
    {
        SetPlayerSkin(playerid, PlayerInfo[playerid][pSkin]); print("Stage 40");
        SetPlayerPos(playerid, PlayerInfo[playerid][Xpos], PlayerInfo[playerid][Ypos], PlayerInfo[playerid][Zpos]+0.3);    print("Stage 41");
    }
    else
    {
        SetPlayerPos(playerid, X, Y, Z); // CHANGE THIS FOR FIRST PLAYER SPAWN WHEN HE ENTERS THE GAME
    }
    print("Stage 42");
    return 1;
}
Delete your previous account and create a new one.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)