Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SGamerXxX · Sep 16, 2012 at 09:50 PM · arraytextscorehighscorestreamreader

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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).

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SGamerXxX · Sep 17, 2012 at 09:56 PM 0
Share

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?

avatar image FatWednesday · Sep 18, 2012 at 08:59 AM 0
Share

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.

avatar image SGamerXxX · Sep 18, 2012 at 10:18 PM 0
Share

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)

avatar image FatWednesday · Sep 19, 2012 at 09:13 AM 0
Share

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.

avatar image SGamerXxX · Sep 20, 2012 at 07:57 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges