- Home /
How to put Streamwriter into an array?
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.
Answer by FatWednesday · Sep 17, 2012 at 09:22 AM
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[i]);
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).
This partially helps, though I still a little stuck. I can understand the basic idea, though I unsure where to put these delimiters. Within, the code, Within, the text file itself?.
I am trying to make it so I have the top 5(or 4)scores displayed in separate(or one big.) textfield(s). Though I thought that would be another question.
Can you give me an example code of what you mean?
the delimiters are stored as part of the text itself. i will stuck some example code in the post so that i can format it.
Hmm, getting closer,I understand what you mean better, though I can't figure how out to integrate/swap "GuiDraw.currentScore" with "scores" Would I define scores as an array then make scores equal GuiDraw.currentScore?
(GuiDraw.currentScore is a int)
well if GuiDraw.currentScore is an array then you could do that, but it seems that your score value there is just a single score, so no you wouldn't be able to assign that if you tried. But its difficult to say how you would replace currentScore because im not entirely sure how your scoring system works, and what exactly you want this array storage for.
I'm trying to make it so that when it writes the scores it will write up to five scores ins$$anonymous$$d of just overwriting the first one.
I also wish to have the scores ordered by highest score but that is for another question.
I thought that an array would probably be the easiest to separate one score into five but maybe not.
Your answer
Follow this Question
Related Questions
Getting A Variable From a Outside Text File 1 Answer
Why isn't my high score sticking? 2 Answers
Implement own highscore script into existing FB SDK example 1 Answer
I want to keep coins and score after player`s death, and also to have high score 1 Answer
Central score counter for 3 score generating buttons 1 Answer