- Home /
Problem with text from a .txt file.
So lately I have been trying to create a script to read .txt files and split it into chunks for NPCs and dialogue. Except, I have run into a problem, when I try to see if something from the text file is equal to a piece of text in the script, they never are, when even they should be. Are there hidden spaces or something when a .txt file is turned into a string? Here is an example of what I'm am trying to say.
var textFile : TextAsset;
var DialogLines : String[];
function Start(){
if(textFile){
DialogLines = (textFile.text.Split("\n"[0]));
}
}
function Update(){
if(DialogLines[0] == "TheTextToFind"){
print("Found it");
}
}
In this case, the first column in the text file would be "TheTextToFind", and yet it never works.It seems like it should be simple enough, but it doesn't work at all, and I can never locate anything/find anything. (I am sure that there is not mistake in the text file/word being used to search, I have tried searching for many things, coming up with nothing)
Any help is greatly appreciated!
Split("\n"[0]);
Depending how how the text file is formatted, sometime a newline is TWO characters (line-feed & carriage-return). When you say [0], this will specify only one character. But it looks like Split(), can actually split based on a string (as opposed to a single char), so I suggest you simply remove the [0].
I would also suggest you use Debug.Log(DialogLine[0]); immediately after you call the split function, to confirm the text is indeed splitting as expected.
I have spent lots of time trying to figure this out, and put many Debug.Logs. Nothing seems to be wrong.
When I try removing the [0], an error pops up: Assets/Common/WorkingAssets_Network/Controllers/test.js(12,35): BCE0023: No appropriate version of 'String.Split' for the argument list '(String)' was found.
Thanks for your reply!
I would suggest using a for loop ex;
for (var i : String in DialogLines)
{
//code
}
should work?
$$anonymous$$y main script already has something that does that, but there is no point in posting that because if I can't even get something as simple as the above to work, I know my main script won't work. That is the only part that doesn't work, when it should.
I don't know why everyone want you to loop through your string. Oh I see now, it's not the string they want you to loop though, they want you to loop through all the DialogLines (so as to check more than just the first dialogline). I'd like to take it one step at a time, before trying to find text in the string (for which I would use the string.Contains() function- as Todd suggested), lets try to figure out why the Split function doesn't work for you.
If it's not finding the split character then the ENTIRE string should end up in DialogLines[0]. What does you debug show for this array element? completely empty? What does size of the DialogLines array show in debug.log, after calling split?
going yet another step backwards, can you confirm that textfile.text variable actually has the text you want in there (also with debug log)? Perhaps you need to specifically load the asset before the textfile.text variable is filled with a string?
Answer by toddisarockstar · Mar 10, 2015 at 04:28 AM
memorymod is right. Where is the loop to search your array????
for(var n = 0; n < DialogLines.Length; n++){
if(DialogLines[n].Contains("TheTextToFind") {yay=DialogLines[n];}
}}
are u trying to get first column if so (not sure if [0] is in right place), not tried this but worth a look
DialogLines = (textFile.text.Split("\n"[0]));
try
DialogLines = (textFile.text.Split("\n")[0]);
Your answer

Follow this Question
Related Questions
Quest Script Help 2 Answers
How to make GUI Text appear after a certain amount of time 2 Answers
Text wont change to correct value 1 Answer
Detect Text in GUI; Print 1 Answer