XNA Essentials

Game Programming for Xbox 360, PC + Windows Phone

NAVIGATION - SEARCH

High Scores tutorial problems

Hi Chad,


Thank you so much for the high scores tutorial! It's fantastic and has helped me out so much. Good advertising for the book too, since after reading and following the tutorial, I've now bought it and am waiting for it to arrive from Amazon.


However, I've had a a problem with the tutorial I was hoping you could help me with.


The first issue I encountered was that if the player's name was shorter than any of the names in the high score file (I'm making a copy of an old C64 game as a present for my mum's birthday, so I've set the PlayerName to "Mum"), the xml file became corrupted. There was leftover text at the end of the xml file (for example, there was usually an extra 'a>' at the end of the file.


I managed to solve this by changing FileMode.OpenorCreate to FileMode.Create, to make sure that the new save file cleared the old data rather than just writing over the top of it.


Having solved that problem, I've hit another and I can't figure out how to solve it.


If the player gets a high score, rather than inserting the player's score in the appropriate place on the high score table and moving everything else down a place, the player's name and score gets copied into the appropriate place and every place below that.


For example, if the player gets the second highest score out of five, the high score table will look like this:


1. Neil
2. Player 1
3. Player 1
4. Player 1
5. Player 1


I haven't the faintest idea why. The only things I've changed from the tutorial is to change PlayerName from "Player1" to "Mum" and  FileMode from FileMode.OpenorCreate to FileMode.Create.


Could either of those changes have caused this problem?


Thanks for your help!


 

Scribblor - Tuesday, May 25, 2010 @ 6:19 AM

Re: High Scores tutorial problems

I need to update the Highscores tutorial.  You are right that the code should be using .Create instead of .OpenorCreate.  That definitely wouldn't cause your problem.  Likewise, simply changing the text from Player 1 to Mum wouldn't cause the issue you are seeing.


Make sure the following code is correct:


        //New high score found ... do swaps
        for (int i = data.Count - 1; i > scoreIndex; i--)
        {
            data.PlayerName[i] = data.PlayerName[i - 1];
            data.Score[i] = data.Score[i - 1];
            data.Level[i] = data.Level[i - 1];
        }

        data.PlayerName[scoreIndex] = "Player1"; //Retrieve User Name Here
        data.Score[scoreIndex] = score;
        data.Level[scoreIndex] = currentLevel + 1;


It is starting with the last item in the list of high scores and moving up the list to where we need to put the score: scoreIndex.


As it loops through the records backwars it sets the current index value to the one before it.  data[i] = data[i - 1]


Finally, it sets the newly achieved high score by overwriting what is present at the scoreIndex.


Assuming the above is correct, you may want to just clear out the highscore XML file and start fresh to make sure that the issue is still present.


Hopefully this helps.  If not, feel free to post your updated code and I'll take a look. 


Thanks!


Chad


P.S. I'm actually not going to be near a computer for the next week, so it may be a bit before I respond.


 

Chad Carter - Friday, May 28, 2010 @ 11:00 PM

Re: High Scores tutorial problems

Hi Chad,


Thanks for getting back to me!


After reading your reply, I checked through my code line by line to make sure I had it all correct...


...and discovered that I was calling SaveHighScores() when the player died and then again immediately afterwards when I displayed the high scores on my 'Game Over' screen. D'oh!


Taking out the second SaveHighScores() solved the problem. Nothing to do with the high score code after all - just me sloppily checking my work.


Thanks for the help!

Scribblor - Saturday, May 29, 2010 @ 5:06 AM