Reading / Writing .dat data

Started by KodiakBehr, Fri 22/11/2013 13:05:09

Previous topic - Next topic

KodiakBehr

Hello.

At the top of my GlobalScript file, I have code that reads:

Code: ags

    void WriteAchievements(){ //Write achievement data to file
      File *i = File.Open("Achievements.dat",eFileWrite);
      i.WriteInt(ACHIEVEMENT1); 
      i.WriteInt(ACHIEVEMENT2);
      i.WriteInt(ACHIEVEMENT3);
      i.WriteInt(ACHIEVEMENT4);
      i.WriteInt(ACHIEVEMENT5);
      
    }

    void ReadAchievements(){//Read Achievement data from file
      if (!File.Exists("Achievements.dat")) return;
      File *i = File.Open("Achievements.dat",eFileRead);
      ACHIEVEMENT1 = i.ReadInt();
      ACHIEVEMENT2 = i.ReadInt();
      ACHIEVEMENT3 = i.ReadInt();
      ACHIEVEMENT4 = i.ReadInt();
      ACHIEVEMENT5 = i.ReadInt();
    }


When an achievement is earned, the ACHIEVEMENT variable becomes 1.

What's happening is those achievements are only being registered when that particular save game is loaded.  I would like these achievements to be persistent every time the game is loaded.

Could you kindly show me what am I doing wrong here?

Thanks,

KB

Khris

First of all, you should close the File after reading/writing values: i.Close();

In order to make achievements persist independent of save games:

When an achievement is unlocked:
-read values from the file
-change the ACHIEVEMENTX variable (check read value first in order to find out whether achievement was already unlocked)
-save values to the file

After a save game is loaded (on_event / eEventRestoreGame):
-read values from the file

KodiakBehr

Worked a treat.  Thank you Khris.

selmiak

you should crypt or somehow else obscure the file, otherwise everyone with a hexeditor can unlock the achievements very easy ;)

Adeel

Quote from: selmiak on Sun 24/11/2013 01:06:07
you should crypt or somehow else obscure the file, otherwise everyone with a hexeditor can unlock the achievements very easy ;)

You can open and edit it with just a notepad too. :=

monkey0506

Well actually Adeel, when using "WriteInt" it's not exactly a human-readable integer. The function writes the four bytes of the integer separately, so you'd have to do some manual calculations to convert the four bytes back into its corresponding decimal value. Most (if not all) hex editors also display a human-readable text pane alongside the hex data. That's not to say you couldn't just use Notepad, but given the likelihood of having non-displayable or non-human-readable bytes (any integer values less than 255 are going to have at least 2 null bytes (decimal value 0, ASCII '\0') which are neither displayable or human-readable) you'd be far better off with a hex editor for that. :=

SMF spam blocked by CleanTalk