• 11
• 9
• 10
• 9
• 11

High scores XNA help

This topic is 776 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi all! Thanks for looking over this! If you could help me out it would be much appreciated!
Right ok....
I'm trying to create a very simple high scores.
The function I want is for the high scores to be displayed while in game ( only has to be the number, no names)
Next when the user dies I want it to write to a text file if the user has a new high score.
When the program starts up I want that high score to be saved ready for the next user.

This is what I have so far

                  if (score > player.highscore)
{
player.highscore = score;
string path = "hScores.txt";
// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
}
}
//
using (StreamWriter sw = File.AppendText(path))
{

sw.WriteLine("Current HighScore: " + player.highscore);
sw.Close();
}

// Open the file to read from.
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
int temp;
Int32.TryParse(s, out temp);
player.highscore = temp;
}
}

}
Currently that code stores the score in the text file however when I reload the project it doesn't show the High score in game as it reverts back to 0. Any pointers would be helpful thanks.

Share on other sites

My guess would be that you're parsing "Current HighScore: 1337" and that will return 0 because "Current" is not a number.. How about searching the string for ": " and parse what's after that?

I'm always using fopen, fclose etc. so don't know about the File method you're using, if it can do it for you...

EDIT: If it was me, I would write the score in a binary file and do some bitwise operations on it, so it's not as easy to manipulate :)

Edited by vinterberg

Share on other sites

your saved string consists of "Current Highscore: <some number>"

that's not an integer written out as a string that you can parse and convert to an int variable.

say the current high score was 123456

Current Highscore: 123456

to read a string and convert to int, the file should just have the number:

123456

alternately, you can just read and display the entire string:

printf("%s\n",s);

outputs:

Current Highscore: 123456

Edited by Norman Barrows