user3803290
user3803290

Reputation:

Unity3D file I/O StreamWriter text file

When my script runs the start() function, which calls the saveLoadedChunk() function which calls the writeChunks() function, all I see in my text file is Chunk(0,0,0).

What needs to happen is that in the text file every chunk gets written with his coordinates (but only once). I wrote the following script, but it only writes (like I said earlier) Chunk(0,0,0).

Is there anyone who can adjust this code or give me some help so that the script writes all the chunks in the file, but only 1 time.

Help is greatly appreciated.

PART OF THE SCRIPT

function saveLoadedChunk() {
    var loadedChunks : GameObject[] = FindObjectsOfType(GameObject) as GameObject[];
    var fileName = "C:/Reactor Games/chunks.txt";
    for (var i = 0; i < loadedChunks.length ; i++) {
            if(loadedChunks[i].name.Substring(0,5) == "Chunk") {
                if(loadedChunks[i].tag == "Player") {
                }else{
                    if(System.IO.File.Exists(fileName)) {
                        Debug.Log("EXIST");
                        var xco1 = loadedChunks[i].transform.position.x;
                        var yco1 = loadedChunks[i].transform.position.y;
                        var zco1 = loadedChunks[i].transform.position.z;
                        writeChunks(fileName, xco1, yco1, zco1);
                    }else{
                        Debug.Log("NOTEXIST");
                        var sr = System.IO.File.CreateText(fileName);
                        var xco2 = loadedChunks[i].transform.position.x;
                        var yco2 = loadedChunks[i].transform.position.y;
                        var zco2 = loadedChunks[i].transform.position.z;
                        writeChunks(fileName, xco2, yco2, zco2);
                    }   
            }
        }
    }

}

function writeChunks(fileName : String, xco : int, yco : int, zco : int) {
    var stringToWrite = "Chunk (" + xco + ", " + yco + ", " + zco + ")";
    Debug.Log(stringToWrite);
    var sw : System.IO.StreamWriter = new System.IO.StreamWriter(fileName);
    sw.WriteLine(stringToWrite);
    sw.Flush();
    sw.Close();
}

Upvotes: 1

Views: 1216

Answers (1)

statueuphemism
statueuphemism

Reputation: 664

In the else branch, the output file is created and held it open so the first call to writeChunks cannot write because it cannot open the file:

}else{
  Debug.Log("NOTEXIST");
  var sr = System.IO.File.CreateText(fileName); // This line opens the file and 
                                                // is unnecessary because the
                                                // StreamWriter in writeChunks will 
                                                // create the file if it does not 
                                                // already exist. Remove this line.
                                                // This entire else branch actually
                                                // is redundant and may be removed.
  var xco2 = loadedChunks[i].transform.position.x;
  var yco2 = loadedChunks[i].transform.position.y;
  var zco2 = loadedChunks[i].transform.position.z;
  writeChunks(fileName, xco2, yco2, zco2);
}

That only affects the first write however, inside writeChunks, you overwrite the contents of the file every time rather than appending to it. The StreamWriter class optionally accepts a second boolean argument which allows you to open the file and append to it:

var sw : System.IO.StreamWriter = new System.IO.StreamWriter(fileName, true);

You may also wish to consider refactoring your code so you are not constantly opening and closing the file similar to the following:

function saveLoadedChunk() {
    var loadedChunks : GameObject[] = FindObjectsOfType(GameObject) as GameObject[];
    var fileName = "C:/Reactor Games/chunks.txt";
    var sw : System.IO.StreamWriter = new System.IO.StreamWriter(fileName);
    for (var i = 0; i < loadedChunks.length ; i++) {
        if(loadedChunks[i].name.Substring(0,5) == "Chunk") {
            if(loadedChunks[i].tag != "Player") {
                var xco1 = loadedChunks[i].transform.position.x;
                var yco1 = loadedChunks[i].transform.position.y;
                var zco1 = loadedChunks[i].transform.position.z;
                writeChunks(sw, xco1, yco1, zco1);
            }
        }
    }
    sw.Flush();
    sw.Close();
}

function writeChunks(sw : System.IO.StreamWriter, xco : int, yco : int, zco : int) {
    var stringToWrite = "Chunk (" + xco + ", " + yco + ", " + zco + ")";
    Debug.Log(stringToWrite);
    sw.WriteLine(stringToWrite);
}

Upvotes: 1

Related Questions