How to elegantly make a high score table.

Started by HAL, Wed 25/02/2015 12:08:06

Previous topic - Next topic

HAL

I'm making an arcade-style game with AGS.

I have a high score table and it works.

However it took a huge amount of code and I'm certain there must be an easier way to do it.

It's a 'top ten' thing and it saves the scores so that when you reopen the game they're still there.
Hence I used arrays to hold and distribute the scores.

However I then found that I couldn't read file data back into the arrays.
I was fine writing the arrays to the file, but I couldn't read them back out again.
So I had to change it, so each score is sent to its own file and then retrieved individually like this:

Code: ags

function Get_Scores() // This function reads the scores from file then distributes them to array elements accordingly.
{
  File *HighScore1 = File.Open("HighScore1.dat",eFileRead);
  scores[0] = HighScore1.ReadInt();
  HighScore1.Close();
  File *HighScore1name = File.Open("HighScore1name.dat",eFileRead);
  names[0] = HighScore1name.ReadStringBack();
  HighScore1name.Close();
  
  File *HighScore2 = File.Open("HighScore2.dat",eFileRead);
  scores[1] = HighScore2.ReadInt();
  HighScore2.Close();
  File *HighScore2name = File.Open("HighScore2name.dat",eFileRead);
  names[1] = HighScore2name.ReadStringBack();
  HighScore2name.Close();
  
// etc etc etc up to 'HighScore10'



As you can see, I needed a separate section of script for every single value of 'name' and 'score'. So 10 chunks in total.

In the manual and in forum searches I couldn't find anything.
The manual says 'ReadInt' is for reading a single integer back into place, and 'ReadStringBack' is for reading a single string back in. No mention of reading array values from a file.

Did I miss something, or do something wrong, or is my long solution the only way?

Thanks. :smiley:

Crimson Wizard

#1
When you work with arrays you need to know how to work with loops. Loops are used whenever you need to process all (or some) items from array.

If your problem is how to read an unknown number of entries from a file, then all you have to do is to add "number of entries" to the file (as a first thing).

Writing:
Code: ags

File *HighScore = File.Open("HighScore.dat",eFileWrite);
HighScore.WriteInt(HighscoresNumber);
int i;
while (i < HighscoresNumber)
{
    HighScore.WriteInt(score[i]);
    HighScore.WriteString(names[i]);
    i++;
}


Reading:
Code: ags

File *HighScore = File.Open("HighScore.dat",eFileRead);
HighscoresNumber = HighScore.ReadInt();
int i;
while (i < HighscoresNumber)
{
    score[i] = HighScore.ReadInt();
    names[i] = HighScore.ReadStringBack();
    i++;
}

HAL

Oh christ I've just remembered this is exactly what I did already!!! I know about loops and used them extensively for most of the high score table.

I'm a total idiot for forgetting though.

I changed it to the 'long' method because I thought surely this isn't right, as there was something loosely related to the High Score table which wasn't functioning properly, so I put the entries into separate files to see if I could remedy it that way.

Needless to say, it didn't help.

However please don't feel that your reply was a waste of time. I had actually decided that my original method was the root of my other problems, but now you've confirmed that this method is correct, so thanks for that.

SMF spam blocked by CleanTalk