So I'm working on a little game and I need some recommendations on how to implementing saving of high scores and such.
This game has many levels, let's say 100s, and each game are very short.
For each level I want to be able to save the 5 best scores and a few other values related to each of those scores.
The binary format for the file could be something like this:
First integer in the file: <number of levels completed and saved>
5 integer holding the best scores for level 1
5 integer holding additional data for the best scores in level 1
5 integer holding additional data for the best scores in level 1
...
5 integer holding the best scores for level N
5 integer holding additional data for the best scores in level N
5 integer holding additional data for the best scores in level N
So each levels have a set of 5*3=15 integers
The initial method I wanted to implement was:
Game opens:
If
save.dat file already exist:
- read file content to memory in a structure
SavedData
else
- create
save.dat file
Player chooses a level X, plays it, completes it:
- Compare score with corresponding best scores in the structure
SavedData
-- If the score is higher than any best scores, update
SavedData,
BUT also write the new value to the file by:
--- open
save.dat, using the level number, go to the position in the file where the score should be updated, write the value.
My problem is that in Appgamekit, when writing to file, you can either overwrite the whole file or append to the end of the file.
It is not possible (as far as I know), to open an existing file, go to a certain location and overwrite a value.
You either write to a new file, or append at end of an existing one.
One solution would be to every time I want to save a value, rewrite all the data. But that seems very inefficient. If the user gets a new high score in level 134, I should only have to rewrite one value in the file, not the complete file.
I could also just keep the new high scores in memory while the user plays, then when he exit the game write all the data to a file. But I'm not sure how this would if the game closes unexpectedly.
I'm sure there is a reasonable way of doing this, so I'm hoping for some suggestions.
Thanks in advance.