- Home /
Set a variable using ReadLine()
Hello!
I am working on a level editor and it is going really well. But now I need to save the level author and the game time in the saved file and read it. I can save the game time and author but when it comes to loading it back in, I can not get it to work. I save my file with "file.WriteLine(gameTime)" and "file.WriteLine(levelAuthor)".
If anyone of you know how I could load it back it and assign the gameTime and levelAuthor variables with this, please tell me!
Thanks in advance!
EDIT: I added the file that it generates a bit of my code
The file it generates. The names in front of the values are not really a part of the file. I added them so you can see what they are
Game Time 120
Level Author Hertzole
Object ID 0
Pos X 0
Pos Y 0
Pos Z 0
Rot X 0
Rot Y 0
Rot Z 0
Rot W 1
0
0
0
Color R 1
Color G 1
Color B 1
Lenght 1
Width 1
Height 1
Now this is the huge chunk of code that I am using to read the text
currentRead = "s";
//Loading objects.
while(currentRead != "TerrainBlock"&& currentRead != null){
currentRead = file.ReadLine();
//Number of lines each object uses, amount of writes in for loop up above ^.
if(i < 17){
//Keep reading down the lines
i ++;
}
//We've finished reading an object, time to create it.
else{
//Build a new object.
GameObject tempObject = Instantiate(levelEditorSettings.objectList[tempObjectId], tempPosition, tempRotation) as GameObject;
//Run the adjust object function in new object.
if(!tempObject.GetComponent<CubeBlock>().isLight) tempObject.GetComponent<CubeBlock>().AdjustObject(tempWidth,tempHeight,tempLength, tempRotation.eulerAngles.x,tempRotation.eulerAngles.y,tempRotation.eulerAngles.z);
//Set object id so the object can be saved again.
tempObject.GetComponent<CubeBlock>().objectId = tempObjectId;
//Reset the counter.
i = 1;
}
//Depending on which line of the object we are on, what we do.
switch(i){
case 1:
//1st line grabs object id.
if(currentRead != "TerrainBlock" && currentRead != null) tempObjectId = int.Parse(currentRead);
break;
case 2:
//2st line grabs x position.
tempPosition = new Vector3(float.Parse(currentRead),0,0);
break;
case 3:
//3st line grabs y position.
tempPosition = new Vector3(tempPosition.x,float.Parse(currentRead),0);
break;
case 4:
////4st line grabs z position.
tempPosition = new Vector3(tempPosition.x, tempPosition.y,float.Parse(currentRead));
break;
case 5:
//5st line grabs x rotation.
tempRotation = new Quaternion(float.Parse(currentRead),0,0,0);
break;
case 6:
//6st line grabs y rotation.
tempRotation = new Quaternion(tempRotation.x,float.Parse(currentRead),0,0);
break;
case 7:
//7st line grabs z rotation.
tempRotation = new Quaternion(tempRotation.x,tempRotation.y,float.Parse(currentRead),0);
break;
case 8:
//8st line grabs w rotation.
tempRotation = new Quaternion(tempRotation.x,tempRotation.y,tempRotation.z,float.Parse(currentRead));
break;
case 9:
//9st line grabs material id.
break;
case 10:
//10st line grabs painted bool.
break;
case 11:
//Grabs paint age or spot angle.
tempSpotAngle = float.Parse(currentRead);
break;
case 12:
//Grabs color R.
tempColor.r = float.Parse(currentRead);
break;
case 13:
//Grabs color G.
tempColor.g = float.Parse(currentRead);
break;
case 14:
//Grabs color B.
tempColor.b = float.Parse(currentRead);
break;
case 15:
//Grabs Length.
tempLength = float.Parse(currentRead);
tempLightType = int.Parse(currentRead);
break;
case 16:
//Grabs Width.
tempWidth = float.Parse(currentRead);
tempRange = float.Parse(currentRead);
break;
case 17:
//Grabs Height.
tempHeight = float.Parse(currentRead);
tempIntensity = float.Parse(currentRead);
break;
}
}
// close the file
file.Close();
@robertbu I have added the file it generates and the code I am using to read it.
I strongly recommend you look into serializing your object either to binary or to X$$anonymous$$L, and then restore it on load using deserialization. Your current method is error prone and the switch statement you're using to restore the data looks like it might grow so big with time that it becomes difficult to maintain it.
Your answer
Follow this Question
Related Questions
Line Reading 1 Answer
Unity iPhone - Using the file system in my app/game? 1 Answer
Saving My World 1 Answer
System.IO documentation? 2 Answers
XmlException when trying to read/write XML from iOS device 1 Answer