[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
#2

Nice! I've been looking for a long time for a tutorial for a save system with the standard file functions.
Just to learn from it because I've never worked before with it.

Thx!
Reply
#3

Thanks for your support!
Reply
#4

I'm sure it will help alot of people but you should explain more on variables, formats and the comparisons.
Reply
#5

I kind of made the tutorial for people that have moderate scripting skills and are able to understand the basics of variable manipulation and knowledge of comparisons and operators. If you don't know that kind of knowledge a person should really look at some basic scripting knowledge tutorials. Then at that point when they understand that knowledge they can jump into the harder things.
Reply
#6

This is nice indeed. You sir have helped me alot =] +rep
Reply
#7

This can actually become even more effective if you know in which order the lines are written. If you're 100% certain your file will look something like:
Code:
user=nakedhoe13 (useless to have something like this in a file tho)
pass=unknown
admin=-1
money=2020
You could just immediately take the values in order.

Edit: Also, this code:
pawn Code:
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
Would just be:
pawn Code:
handle = fopen(file, io_write);
The main problem I've got with files though is that it's somewhat hard to edit a point in a file (hence why I'm mainly using MySQL). Good tut, nevertheless.
Reply
#8

Nice one
Reply
#9

There's a bug in this type of system, when you load a string it loads the \r\n with it.
Reply
#10

StripNL() from YSI y_utils will remove the r\n\ i believe
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)