SA-MP Forums Archive
File save/loading system - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: File save/loading system (/showthread.php?tid=394670)



File save/loading system - thefatshizms - 23.11.2012

Hello, trying to 're-learn' PAWN to get more knowledge of the language and hopefully get better
I want to make a saving and loading system for players with the default sa-mp file functions i know how to write the value

I mean i could possible find the field name (like "score")
But when trying to find when its "score 123" 123 being the value and the only thing i want to retrieve
How would i do this?


Re: File save/loading system - dr.lozer - 23.11.2012

use
pawn Код:
SetPVarInt(playerid,"Score",value);
pawn Код:
GetPVarInt(playerid,"Score");



Re: File save/loading system - thefatshizms - 23.11.2012

Quote:
Originally Posted by dr.lozer
Посмотреть сообщение
use
pawn Код:
SetPVarInt(playerid,"Score",value);
pawn Код:
GetPVarInt(playerid,"Score");
What? I'm talking about loading scores, money etc from a file not setting a players player variable


Re: File save/loading system - dr.lozer - 23.11.2012

Oh! use Dini.inc Functions

Click here to download Dini.inc

EXAMPLE:
pawn Код:
stock PlayerName(playerid)
{
   new Name[24];
   GetPlayerName(playerid, Name,24);
   return Name;
}

CMD:loadmymoneyfromaccount(playerid)
{
   new File[256]; format(File,256,"Users/%s.sav",PlayerName(playerid));
   ResetPlayerMoney(playerid);
   GivePlayerMoney(playerid, dini_Get(File,"Money"));
   return 1;
}



Re: File save/loading system - thefatshizms - 23.11.2012

Quote:
Originally Posted by thefatshizms
Посмотреть сообщение
Hello, trying to 're-learn' PAWN to get more knowledge of the language and hopefully get better
I want to make a saving and loading system for players with the default sa-mp file functions i know how to read the value

I mean i could possible find the field name (like "score")
But when trying to find when its "score 123" 123 being the value and the only thing i want to retrieve
How would i do this?
:facepalm: Don't you read? I want to learn how to do it with the default functions, not with a include
This is because I want to learn everything on PAWN + I might want to make my own ini system


Re: File save/loading system - dr.lozer - 23.11.2012

hmmm if that what you mean i only know how to write in file

Use this:

pawn Код:
new File:File1,PATH[256],string[128];

public OnPlayerSpawn(playerid)
{
   format(PATH,126,"Users/%s.sav",PlayerName(playerid));
   File1 = fopen(PATH,io_append);
   format(string,128,"Money: %d\r\n",GetPlayerMoney(playerid));
   fwrite(File1,string);
   format(string,128,"Score: %d\r\n",GetPlayerScore(playerid));
   fwrite(File1,string);
   fclose(File1);
   return 1;
}



Re: File save/loading system - thefatshizms - 23.11.2012

I know how to write, im not asking that


Re: File save/loading system - dr.lozer - 23.11.2012

Man! I suck in english! i dont understand english that much :P


Re: File save/loading system - iggy1 - 23.11.2012

I have an example of how you might do it. I know this isn't the best way to do it but it's the smallest i could think of.

pawn Код:
stock GetIniInt(FilePath[], Key[])
{
    if( fexist(FilePath) )
    {
        new
            pos,
            line[128],
            File: file_handle = fopen(FilePath, io_read)
        ;
        while( fread(file_handle, line) )//loop through the file
        {
            if( (pos = strfind(line, Key)) != -1 )//search the line for the Key
            {
                fclose(file_handle);
           
                new idx = pos + strlen(Key)+1;//+1 is for the equals sign
                return (idx<sizeof(line)) ? strval(line[idx]) : 0;//return the converted value or zero if out of bounds
            }
        }
        fclose(file_handle);
    }
    return 0;
}
//To use

printf("TEST score == %d", GetIniInt("test.ini", "score"));
This isn't really safe to use for all types of ini file, it will only work if the ini format is "Key=Value" it will not work if the format is Key = Value (note the spaces). It also opens/closes the file each time it is called. For better ways of doing it you should look at some of the released file scripts, they will be much better than this.


Re: File save/loading system - thefatshizms - 23.11.2012

Quote:
Originally Posted by iggy1
Посмотреть сообщение
I have an example of how you might do it. I know this isn't the best way to do it but it's the smallest i could think of.

pawn Код:
stock GetIniInt(FilePath[], Key[])
{
    if( fexist(FilePath) )
    {
        new
            pos,
            line[128],
            File: file_handle = fopen(FilePath, io_read)
        ;
        while( fread(file_handle, line) )//loop through the file
        {
            if( (pos = strfind(line, Key)) != -1 )//search the line for the Key
            {
                new len = pos + strlen(Key)+1;
                return (len<sizeof(line)) ? strval(line[len]) : 0;//return the converted value or zero if out of bounds
            }
        }
        fclose(file_handle);
    }
    return 0;
}
//To use

printf("TEST score == %d", GetIniInt("test.ini", "score"));
This isn't really safe to use for all types of ini file, it will only work if the ini format is "Key=Value" it will not work if the format is Key = Value (note the spaces). It also opens/closes the file each time it is called. For better ways of doing it you should look at some of the released file scripts, they will be much better than this.
Thanks you have given me some insight

Ill be looking at some file systems now such as dini and yini and see how they do it