Question

I'm attempting to add a local high score system to my game and have run int a brick wall with the file reading and writing, according to programmers and lecturers I know in real life and a few tutorials on the internet my code should be working as I want it to, however whenever I start my game my high score isn't loaded, below is my functions for both reading and writing to the file, are there any silly mistakes I'm making?

public void ReadHighScore()
    {
        byte[] myByteArray = new byte[64]; // Creates a new local byte array with a length of 64
        using (var store = IsolatedStorageFile.GetUserStoreForApplication()) // Creates an IsolatedStorageFile within the User Storage
        using (var stream = new IsolatedStorageFileStream("highscore.txt", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, store)) // Creates a new filestream attatched to the storage file
        {
            if (stream != null) // Checks to see if the filestream sucessfully read the file
            {
                int streamLength = (int)stream.Length; // Gets the length of the filestream
                stream.Read(myByteArray, 0, streamLength); // Parses the filestream to the byte array
            }
            else
                myState = (int)game_state.Terminate; // Temporary Error checking, the function gets though this without triggering the 'terminate' gamestate
        }

        string ScoreString = myByteArray.ToString(); // Parses the byte array to a string
        Int32.TryParse(ScoreString, out highScore.score); // Parses the string to an integer
    }

    public void SaveHighScore()
    {
        byte[] myByteArray = new byte[64]; // Creates a new local byte array with a length of 64
        using (var store = IsolatedStorageFile.GetUserStoreForApplication()) // Creates an IsolatedStorageFile within the User Storage
        using (var stream = new IsolatedStorageFileStream("highscore.txt", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, store)) // Creates a new filestream attatched to the storage file
        {
            if (stream != null) // Checks to see if the filestream sucessfully read the file
            {
                int streamLength = (int)stream.Length; // Gets the length of the filestream
                stream.Write(myByteArray, 0, streamLength); // Parses the byte array to the filestream
            }
            else
                myState = (int)game_state.Terminate; // Temporary Error checking, the function gets though this without triggering the 'terminate' gamestate
        }
    }
}
Was it helpful?

Solution

First, there's a mistake in the reading part: you're opening the file with FileMode.Create.

And according to the documentation:

Specifies that the operating system should create a new file. If the file already exists, it will be overwritten.

So basically, your ReadHighScore is deleting the old file and creating a new one. Which is, I believe, not what you wanted to do. Replace FileMode.Create by FileMode.OpenOrCreate (only in the ReadHighScore method) and you should have a better result.


Also, there's an error in the SaveHighScore, on this line:

stream.Write(myByteArray, 0, streamLength);

Since you're creating the file, streamLength is supposed to be equal to 0. Therefore, you aren't writing anything. What you really want to do is:

stream.Write(myByteArray, 0, myByteArray.Length);

You should consider using StreamReader, StreamWriter, BinaryReader, and BinaryWriter to read/write into streams, as they are far easier to use.


Last but not least, the data you're reading and writing is wrong. In the SaveHighScore method, you're trying to save an empty array, the actual high score is nowhere to be found. In the ReadHighScore method, it's even worse: you're reading myByteArray.ToString(), which will always be equal to System.Byte[].


In the end, your code should be something like: (given that highScore.score is declared as int)

public void ReadHighScore()
{
    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (var stream = new IsolatedStorageFileStream("highscore.txt", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read, store))
        {
            using (var reader = new BinaryReader(stream))
            {
                highScore.score = reader.ReadInt32();
            }
        }
    }
}

public void SaveHighScore()
{
    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (var stream = new IsolatedStorageFileStream("highscore.txt", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, store))
        {
            using (var writer = new BinaryWriter(stream))
            {
                writer.Write(highScore.score);
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top