Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by ILoveMyDada · Aug 22, 2017 at 09:12 AM · arrayfor-loophighscoreslength

Arrays and For loops question

Hi I'm following a tutorial online to figure out how to compile a list of high scores in a game. I understand most of this code following along but the only section that I can't wrap my head around is all the code under the FormatHighscores method. It's very confusing to me and I'm just learning how to do this.

I understand why that method is being used, but I don't understand the lines of code within it that makes it do what it does.

If anyone can give me a brief explanation if possible, that would be great. The vid doesn't explain much during that part.

Thanks!

 const string privateCode = "YaCRIoQQDkG39N9_tr3CygJgI08xYI2EylCY7MXRXoA"; 
 const string publicCode = "599be7366b2b651a6cba683"; 
 const string webURL = "http://dreamlo.com/lb/";

 public Highscore[] highscoresList;






 void Awake()
 {
     AddNewHighscore ("Sean", 10);
     AddNewHighscore ("Lucas", 8);
     AddNewHighscore ("Austin", 2); 

     DownloadHighscores ();

 }




 public void AddNewHighscore (string username, int score)
 {
     StartCoroutine (UploadNewHighscore (username, score));
 }


 IEnumerator UploadNewHighscore(string username, int score)
 {
     WWW www = new WWW (webURL + privateCode + "/add/" + WWW.EscapeURL (username) + "/" + score);

     yield return www;

     if (string.IsNullOrEmpty (www.error)) 
     {
         print ("Upload Successful!");
     }

     else
     {
         print ("Error Uploading: " + www.error);
     }
 }





 public void DownloadHighscores()
 {
     StartCoroutine ("DownloadHighscoresFromDatabase");
 }


 IEnumerator DownloadHighscoresFromDatabase()
 {
     WWW www = new WWW (webURL + publicCode + "/pipe/");

     yield return www;

     if (string.IsNullOrEmpty (www.error)) 
     {
         FormatHighscores (www.text);
     }

     else
     {
         print ("Error Downloading: " + www.error);
     }
 }
     




 void FormatHighscores(string textStream)
 {
     string[] entries = textStream.Split (new char[] { '\n' }, System.StringSplitOptions.RemoveEmptyEntries); 

     highscoresList = new Highscore[entries.Length];

     for (int i = 0; i < entries.Length; i++) 
     {
         string[] entryInfo = entries [i].Split (new char[] { '|' });

         string username = entryInfo [0];

         int score = int.Parse (entryInfo [1]);

         highscoresList[i] = new Highscore (username, score);

         print (highscoresList [i].username + ": " + highscoresList [i].score);
     }
 }

}

public struct Highscore { public string username; public int score;

 public Highscore(string _username, int _score)
 {
     username = _username;

     score = _score;

 }

}

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
3
Best Answer

Answer by akillingbeck · Aug 22, 2017 at 09:33 AM

 string[] entries = textStream.Split (new char[] { '\n' }, System.StringSplitOptions.RemoveEmptyEntries); 

\n is an escape sequence. It indicates a new line in the text. This line of code reads textStream characters until it reaches a \n, when it does it stores the characters it has read in a new string. This will happen for every \n that occurs. It will finish when it reaches the end character of textStream. It then adds all the newly created strings to a string array (string[])

  for (int i = 0; i < entries.Length; i++) 
      {
          string[] entryInfo = entries [i].Split (new char[] { '|' });
          string username = entryInfo [0];
 

This second loop, does the same thing, but now it's working on each newly created string from the previous section. It assumes these strings are in the format: "name | score". Using the | as the splitting character means for each line you'll get 2 new strings , "name" and "score". name is already a string, so we can just set the username to be that. score will be in integer format, which is why the parse happens.

Comment
Add comment · Show 2 · 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 DJT4NN3R · Aug 22, 2017 at 09:45 AM 0
Share

Very detailed answer!

avatar image ILoveMyDada · Aug 22, 2017 at 10:10 AM 0
Share

Ah I gotcha. Thanks for the response!

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

78 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to save a 2d-array in C# 2 Answers

Sorting Javascript array by Distance 0 Answers

Making an Array of Animations Play at Random With No Repeats 3 Answers

Values in Array Automatically Revert 1 Answer

rotating vertices with for loop in 0 Answers


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