- Home /
Issues with string array taken from a .txt file
Im currently trying to code a save state for my project. So far Ive managed to get the file written, read to an array, then set to variables in another script. However, although the string variable for "Owner" appears correctly in the inspector (It should be "AAA", which it is). Im not too sure why this is. Debug.Log shows the string as "AAA", but Owner.Length returns 4 characters, which is also odd.
Here is the code for the read/write script.
import System.IO;
//FileWritingVariables
var savedirectory : String = "assets/savedgames/";
var savename : String = "SaveGame1.txt";
var LoadingSave : boolean = true;
//LoadingVariables
var savefile : TextAsset;
var PlanetSaveData = new Array();
//PlanetVariables
//LargePlanets
var LP01 : GameObject;
var LP01Pop = 0;
var LP01Name : String = "LP1";
var LP01Owner : String;
var LP02 : GameObject;
var LP02Pop = 0;
var LP02Name : String = "LP2";
var LP02Owner : String;
var LP03 : GameObject;
var LP03Pop = 0;
var LP03Name : String = "LP3";
var LP03Owner : String;
//SmallPlanets
//Regions
function Start () {
Debug.Log("Save Key is bound to F12");
//Loading Save File to String Array//
if (LoadingSave == true){
var linearray : String[] = savefile.text.Split("\n"[0]);
for (var thisLine : String in linearray){
PlanetSaveData.Push(thisLine);
}
}
}
function Update () {
if (Input.GetKeyDown(KeyCode.F12)){
//Defining Planets//
LP01 = GameObject.Find("LP01");
LP01Pop = LP01.GetComponent(PlanetData).Population;
LP01Name = LP01.GetComponent(PlanetData).Name;
LP01Owner = LP01.GetComponent(PlanetData).Owner;
LP02 = GameObject.Find("LP02");
LP02Pop = LP02.GetComponent(PlanetData).Population;
LP02Name = LP02.GetComponent(PlanetData).Name;
LP02Owner = LP02.GetComponent(PlanetData).Owner;
LP03 = GameObject.Find("LP03");
LP03Pop = LP03.GetComponent(PlanetData).Population;
LP03Name = LP03.GetComponent(PlanetData).Name;
LP03Owner = LP03.GetComponent(PlanetData).Owner;
//Start Writing//
sw = new StreamWriter(savedirectory + savename);
sw.WriteLine(LP01Name);
sw.WriteLine(LP01Pop);
sw.WriteLine(LP01Owner);
sw.WriteLine(LP02Name);
sw.WriteLine(LP02Pop);
sw.WriteLine(LP02Owner);
sw.WriteLine(LP03Name);
sw.WriteLine(LP03Pop);
sw.WriteLine(LP03Owner);
sw.Close();
Debug.Log("GameData Saved");
}
}
This is the code that references it and puts it into variables:
import System.IO;
var Name : String = "MajPlanet1";
var Population = 10000;
var Owner : String;
var PlanetTag : String;
var StatsHolder : GameObject;
var OwnerFloat = 0;
var UpdateOwner : boolean = false;
var SaveArray;
function Awake(){
StatsHolder = GameObject.Find("StatsHolder");
}
function Start(){
//Loading From SaveData//
var LoadingSave : boolean = StatsHolder.GetComponent(WriteScript).LoadingSave;
if (LoadingSave == true){
yield WaitForSeconds (1); //Need to wait for array to form in writescript
SaveArray = StatsHolder.GetComponent(WriteScript).PlanetSaveData;
//Setting Planet Data//
if (PlanetTag == "LP01"){
var LP01Pop : String = SaveArray[1];
Name = SaveArray[0];
Population = System.Int32.Parse(LP01Pop);
Owner = SaveArray[2];
}
if (PlanetTag == "LP02"){
var LP02Pop : String = SaveArray[4];
Name = SaveArray[3];
Population = System.Int32.Parse(LP02Pop);
Owner = SaveArray[5];
}
if (PlanetTag == "LP03"){
var LP03Pop : String = SaveArray[7];
Name = SaveArray[6];
Population = System.Int32.Parse(LP03Pop);
Owner = SaveArray[8];
}
//Done Setting Planet Data//
}
Debug.Log (Owner.Length);
}
function Update(){
if (Owner == "AAA"){
OwnerFloat = 1;
}
if (Owner == "BBB"){
OwnerFloat = 2;
}
if (Owner == "CCC"){
OwnerFloat = 3;
}
else{
OwnerFloat = 5;
}
}
And this is how the text file Im reading from currently looks
NotEuthalia
2000000
AAA
Aleria
43563
BBB
AlmostAssuredlyNotSilaari
124
CCC
SomeString
PS, I know my code is probably insanely messy, fairly new to all of this.
In your text file, are there spaces after the text? If the text was "AAA " you'd get a length of 4.
Nope, no spaces. Copied the text up there directly from the file
Answer by pazzesco · May 15, 2013 at 08:30 PM
Very possibly line endings. Try changing your code from this...
Owner = SaveArray[2];
To this...
Owner = SaveArray[2].Trim();
You'll want to change that for all instances. Might eliminate that extra white space, if that's the issue.
Either way I would investigate to find out exactly what character is causing it rather than just stripping out, if that is indeed the problem.Good idea pazzesco :)
I agree. Definitely look into why those line endings are showing up that way, if that's the case. Understanding a bug or problem fully is always a good practice. You don't want to have a program "work" by coincidence. And thank you, UFO Hunter.
Ah, that was it. Opened it up in Notepad++ and it had line ending characters. Thanks, and thanks to UFO Hunter too.
Answer by code-blep · May 15, 2013 at 08:24 PM
How about hidden characters like line endings? I use Notepad++ to expose stuff like that.
Your answer
Follow this Question
Related Questions
PlayerPrefs script problem 2 Answers
save string from array to player prefs 1 Answer
Get the order of scenes from a text file 0 Answers
Why can't I save an array? 1 Answer
Unity c# StreamReader to list always returns false. 1 Answer