[Tutorial] Creating a File System that is Effcient
#1

So I am tired of seeing all these super fancy file saving methods, so I though I would show people how you can do the same thing that those super fancy file saving systems do in the end. It just cuts the conversion of the commands into what this is basically making it a little faster. I do use sscanf however but somebody could easy make a replacement for that if they wanted just for file saving which would cut a little time out.

pawn Code:
#include <a_samp>
#include <sscanf2>

// Basic Player Varibles That You Might Save
new Player_Pass[MAX_PLAYERS][16];
new Player_Cash[MAX_PLAYERS];
new Float:Player_Pos[MAX_PLAYERS][3];

main() return 1;

public OnPlayerConnect(playerid)
{
    Load_Player(playerid); // Mabey where you would load your player?
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    Save_Player(playerid); // Mabey you might save the player here?
    return 1;
}

Load_Player(playerid)
{
    new file[32], name[MAX_PLAYER_NAME], File:handle, string[128], var[128], val[128];
   
    GetPlayerName(playerid, name, sizeof(name));    // Get the file name
    format(file, sizeof(file), "players/%s", name);
   
    if(!fexist(file)) // check if file exist so it doesn't crash the server
    {
        return 0;
    }
   
    handle = fopen(file, io_read); // Open our handle to read
   
    while(fread(handle, string))
    {
        if(sscanf(string, "s[128]s[128]", var, val)) // Use sscanf to parse the line
        {
            if(!strcmp(var, "Pass", true)) // Compare Varible & Example of a String
            {
                format(Player_Pass[playerid], 16, "%s", val); // Move string to string -- note to put the proper password size
            }
            else if(!strcmp(var, "Cash", true)) // Compare Varible & Example of a Integer
            {
                Player_Cash[playerid] = strval(val); // Put string into integer
            }
            else if(!strcmp(var, "Pos[0]", true)) // Compare Varible & Example of a Float
            {
                Player_Pos[playerid][0] = floatstr(val); // Put string into float
            }
            else if(!strcmp(var, "Pos[1]", true))
            {
                Player_Pos[playerid][1] = floatstr(val);
            }
            else if(!strcmp(var, "Pos[2]", true))
            {
                Player_Pos[playerid][2] = floatstr(val);
            }
        }
    }
   
    fclose(handle); // complete the read so others can be opened later
   
    return 1;
}

Save_Player(playerid)
{
    new file[32], name[MAX_PLAYER_NAME], string[128], File:handle;
   
    GetPlayerName(playerid, name, sizeof(name));
    format(file, sizeof(file), "players/%s", name);
   
    if(fexist(file)) // Check to see if our file exist
    {
        fremove(file); // Delete our old file, so we can make the new one.
    }
   
    handle = fopen(file, io_append); // Open our handle to append
   
    format(string, sizeof(string), "Pass %s\r\n", Player_Pass[playerid]); // format our string that we are going to write
    fwrite(handle, string); // write our string
   
    format(string, sizeof(string), "Cash %i\r\n", Player_Cash[playerid]); // repeat
    fwrite(handle, string);
   
    format(string, sizeof(string), "Pos[0] %f\r\n", Player_Pos[playerid][0]);
    fwrite(handle, string);
   
    format(string, sizeof(string), "Pos[1] %f\r\n", Player_Pos[playerid][1]);
    fwrite(handle, string);
   
    format(string, sizeof(string), "Pos[2] %f\r\n", Player_Pos[playerid][2]);
    fwrite(handle, string);
   
    fclose(handle); // Close our handle to complete the save.
   
    return 1;
}
Reply


Messages In This Thread
Creating a File System that is Effcient - by AustinJ - 27.03.2012, 04:59
Re: Creating a File System that is Effcient - by Jantjuh - 27.03.2012, 05:16
Re: Creating a File System that is Effcient - by AustinJ - 27.03.2012, 05:20
Re: Creating a File System that is Effcient - by T0pAz - 27.03.2012, 05:42
Re: Creating a File System that is Effcient - by AustinJ - 27.03.2012, 05:49
Re: Creating a File System that is Effcient - by TheLazySloth - 27.03.2012, 08:25
Re: Creating a File System that is Effcient - by Hiddos - 27.03.2012, 12:29
Re: Creating a File System that is Effcient - by Sting. - 27.03.2012, 14:04
Re: Creating a File System that is Effcient - by TheLazySloth - 06.04.2012, 07:01
Re: Creating a File System that is Effcient - by Jonny5 - 06.04.2012, 14:59

Forum Jump:


Users browsing this thread: 1 Guest(s)