I am creating a High Score List and but I want new scores to not overwrite the old.
So i thought about putting my Streamwriter into an array.
Here is my code so far.
import System.IO;
function Update () {
// Create an instance of StreamWriter to write text to a file.
sw = new StreamWriter("HighScore.txt");
// Add some text to the file.
sw.Write(GuiDraw.currentScore);
sw.Close();
}
So I have this and it is being read in another code. I thought an array would be the easiest way to put multiple scores ( onto different lines). Problem is i’m not sure how to do it.
Help Please.
well since you’re storing the values as just text by the looks of things, you need to write in delimiters. These are special characters that you know wont appear in the data itself, (so for scores, something like a pipe character ‘|’ or a hash #) you could even put in groups of characters to search for (like /SCORE/) but that takes up more space than you really need.
Then when you read in from the text file, you simply take the string data and use the String.Split() command (providing the delimiter) and it will break the string down into an array for you.
hope this helps.
(EDIT: some example code)
Lets say you have an array of scores called…well scores. this loop would write them all to your file, and put a # symbol between each score.
for(var i = 0; i < scores.length; i++)
{
sw.Write(scores*);*
- if(i!= scores.length-1) sw.Write(“#”);*
}
this would give you a text file like 100#9000#200#150#10
then when you read the string back in from the file later, you simple split the string by the delimiter (the #) (im not sure of the exacts of this in java, but its sure it will be part of the string class).