12 Replies - 1254 Views - Last Post: 08 March 2016 - 10:31 AM

#1 Mr n1nja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-March 16

High scores XNA help

Posted 08 March 2016 - 09:10 AM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: High scores XNA help

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • Posts: 53,476
  • Joined: 12-June 08

Re: High scores XNA help

Posted 08 March 2016 - 09:16 AM

So your text file has some value like "Current HighScore: 13" in it..

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


.. but you are trying to parse an integer out of that string? Example try parse "Current HighScore: 13" wouldn't be a number.

28	                  Int32.TryParse(s, out temp);


.. so if you are expecting a number remove the characters saying 'high score'
Was This Post Helpful? 0
  • +
  • -

#3 Mr n1nja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-March 16

Re: High scores XNA help

Posted 08 March 2016 - 09:27 AM

View Postmodi123_1, on 08 March 2016 - 09:16 AM, said:

So your text file has some value like "Current HighScore: 13" in it..

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


.. but you are trying to parse an integer out of that string? Example try parse "Current HighScore: 13" wouldn't be a number.

28	                  Int32.TryParse(s, out temp);


.. so if you are expecting a number remove the characters saying 'high score'


sw.WriteLine(player.highscore)


Int32.TryParse(s, out player.highscore)


It seems to have stopped outputting to the file now
Also it is only outputting to the debug part and not as actual text in the document, is that correct?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • Posts: 53,476
  • Joined: 12-June 08

Re: High scores XNA help

Posted 08 March 2016 - 09:32 AM

FYI - you don't need to quote the post right above yours.. just use the 'reply' button.


Quote

It seems to have stopped outputting to the file now

Then put a breakpoint on there and see what is happening. I am unclear what your objects are, if there are uncaught exceptions, etc.

Quote

Also it is only outputting to the debug part and not as actual text in the document, i

What?
Was This Post Helpful? 0
  • +
  • -

#5 Mr n1nja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-March 16

Re: High scores XNA help

Posted 08 March 2016 - 09:36 AM

Ok sorry about that.
I am not getting exception errors however i've just changed this to this

                             sw.WriteLine(player.highscore);


                             Console.WriteLine(player.highscore);



this now prints out the highscore to the debug output section however it does it multiple times and fails to store the high score when i reload the project.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • Posts: 53,476
  • Joined: 12-June 08

Re: High scores XNA help

Posted 08 March 2016 - 09:38 AM

The console is not a file.. that is literally a command console/terminal. Zero reasons why displaying it there would get it into a file.
Was This Post Helpful? 0
  • +
  • -

#7 Mr n1nja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-March 16

Re: High scores XNA help

Posted 08 March 2016 - 09:40 AM

I understand that, however i don't understand why this wouldn't?

                            using (StreamWriter sw = File.AppendText(path))
                            {
                                
                                sw.WriteLine(player.highscore);
                                                               
                            }

Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • Posts: 53,476
  • Joined: 12-June 08

Re: High scores XNA help

Posted 08 March 2016 - 09:45 AM

Again - I have no idea what the value of 'path' you have, or what play.highscore even is.

Put a break point on it and step through it... make sure values are being interpreted and paths are where they are supposed to be.
Was This Post Helpful? 0
  • +
  • -

#9 Mr n1nja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-March 16

Re: High scores XNA help

Posted 08 March 2016 - 09:53 AM

player.highscore is a global variable set in the player class
when i breakpoint it, it does store the value for example

		player.highscore	1000	int



the value of path is this

string path = "hScores.txt";


It just doesn't seem to write it to the text file when i use stream writer
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • Posts: 53,476
  • Joined: 12-June 08

Re: High scores XNA help

Posted 08 March 2016 - 10:05 AM

Then you are doing something oddball and are not aware of it or explaining it.

Try the more explicit way of doing streamwriter.. see if anything crops up.

  StreamWriter sw = new StreamWriter(path);
            sw.WriteLine(highscore);
            sw.Close();

Was This Post Helpful? 0
  • +
  • -

#11 Mr n1nja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-March 16

Re: High scores XNA help

Posted 08 March 2016 - 10:25 AM

Ok for some reason it only seems to work if i use the full directory from c:\users etc
However It still isn't storing the high score when i reload it so there must be a problem with my reading in

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


I'll try the same format as the other one you just provided.

StreamReader sr = new StreamReader(path);

string s = "";

while ((s = sr.ReadLine()) != null)
Int32.TryParse(s, out player.highscore);



works a charm at the moment
One more thing if you don't mind, is sw.dispose(); how you delete everything in the file?
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • Posts: 53,476
  • Joined: 12-June 08

Re: High scores XNA help

Posted 08 March 2016 - 10:30 AM

I would advocate reading the streamwriter docs.

"msdn streamwriter dispose"
https://msdn.microso...(v=vs.110).aspx
Was This Post Helpful? 0
  • +
  • -

#13 Mr n1nja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-March 16

Re: High scores XNA help

Posted 08 March 2016 - 10:31 AM

Thank you for your patience and your help much appreciated :)

Have a nice day.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1