Jump to content
  • Advertisement
Sign in to follow this  
Mr N1nja

High scores XNA help

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

If you intended to correct an error in the post then please contact us.

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.
                            using (StreamReader sr = File.OpenText(path))
                            {
                                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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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

 

your text file would contain:

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!