- Home /
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;
}
}
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.
Your answer
Follow this Question
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