I understand, I have a full-time job and I go to college full-time aswell. Learning how to save variables took me nearly 4 days!! Needless to say I lost several minutes of good TV time.
pawn Код:
public SavePlayer(playerid);//public means you also have to add 'forward SavePlayer(playerid)' at the top, this is to be ran under OnPlayerDisconnect
{
new pname[MAX_PLAYER_NAME]; //A string to save the player's name to
GetPlayerName(playerid,pname,sizeof(pname));//Saving the player's name to the string array
new string[75];//A new string to save the file's name to and for placing information in
format(string,sizeof(string),"%s.ini",pname);//%s will be 'pname' or the player's name and the file extension I'm using is ini
new File:file = fopen(string,io_write);//a variable that's a file uses the 'File:' prefix and will hold the return for 'fopen' where I use 'io_write' because we don't care if the file already exists, we're overwriting/creating it
//Saving Regular Variables (integers)
format(string,sizeof(string),"Cash %d",GetPlayerMoney(playerid));//Recycling string and setting it as the 'Cash' variable (uneccesary but pretty and explained further on) then '%d' ('d' for decimal, can also use 'i' for integer) which will be the player's current cash, you could replace this with another variable that's holding an integer (PlayerInfo[playerid][pAdmin] for example)
fwrite(file,string); //Writing to 'file' (fopen(string,io_write);) and writing 'string' (Cash %d)
//Saving Float Variables (numbers with a decimal point '5.75')
new Float:x,Float:y,Float:z; //Creating new float variables to be set as the x,y,z coordinates of a player
GetPlayerPos(playerid,x,y,z); //Setting x,y,z
format(string,sizeof(string),"\nXPosition %f",x);//this is just 1 of the coordinates, notice '%f' for Float. the '\n' means new line, so your file doesn't look like "Cash 200XPosition -1245.0043" or some crap
fwrite(file,string); //writing to the file again, no change. This will jsut continue off from where the last fwrite stopped.
//Saving String Arrays (new string[20];string = "This is a string")
format(string,sizeof(string),"PlayerName %s",pname)//Nothing like recycling strings! '%s' for string
fwrite(file,string);
}
public LoadPlayer(playerid);//forward this one too, this goes where you want the information to be loaded in your script (/login or OnPlayerConnect)
{
new pname[MAX_PLAYER_NAME]; //the same ol' player name string
new string[75]; //everyone's favorite hippy string
GetPlayerName(playerid,pname,sizeof(pname));//Loading player's name to pname again
format(string,sizeof(string),"%s.ini",pname);//Getting the file's name
if(!fexist(string))return 0; //But Oh no! Some one new has joined the day-time show. Use this to prevent from loading a file that doesn't exist and resulting in a server crash
new File:file = fopen(string,io_read);//Notice how it's io_read this time
LOADPLAYERLOOP: //My method of reading each line in a file (incase you add a new line or perhaps someone placed them in the wrong order, this is a label, you can use 'goto LOADPLAYERLOP' to return to this exact point in the script
fread(file,string);//reading the first line and pacing it on string
if(!strlen(string))return 1; //This means that there are no more lines to read and stops the loop
//Load regular integer variable
if(!strcmp(string,"Cash",true,4)) //If the first bit of the line says 'Cash' then this passes through, otherwise skips the next bit
{
SetPlayerMoney(playerid,strval(string[5]));//Set the player's money to the value that's written after 'Cash ' (that's 5 characters including space, hence 'string[ >5< ]'
goto LOADPLAYERLOOP; //Go back to LOADPLAYERLOOP and restart with the next line
}
//Load Float Variable
if(!strcmp(string,"XPosition",true,9))//XPosition is 9 characters long
{
PlayerInfo[playerid][posX] = floatstr(string[10]); //This time I'm using 'floatstr' because I want a float, I'm saving it to a float variable because I need the rest of the coordinates (y and z) before placing the player anywhere
goto LOADPLAYERLOOP;
}
//Load String
if(!strcmp(string,"PlayerName",true,10))//PlayerName is 10 characters long
{
format(PlayerInfo[playerid][PlayerName],MAX_PLAYER_NAME,"%s",string[11])//I use format because frankly I'm afraid of messing up strmid, there's really no point in saving or loading your player's name, but this is an example
goto LOADPLAYERLOOP;
}
printf("Empty Line or Invalid Destination in %s.ini",pname);//You probably won't see this happen, but in the event there's a random empty line or someone added something without giving it purpose, this will log it (non-harmful)
}