14.08.2011, 22:06
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.
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
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:
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.
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:
For OnPlayerLogout:
I do this under OnPlayerLogout because under my OnPlayerDisconnect i make it refer to (do the same as) OnPlayerLogout so my OnPlayerDisconnect looks like
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.
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?
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!
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.
Код:
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.
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
Код:
Float:pLastX, Float:pLastY, Float:pLastZ,
pawn Код:
enum pInfo
{
Float:pLastX,
Float:pLastY,
Float:pLastZ,
}
new PlayerInfo[MAX_PLAYERS][pInfo];
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 Код:
INI_WriteFloat(playerFile, "LastX", PlayerInfo[playerid][pLastX]);
INI_WriteFloat(playerFile, "LastY", PlayerInfo[playerid][pLastY]);
INI_WriteFloat(playerFile, "LastZ", PlayerInfo[playerid][pLastZ]);
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 Код:
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;
}
pawn Код:
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;
}
pawn Код:
public OnPlayerDisconnect(playerid, reason)
{
OnPlayerLogout(playerid);
return 1;
}
pawn Код:
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;
}
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 Код:
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;
}