- Home /
Text file read, random quote C#
//YES, there is the monobehavior part and all that, but i'm not concerned with it right now.
string quote;
public TextAsset splashFile;
void Start(){
string[] dataLines = splashFile.text.Split('\n');
string[][] dataPairs = new string[dataLines.Length][];
int lineNum = 0;
foreach (string line in dataLines){
dataPairs[lineNum++] = line.Split(',');
}
quote = dataPairs[Random.Range(0,lineNum++)][Random.Range(0,lineNum++)];
}
/*I have absolutely no idea what I am doing or what I am reading exactly. I have a text file that works like this:
hello,hi hey,hru
(For some reason the enter button isn't seperating between "hi" and "hey,") I am trying to get this script to work by having it select a random quote from the text file. What is this lineNum++, and what code did I just type in? I tried to manipulate it from this answers post but no luck so far: http://answers.unity3d.com/questions/16225/how-can-i-read-data-from-a-text-file-putting-a-lar.html
The error I get from my fail code is
"IndexOutOfRangeException: Array index is out of range. _StartMenu.Start () (at Assets/Resources/Scripts/_StartMenu.cs:41)"
Please help me get this code, and please try to explain nearly every line from void Start(){ to the end of the foreach part, because I am totally confused on what it's trying to do. */
Answer by Muuskii · Oct 17, 2012 at 05:15 AM
string quote;
public TextAsset splashFile;
void Start(){
string[] dataLines = splashFile.text.Split('\n');
string[][] dataPairs = new string[dataLines.Length][];
int lineNum = 0;
foreach (string line in dataLines){
dataPairs[lineNum++] = line.Split(',');
}
quote = dataPairs[Random.Range(0,lineNum++)][Random.Range(0,lineNum++)];
}
First two lines, declare a String and a TextAsset. You don't mention instantiating the text asset and that answer that you linked to only vaguely mentions that you must drag the text asset (the .txt file) into the slot where it says "splashFile" in the inspector while viewing a GameObject that has your script attached.
It is at this point that I need you to confirm that you have done this because it is a common mistake to make and can sometimes lead to similar errors, but probably not this one.
You indicated that you do not understand what "lineNum++" means: I would suggest that if you do not have any, or much programming experience that you first attempt to learn a language in a more formal (and supported) environment as there are more tutorials for a coder of your level for good 'ole C# than C# in Unity, at your level.
Continuing on for an explanation of the remaining lines:
string[] dataLines = splashFile.text.Split('\n');
string[][] dataPairs = new string[dataLines.Length][];
The first line (presumably) takes ALLLL of the text from the file and loads it into a string, and then splits it at every instance of the '/n' character. Which is usually the newline. (UnityAns is parsing out the slash, so just pretend that it's the right direction)
int lineNum = 0;
foreach (string line in dataLines){
dataPairs[lineNum++] = line.Split(','); //This line here
}
"This line here" is splitting each line at the comma.
This means that "Hello,/n"
After those two splits will become: ("Hello" , null )
Note how there is nothing after the comma (the newline is taken off from the first line) and since the elements of the array were previously undefined (null) then the later line.
quote = dataPairs[Random.Range(0,lineNum++)][Random.Range(0,lineNum++)];
May actually throw a nullreference exception. However, since the second dimension of the array is also not set before being accessed by this line it doesn't have a chance to throw said exception but instead will complain about the array not even having an element to access there.
(It may be confusing how that is different from a nullreference exception, but believe me. . .there is a difference)
Well, that was a wall of text. I hope it's at least helpful. My suggestion for this code is that it needs some very serious refinement and some refactoring to be more in line with your needs, which need to be more defined for me to fully write a derived version.
Thank you very much for your time.
Your answer
Follow this Question
Related Questions
Resources.Load not giving me all data for a .txt file 1 Answer
How does a TextAsset work? 1 Answer
Reading Data generated in Processing from a TextAsset 0 Answers
Basic Question about TextAssets 0 Answers