SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 14/08/2011, 10:06 PM   #1
PhoenixB
Huge Clucker
 
PhoenixB's Avatar
 
Join Date: Mar 2011
Location: Pawno.exe
Posts: 421
Reputation: 46
Post How to SavePlayerPos

Tutorial
How to SavePlayerPos[Works for me 100%.]
For a Y_INI SaveSystem.


In this tutorial i will be showing you how to Save your players position when he is Disconnected/Logged out and when he/she is logging into the server, they will be spawned at the position they have logged out of. I have made a tutorial before this but the coding was not Correct as it only saved the position for ID 0. An example of what happened with the old coding is JaTochNietDan told me thankfully.

Code:
PhoenixB joins server (ID: 0)
PhoenixB leaves server and his position is saved

JaTochNietDan joins server (ID: 0)
JaTochNietDan spawns at the position that was saved for PhoenixB

So your code is not doing what people are "searching for" which is a persistant saving system for unique players, not just the ID's that people assume when they join a server.
But in this coding im explaining in the tutorial it will work for all players. As it was tested on the new version of Extraterrestrial Roleplay im currently coding with me and a few others. and it worked a charm. So lets get started!

Step 1 - Adding into the PlayerInfo system

At this stage i am assuming you must have a PlayerInfo system in your Registration System, It is similar to PlayerVariable. Underneath you enum pInfo{ you will add the following and i will explain the roles of each one and what they will collect from the player

Code:
	
Float:pLastX,
Float:pLastY,
Float:pLastZ,
Now, the coding above will collect the positioning of the player, It will grab the LastX LastY and the LastZ position of the player and store it in the PlayerInformation system that you have, so overall i currently have:

pawn Code:
enum pInfo
{
    Float:pLastX,
    Float:pLastY,
    Float:pLastZ,
}
new PlayerInfo[MAX_PLAYERS][pInfo];

Step 2 - Saving the Positions

Now im assuming you would like the last position to be SAVED to the players file on their Logout/Disconnect right? So now i will be explaining how to do that. Now i have OnPlayerLogout so if you have the same follow me on it but if you do not its perfectly fine, i will explain both ways. Underneath either OnPlayerLogout or OnPlayerDisconnect (They are both the same thing, difference is OnPlayerLogout is called as OnPlayerDisconnect). Place the following coding under the one that suits you and then i will explain what it does. Remembering that I am using Y_INI so this tutorial for Saveing PlayerPosition and loading it on Spawn for Y_INI reg system using scripts.

pawn Code:
INI_WriteFloat(playerFile, "LastX", PlayerInfo[playerid][pLastX]);
    INI_WriteFloat(playerFile, "LastY", PlayerInfo[playerid][pLastY]);
    INI_WriteFloat(playerFile, "LastZ", PlayerInfo[playerid][pLastZ]);

We are going to Write these as Floats because they were defined as Floats on the PlayerInfo system and they MUST be, in this tutorial i will not explain what a Float if you dont know what a Float is, learn please. Now the coding above.

INI_WriteFloat - Writes it as a float into the player scriptfile.
playerFile - Places the data onto your Playerfile (In my case anyway, dont know your system as i havent seen it)
"LastX - LastZ" - How it will appear on the players .ini file at Scriptfiles, what it will say.
PlayerInfo - Grabbing the data from the PlayerInfo system we have.
playerid - For the player that is disconnecting.
pLastX - pLastZ - The Last Positions we have on the PlayerInfo system but on the playerinfo system they are as Float:!?? dont worry, its how its meant to be. Learn about Floats.

So overall we should have a code like this


For OnPlayerDisconnect:

pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
        INI_WriteFloat(playerFile, "LastX", PlayerInfo[playerid][pLastX]);
    INI_WriteFloat(playerFile, "LastY", PlayerInfo[playerid][pLastY]);
    INI_WriteFloat(playerFile, "LastZ", PlayerInfo[playerid][pLastZ]);
    return 1;
}

For OnPlayerLogout:

pawn Code:
OnPlayerLogout(playerid)
{
        INI_WriteFloat(playerFile, "LastX", PlayerInfo[playerid][pLastX]);
    INI_WriteFloat(playerFile, "LastY", PlayerInfo[playerid][pLastY]);
    INI_WriteFloat(playerFile, "LastZ", PlayerInfo[playerid][pLastZ]);
        return 1;
}
I do this under OnPlayerLogout because under my OnPlayerDisconnect i make it refer to (do the same as) OnPlayerLogout so my OnPlayerDisconnect looks like

pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
    OnPlayerLogout(playerid);
    return 1;
}

Though this depends on the Registration System that you are using. Now that we have the positioning saved, its time to make it the players spawn so add the following underneath your OnPlayerSpawn, now we are placing the code underneath here because it will make it the players spawn point now look at the code and you will understand slightly what i mean, i will explain under the coding what each function of the code does.


pawn Code:
public OnPlayerSpawn(playerid)
{
    if(PlayerInfo[playerid][pLastX] == 0.0 && PlayerInfo[playerid][pLastY] == 0.0)
    {
        SetPlayerPos(playerid, X, Y, Z);
    }
    else SetPlayerPos(playerid, PlayerInfo[playerid][pLastX], PlayerInfo[playerid][pLastY], PlayerInfo[playerid][pLastZ]);
    return 1;
}

What does this code do? well let me tell you. As you can see we are opening the coding with
if(PlayerInfo[playerid][pLastX] == 0.0 && PlayerInfo[playerid][pLastY] == 0.0) and this means that the player hasnt got a saved position prefably because he hasnt registered. So it will detec that IF the players Last X and Y positons where 0.0 we open the bracket and type SetPlayerPos, so what we are doing now is SETTING the player's position to the default spawn point you want so you will add the X,Y,Z positions you want your player to start off at in the script.

SetPlayerPos(playerid, X,Y,Z); // The X,Y,Z part is for you to fill.
Then we are going to close the function with a bracket like above. though we do not place the return call yet! if the player has a LastX Y and Z position we are now going to make it their spawn point - As we have placed underneath our Disconnection function it will get the Last positions from there so it will SET the players Last positions they disconnected/logged out from using the OnPlayerDisconnect function or in my case OnPlayerLogout function.

else SetPlayerPos(playerid, PlayerInfo[playerid][pLastX], PlayerInfo[playerid][pLastY], PlayerInfo[playerid][pLastZ]); // This automatically means ELSE if the player has a LastX -Y positioning the player will be set at those positions.


Step 3 - Your done!

Now that you have done it, i hope this works for you as it has worked for me so lets review the full coding shall we?

pawn Code:
enum pInfo
{
    Float:pLastX,
    Float:pLastY,
    Float:pLastZ,
}
new PlayerInfo[MAX_PLAYERS][pInfo];

public OnPlayerDisconnect(playerid, reason)
{

    INI_WriteFloat(playerFile, "LastX", PlayerInfo[playerid][pLastX]);
    INI_WriteFloat(playerFile, "LastY", PlayerInfo[playerid][pLastY]);
    INI_WriteFloat(playerFile, "LastZ", PlayerInfo[playerid][pLastZ]);
    return 1;
}

public OnPlayerSpawn(playerid)
{
    if(PlayerInfo[playerid][pLastX] == 0.0 && PlayerInfo[playerid][pLastY] == 0.0)
    {
        SetPlayerPos(playerid, -299.8857, 1015.5681, 19.5938);
    }
    else SetPlayerPos(playerid, PlayerInfo[playerid][pLastX], PlayerInfo[playerid][pLastY], PlayerInfo[playerid][pLastZ]);
    return 1;
}

Now Im assuming you player the coding on OnPlayerDisconnect/OnPlayerlogout above your INI_Close(playerFile); IF you do have one, if you do not then this tutorial is useless for you. So please let me know if this worked for you as it has worked for me 100% and that i have tested it!
PhoenixB is offline   Reply With Quote
Old 15/08/2011, 09:22 AM   #2
Basicz
Gangsta
 
Basicz's Avatar
 
Join Date: Aug 2010
Location: Indonesia, Surabaya.
Posts: 621
Reputation: 35
Default Re: How to SavePlayerPos

For you, it works 100%

In my logic, it works 0%

pawn Code:
undefined symbol "playerFile"

pawn Code:
#define playerFile "%s.ini"
stock getini( playerid ) {
    new name[24]; GetPlayerName( playerid, name, 24 ); new file[ 64 ]; format( file, playerFile, name );
    return file;
}

You didn't even load it stats when a people connects.
pawn Code:
public OnPlayerConnect( playerid )
{
    if ( fexist( getini( playerid ) ) )
    {
        INI_ParseFile( getini( playerid ), "position", .bExtra = true, .extra = playerid );
    }
 
    return 1;
}

forward position( playerid, name[], value[] );
public  position( playerid, name[ ], value[ ] )
{
    INI_Float( "LastX", playerInfo[ playerid ][ posX ] );
    // .......

    return 1;
}

// AND
stock onPlayerLogout( playerid )
{
    new
        INI: pFile = INI_Open( getini( playerid ) )
    ;

    INI_WriteFloat(pFile, "LastX", PlayerInfo[playerid][pLastX]);
    INI_WriteFloat(pFile, "LastY", PlayerInfo[playerid][pLastY]);
    INI_WriteFloat(pFile, "LastZ", PlayerInfo[playerid][pLastZ]);

    INI_Close(pFile);

    return 1;
}

public OnPlayerDisconnect( playerid )
    return onPlayerLogout( playerid );

I guess, it worked in your server, but it didn't even compile ( the tutorial )
I guess, you didn't used Y_INI.

Not bad, you are good in explaining.
__________________

- Quina, from the game " Final Fantasy IX "
Basicz is offline   Reply With Quote
Old 15/08/2011, 12:53 PM   #3
PhoenixB
Huge Clucker
 
PhoenixB's Avatar
 
Join Date: Mar 2011
Location: Pawno.exe
Posts: 421
Reputation: 46
Default Re: How to SavePlayerPos

Oh no it worked for me, i didnt have to put stuff like that. I have completely different types. such as LoadUserData and OnPlayerLogout and OnPlayerLogin. I felt not needing to explain them
PhoenixB is offline   Reply With Quote
Old 03/09/2011, 01:40 PM   #4
SantarioLeone
Big Clucker
 
Join Date: Jul 2008
Posts: 164
Reputation: 23
Default Re: How to SavePlayerPos

Didnt work for me at all.

Code:
C:\Users\Tab\Desktop\True Roleplay [0.3c]\True Roleplay [0.3c]\gamemodes\Scratch.pwn(198) : error 017: undefined symbol "playerFile"
C:\Users\Tab\Desktop\True Roleplay [0.3c]\True Roleplay [0.3c]\gamemodes\Scratch.pwn(199) : error 017: undefined symbol "playerFile"
C:\Users\Tab\Desktop\True Roleplay [0.3c]\True Roleplay [0.3c]\gamemodes\Scratch.pwn(200) : error 017: undefined symbol "playerFile"
C:\Users\Tab\Desktop\True Roleplay [0.3c]\True Roleplay [0.3c]\gamemodes\Scratch.pwn(207) : error 017: undefined symbol "X"
Pawn compiler 3.2.3664	 	 	Copyright (c) 1997-2006, ITB CompuPhase


4 Errors.
__________________

Easiest way to help your server stay alive with free hosting.Click Here
Working on a RP project from scratch, anyone interested in working together PM me, i have a beta host to test things.
SantarioLeone is offline   Reply With Quote
Old 03/09/2011, 01:44 PM   #5
PhoenixB
Huge Clucker
 
PhoenixB's Avatar
 
Join Date: Mar 2011
Location: Pawno.exe
Posts: 421
Reputation: 46
Default Re: How to SavePlayerPos

Its because playerFile is Defined in my Script as well as X, You must define them yourself. I have given you the IDEA of how its done.
PhoenixB is offline   Reply With Quote
Old 03/09/2011, 01:49 PM   #6
SantarioLeone
Big Clucker
 
Join Date: Jul 2008
Posts: 164
Reputation: 23
Default Re: How to SavePlayerPos

It would actually help if you explained it in more depth, EXACTLY what to do. In example At the top of the script you #define such and such ect... or #include. As there are many "Beginner" scripters who dont know how to do everything a tutorial is to help people understand how to do something and learn. It would help if you could explain better.
__________________

Easiest way to help your server stay alive with free hosting.Click Here
Working on a RP project from scratch, anyone interested in working together PM me, i have a beta host to test things.
SantarioLeone is offline   Reply With Quote
Old 06/09/2011, 02:56 PM   #7
sparkley
Little Clucker
 
Join Date: Aug 2011
Posts: 12
Reputation: 0
Default Re: How to SavePlayerPos

Quote:
Originally Posted by Basicz View Post
For you, it works 100%

In my logic, it works 0%

pawn Code:
undefined symbol "playerFile"

pawn Code:
#define playerFile "%s.ini"
stock getini( playerid ) {
    new name[24]; GetPlayerName( playerid, name, 24 ); new file[ 64 ]; format( file, playerFile, name );
    return file;
}

You didn't even load it stats when a people connects.
pawn Code:
public OnPlayerConnect( playerid )
{
    if ( fexist( getini( playerid ) ) )
    {
        INI_ParseFile( getini( playerid ), "position", .bExtra = true, .extra = playerid );
    }
 
    return 1;
}

forward position( playerid, name[], value[] );
public  position( playerid, name[ ], value[ ] )
{
    INI_Float( "LastX", playerInfo[ playerid ][ posX ] );
    // .......

    return 1;
}

// AND
stock onPlayerLogout( playerid )
{
    new
        INI: pFile = INI_Open( getini( playerid ) )
    ;

    INI_WriteFloat(pFile, "LastX", PlayerInfo[playerid][pLastX]);
    INI_WriteFloat(pFile, "LastY", PlayerInfo[playerid][pLastY]);
    INI_WriteFloat(pFile, "LastZ", PlayerInfo[playerid][pLastZ]);

    INI_Close(pFile);

    return 1;
}

public OnPlayerDisconnect( playerid )
    return onPlayerLogout( playerid );

I guess, it worked in your server, but it didn't even compile ( the tutorial )
I guess, you didn't used Y_INI.

Not bad, you are good in explaining.
Dont work for me, it just doesn't save the position !
Userfile:
Quote:
LastX = 0.000000
LastY = 0.000000
LastZ = 0.000000
About the - undefined symbol "playerFile" - I just changed the playerFile to File and it works now - I guess PhoenixB has a bit different code.
My code:

-----------------------------------------------------

public OnPlayerDisconnect(playerid, reason)
{
new INI:File = INI_Open(UserPath(playerid));
INI_SetTag(File,"data");
INI_WriteInt(File,"Cash",GetPlayerMoney(playerid)) ;
INI_WriteInt(File,"Admin",PlayerInfo[playerid][pAdmin]);
INI_WriteInt(File,"Kills",PlayerInfo[playerid][pKills]);
INI_WriteInt(File,"Deaths",PlayerInfo[playerid][pDeaths]);
INI_WriteFloat(File, "LastX", PlayerInfo[playerid][pLastX]);
INI_WriteFloat(File, "LastY", PlayerInfo[playerid][pLastY]);
INI_WriteFloat(File, "LastZ", PlayerInfo[playerid][pLastZ]);
INI_Close(File);
return 1;
}

-----------------------------------------------------
sparkley is offline   Reply With Quote
Old 06/09/2011, 06:07 PM   #8
PhoenixB
Huge Clucker
 
PhoenixB's Avatar
 
Join Date: Mar 2011
Location: Pawno.exe
Posts: 421
Reputation: 46
Default Re: How to SavePlayerPos

IF you read carefully i do explain about my system being different but this gives you an idea of the format you must make it..
PhoenixB is offline   Reply With Quote
Old 18/09/2011, 02:35 PM   #9
SantarioLeone
Big Clucker
 
Join Date: Jul 2008
Posts: 164
Reputation: 23
Default Re: How to SavePlayerPos

Also would be better if you could add saving the Interior, as well as Virtual World, or a player who disconnects inside a interior, and relogs, will spawn in the middle of the air.
__________________

Easiest way to help your server stay alive with free hosting.Click Here
Working on a RP project from scratch, anyone interested in working together PM me, i have a beta host to test things.
SantarioLeone is offline   Reply With Quote
Old 18/09/2011, 03:35 PM   #10
Kush
Gangsta
 
Join Date: May 2011
Posts: 783
Reputation: 123
Default Re: How to SavePlayerPos

Quote:
Originally Posted by PhoenixB View Post
IF you read carefully i do explain about my system being different but this gives you an idea of the format you must make it..
Simply looking at the code it doesn't work! Where are you getting the positions from, and how is it being saved? This is bad, 0/10.
__________________
Quote:
Originally Posted by Dripac
i have more reputation than you, that means i am more clever than you my friend
Kush is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SavePlayerPos PhoenixB Help Archive 0 13/08/2011 11:19 PM
SavePlayerPos PhoenixB Help Archive 0 07/08/2011 03:44 PM


All times are GMT. The time now is 08:26 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.