Parsing multiple lines from text file.

What it does:
The script below reads the first line from the specified text file and instantiates a prefab based on information in said file.

My issue:
I’m not sure how to make it so that it continues parsing the rest of the lines, spawning multiple objects accordingly.

Additionally:

  • Obviously, the variable savedObj will have to change during each pass to correspond to the information given. I’m currently getting errors thrown saying the variable is already specified. Not sure how to set up the variable so it’s editable based on the condition.
  • I’m having issues not only setting the name of the object being instantiated, but allowing it to increment as well.

Any nudge in the right direction would be more than appreciated. Thank you.

#pragma strict
#pragma implicit
#pragma downcast

import System.IO;

// Spawnable Object Vars
var Cube:GameObject;
var Circle:GameObject;

// Data Vars
var dataFile:TextAsset;
var sr = new StreamReader("Assets/ObjectEditor.txt");
var line = sr.ReadLine();
var separator = ",";

function Start(){

	//for (line in linesOfFile){

		var dataArray = line.Split(separator[0]);
			
		var posX = (parseFloat(dataArray[1]));
		var posZ = (parseFloat(dataArray[2]));
		var objRotationX = (parseFloat(dataArray[3]));
		var objRotationY = (parseFloat(dataArray[4]));
		var objRotationZ = (parseFloat(dataArray[5]));
			
		if ((dataArray[0] == "Cube")) {
			var savedObj = Cube;
		}
		//if ((dataArray[0] == "Circle")) {
		//	var savedObj = Circle;
		//}
		
		// Spawn Object
		var obj = Instantiate(savedObj, Vector3(posX, 0, posZ), Quaternion.Euler(objRotationX, objRotationY, objRotationZ));
		//var objCount++;
		//var obj.name = ("Object" + objCount);
		var objName = obj.name;
	//}
}

Well since your reading in multiple lines your going to have to use a for loop or while loop… Otherwise your only going to read the initial first position/line.
You can easily do this with the EoF(End of File) commands… Just google them you will find what your looking for, there are different types of EoF’s depending on which reader your using and how your reading( line, character, etc)

Next thing. Your variable saveObj cannot be used outside of that if statement… Simple fix there. at the start of your for loop or while loop just put var savedObj = null;

while(myFile != EOF) //Whatever that might be
{
    var savedObj = null;
    //read the line/char/whatever and do what you were doing. 

     if ((dataArray[0] == "Cube"))  {
          savedObj = Cube; 
     }
}

Instead of all that, just use ReadAllText.

var fileText = File.ReadAllText(filePath);
var lines = fileText.Split("\n"[0]);

–Eric

Thanks a lot for the help. But I’m not sure sure how to use EOF or ReadAllText commands in JS, I’m assuming there’s an import for it? I have System.IO imported but it’s giving me member errors for both.

If I could get this to work:

var fileText = File.ReadAllText(filePath);
var lines = fileText.Split("\n"[0]);

Looks like it would solve most of my issues. From the looks of it, the script splits after a line break and stores the entire line in an array. From there, I can probably use another split to separate that line via commas. The logic is sound, just need to get my file to read those commands.

The code I posted was JS. Using “import System.IO;” is correct; if that doesn’t work, I guess you’re using an old version of Unity?

–Eric

That was the issue. I’m using 3.5 BUT the project was made in an earlier version. Script works fine now. Thanks a ton. I’ll just move my stuff over to a fresh project.

Thanks again.

If you already got it all figured out you can disregard this… I however went and made sure if worked correctly. I used eric’s method.

#pragma strict
#pragma implicit
#pragma downcast

import System;
import System.IO;
import System.Text;

// Spawnable Object Vars
var Cube:GameObject;
var Circle:GameObject;

// Data Vars
var dataFile:TextAsset;
//var sr = new StreamReader("Assets/ObjectEditor.txt");
//var line = sr.ReadLine();
var separator1 = "\n";
var separator2 = ",";
var dataArray: String[];
var line: String[];

function Start(){

    //while (dataFile != null){

       var fileText = File.ReadAllText("Assets/ObjectEditor.txt");
       line = fileText.Split("\n"[0]);
        var savedObj:GameObject = null;

        for(i = 0; i < (line.Length); i++)
        {
       	 dataArray = line[i].Split(","[0]);
	        // Temp Check
	        print(dataArray[0]);
	        var posX = (parseFloat(dataArray[1]));
	        var posZ = (parseFloat(dataArray[2]));
	        var objRotationX = (parseFloat(dataArray[3]));
	        var objRotationY = (parseFloat(dataArray[4]));
	        var objRotationZ = (parseFloat(dataArray[5]));
	           
	        if ((dataArray[0] == "Cube")) {
	            savedObj = Cube;
	        }

	        //if ((dataArray[0] == "Circle")) {
	        //  var savedObj = Circle;
	        //}       
	        // Spawn Object
	        var obj = Instantiate(savedObj, Vector3(posX, 0, posZ), Quaternion.Euler(objRotationX, objRotationY, objRotationZ));

	        //var objCount++;
	        //var obj.name = ("Object" + objCount);
	        var objName = obj.name;
	      }
    //}
}

Here’s a picture of it working :

Thanks a lot guys, here’s the working script if someone else would like it.

Credits:

  • Eiznek
  • Eric5h5
  • Xerosigma
#pragma strict
#pragma implicit
#pragma downcast

import System;
import System.IO;
import System.Text;

// Spawnable Object Vars
var Cube:GameObject;
var Circle:GameObject;

// Data Vars
var dataFile:TextAsset;
//var sr = new StreamReader("Assets/ObjectEditor.txt");
//var line = sr.ReadLine();
var separator1 = "\n";
var separator2 = ",";
var dataArray: String[];
var line: String[];

function Start(){

	var fileText = File.ReadAllText("Assets/ObjectEditor.txt");
	line = fileText.Split("\n"[0]);
	var savedObj:GameObject = null;
	var objCount = 0;
	
	for(i = 0; i < (line.Length); i++) {

		dataArray = line[i].Split(","[0]);
		
		var posX = (parseFloat(dataArray[1]));
		var posY = (parseFloat(dataArray[2]));
		var posZ = (parseFloat(dataArray[3]));
		var objRotationX = (parseFloat(dataArray[4]));
		var objRotationY = (parseFloat(dataArray[5]));
		var objRotationZ = (parseFloat(dataArray[6]));

		if ((dataArray[0] == "Cube")) {
			savedObj = Cube;
		}

		if ((dataArray[0] == "Circle")) {
			savedObj = Circle;
		} 
			      
		// Spawn Object
		var obj = Instantiate(savedObj, Vector3(posX, posY, posZ), Quaternion.Euler(objRotationX, objRotationY, objRotationZ));
		objCount++;
		obj.name = ("Object" + objCount + "");
	}
}

Thank u, this script helped me on my project ! :slight_smile:

this should work, but i dunno js all that well…

ps> better syntax, in c# at least, would be:

string [] lines = fileText.Split(new char{'\n'}); //single ' = char, no need to index

I assume js is something like:

var lines = fileText.Split('\n');

It’s not; JS uses both single and double quotes for strings, and string indices for chars. fileText.Split(“\n”[0]) is correct. Also that “new char{}” code wouldn’t compile in C#; just fileText.Split(‘\n’) is fine.

By the way, the issue had already been solved.

–Eric