- Home /
Load function not working propperly?
Here is my load function
function load1(){
var sr = new File.OpenText("Saves/planets.txt");
var fc = sr.ReadToEnd();
sr.Close();
var planets = fc.Split("---"[0]);
for(pla in planets){
var lines = fc.Split("\n"[0]);
for(line in lines){
Debug.Log(lines[1]);
x1 = parseInt(parseFloat (lines[1]));
x3 = parseInt (parseFloat(lines[3]));
}
planet = Instantiate (ast, Vector3(x1, x2, x3), Quaternion.identity );
}
}
The problem that I'm facing is that will only load one planet. When there will be multiple one's in the file. Each one is split by "---" and it's suppose to be splitting those in the load function. What am I doing wrong here?
Answer by whydoidoit · Dec 15, 2012 at 06:35 AM
You can't split on "---" you are actually splitting on "-" with that line of code because the [0] just takes the first element of the string.
The easiest thing is probably to convert the "---" to a single character if there is one that can't be a part of the input - so something like:
var fc = sr.ReadToEnd().Replace("---","`");
var planets = fc.Split("`"[0]);
It's still only spawning one. When there are at least 5 saved in the file
Right your other problem is that you are still getting the lines from fc not from planets - you need to make it:
var lines = pla.Split("\n"[0])
IndexOutOfRangeException: Array index is out of range. saving.load1 () (at Assets/saving.js:114) saving.Update () (at Assets/saving.js:21)
114: x1 = parseInt(parseFloat (lines[1]));
Well I'm guessing your file format has an instance where there are less than 2 lines in a section (or at least what it has managed to parse). I'm also not sure what the for next loop is doing over the lines collection as you appear to ignore it and just take a value out of the array irrespective of the line you are on.
Perhaps if you could post a couple of entries from your file it might be easier to spot.
Here are two entries from the file -
1610.388
1241.892
Laose429
100
RGBA(1.000, 0.902, 0.000, 1.000)
0
0
1
0
0
null
5524
133
133
244
244
0
746
0
null
1
1
0
476
sun (UnityEngine.Transform)
9
0
131
0
601
22
5
10
(0.0, 0.0, 0.0)
0
(1610.6, -476.0, 1241.6)
null
134
610
394
6
1
(0.0, -0.3, 0.0, 1.0)
574.2449
1201.469
Altar295
100
RGBA(1.000, 0.483, 0.000, 1.000)
0
0
1
0
0
null
5304
132
132
254
254
0
613
0
null
3
1
0
476
sun (UnityEngine.Transform)
6
0
128
0
472
18
5
10
(0.0, 0.0, 0.0)
0
(574.0, -476.0, 1201.3)
null
133
485
196
7
2
(0.0, 0.4, 0.0, 0.9)
Your answer
Follow this Question
Related Questions
Loading from multiple files? 2 Answers
Saving items to a text file 1 Answer
Spawning random terrain pieces 1 Answer
Android File-io? 1 Answer
Android Multitouch 0 Answers