Jordi
Jordi

Reputation: 166

Unity 3D : Compare array in for loop

I have a for loop wich gets all the "chunks" in my scene and gets their X,Y and Z coordinates, then it writes these coordinates plus it's name to a text file. It does this for all chunks. This for loop is inside a function called saveLoadedChunk. This function gets called every minute to save the chunks. But the problem is that when the player is standing in the same area as 10 minutes ago, it saves the same chunks and results into a textfile with all the chunks, but multiple times. I don't want this to happen so wrote a little block of code that puts all the chunks of the text file in an array and compares those with the chunk it is going to save. And if this is so, it won't save the chunk again. But it is not working, after adding some Debug.Log(); functions, I found out that the if statement at line 20, always takes the else branch. I have no clue why and appreciate all help.

Thank you very much,

THE CODE:

function saveLoadedChunk() {
    var loadedChunks : GameObject[] = FindObjectsOfType(GameObject) as GameObject[];
    var fileName = "C:/Reactor Games/chunks.txt";    

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

    var dontWrite = false;

    var sw : System.IO.StreamWriter = new System.IO.StreamWriter(fileName, true);
    for (var i = 0; i < loadedChunks.length ; i++) {
        if(loadedChunks[i].name.Substring(0,5) == "Chunk" || loadedChunks[i].name.Substring(0,5) == "_TERR") {
            if(loadedChunks[i].tag != "Player") {
                var xco = loadedChunks[i].transform.position.x;
                var yco = loadedChunks[i].transform.position.y;
                var zco = loadedChunks[i].transform.position.z;
                var stringToWrite = "Chunk (" + xco + ", " + yco + ", " + zco + ")";

                for(var chunkName in lines) {
                    if(chunkName.Equals(stringToWrite)) {
                        Debug.LogError("dontwrite = true");
                        dontWrite = true;
                    }
                    else{
                        dontWrite = false;
                    }
                }

                if(!dontWrite){sw.WriteLine(stringToWrite);}        
            }
        }
    }
    sw.Flush();
    sw.Close();
}

EXAMPLE OF TEXT FILE (HOW IT SHOULD BE, NO CHUNKS ARE WRITTEN TWICE):

Chunk (100, 0, -100)
Chunk (100, 0, -50)
Chunk (100, 0, 0)
Chunk (100, 0, 50)
Chunk (100, 0, 100)
Chunk (50, 0, -100)
Chunk (50, 0, 100)
Chunk (0, 0, -100)
Chunk (0, 0, 100)
Chunk (-50, 0, -100)
Chunk (-50, 0, 100)
Chunk (-100, 0, -100)
Chunk (-100, 0, -50)
Chunk (-100, 0, 0)
Chunk (-100, 0, 50)
Chunk (-100, 0, 100)
Chunk (0, 0, -50)
Chunk (0, 0, 50)
Chunk (-50, 0, -50)
Chunk (-50, 0, 0)
Chunk (-50, 0, 50)
Chunk (50, 0, -50)
Chunk (50, 0, 0)
Chunk (50, 0, 50)
Chunk (0, 0, 0)

EXAMPLE OF THE TEXT FILE WHERE IT WENT WRONG: As you can see in the textfile below, you can see that it writes the same chunks over and over again, every time that the saveLoadedChunk(); function gets called.

Chunk (0, 0, -100)
Chunk (0, 0, 100)
Chunk (-50, 0, -100)
Chunk (-50, 0, 100)
Chunk (-100, 0, -100)
Chunk (-100, 0, -50)
Chunk (-100, 0, 0)
Chunk (-100, 0, 50)
Chunk (-100, 0, 100)
Chunk (0, 0, -50)
Chunk (0, 0, 50)
Chunk (-50, 0, -50)
Chunk (-50, 0, 0)
Chunk (-50, 0, 50)
Chunk (50, 0, -50)
Chunk (50, 0, 0)
Chunk (50, 0, 50)
Chunk (0, 0, 0)
Chunk (100, 0, -100)
Chunk (100, 0, -50)
Chunk (100, 0, 0)
Chunk (100, 0, 50)
Chunk (100, 0, 100)
Chunk (50, 0, -100)
Chunk (50, 0, 100)
Chunk (0, 0, -100)
Chunk (0, 0, 100)
Chunk (-50, 0, -100)
Chunk (-50, 0, 100)
Chunk (-100, 0, -100)
Chunk (-100, 0, -50)
Chunk (-100, 0, 0)
Chunk (-100, 0, 50)
Chunk (-100, 0, 100)
Chunk (0, 0, -50)
Chunk (0, 0, 50)
Chunk (-50, 0, -50)
Chunk (-50, 0, 0)
Chunk (-50, 0, 50)
Chunk (50, 0, -50)
Chunk (50, 0, 0)
Chunk (50, 0, 50)
Chunk (0, 0, 0)
Chunk (100, 0, -100)
Chunk (100, 0, -50)
Chunk (100, 0, 0)
Chunk (100, 0, 50)
Chunk (100, 0, 100)
Chunk (50, 0, -100)
Chunk (50, 0, 100)
Chunk (0, 0, -100)
Chunk (0, 0, 100)
Chunk (-50, 0, -100)
Chunk (-50, 0, 100)
Chunk (-100, 0, -100)
Chunk (-100, 0, -50)
Chunk (-100, 0, 0)
Chunk (-100, 0, 50)
Chunk (-100, 0, 100)
Chunk (0, 0, -50)
Chunk (0, 0, 50)
Chunk (-50, 0, -50)
Chunk (-50, 0, 0)
Chunk (-50, 0, 50)
Chunk (50, 0, -50)
Chunk (50, 0, 0)
Chunk (50, 0, 50)
Chunk (0, 0, 0)
Chunk (100, 0, -100)
Chunk (100, 0, -50)
Chunk (100, 0, 0)
Chunk (100, 0, 50)
Chunk (100, 0, 100)
Chunk (50, 0, -100)
Chunk (50, 0, 100)
Chunk (0, 0, -100)
Chunk (0, 0, 100)
Chunk (-50, 0, -100)
Chunk (-50, 0, 100)
Chunk (-100, 0, -100)
Chunk (-100, 0, -50)
Chunk (-100, 0, 0)
Chunk (-100, 0, 50)
Chunk (-100, 0, 100)
Chunk (0, 0, -50)
Chunk (0, 0, 50)
Chunk (-50, 0, -50)
Chunk (-50, 0, 0)
Chunk (-50, 0, 50)
Chunk (50, 0, -50)
Chunk (50, 0, 0)
Chunk (50, 0, 50)
Chunk (0, 0, 0)
Chunk (100, 0, -100)
Chunk (100, 0, -50)
Chunk (100, 0, 0)
Chunk (100, 0, 50)
Chunk (100, 0, 100)
Chunk (50, 0, -100)
Chunk (50, 0, 100)
Chunk (0, 0, -100)
Chunk (0, 0, 100)
Chunk (-50, 0, -100)
Chunk (-50, 0, 100)
Chunk (-100, 0, -100)
Chunk (-100, 0, -50)
Chunk (-100, 0, 0)
Chunk (-100, 0, 50)
Chunk (-100, 0, 100)
Chunk (0, 0, -50)
Chunk (0, 0, 50)
Chunk (-50, 0, -50)
Chunk (-50, 0, 0)
Chunk (-50, 0, 50)
Chunk (50, 0, -50)
Chunk (50, 0, 0)
Chunk (50, 0, 50)
Chunk (0, 0, 0)

UPDATE 1: I changed my code to the following thanks to Robert. But it still doesn't debug dontwrite for some reason... Help is greatly appreciated and I will accept the answer when it works + I give an upvote to every answer that helps me.

    function saveLoadedChunk() {
    var loadedChunks : GameObject[] = FindObjectsOfType(GameObject) as GameObject[];
    var fileName = "C:/Reactor Games/chunks.txt";    

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

    var write = true;

    var sw : System.IO.StreamWriter = new System.IO.StreamWriter(fileName, true);
    for (var i = 0; i < loadedChunks.length ; i++) {
        if(loadedChunks[i].name.Substring(0,5) == "Chunk" || loadedChunks[i].name.Substring(0,5) == "_TERR" || loadedChunks[i].name.Substring(0,5) == "_ACTI") {
            if(loadedChunks[i].tag != "Player") {
                var xco = loadedChunks[i].transform.position.x;
                var yco = loadedChunks[i].transform.position.y;
                var zco = loadedChunks[i].transform.position.z;
                var stringToWrite = "Chunk (" + xco + ", " + yco + ", " + zco + ")";

                write = true;
                for(var chunkName in lines) {
                    if(chunkName.Equals(stringToWrite)) {
                        Debug.LogError("dontwrite = true");
                        write = false;
                        break;
                    }
                }

                if(write){sw.WriteLine(stringToWrite);}     
            }
        }
    }
    sw.Flush();
    sw.Close();
}

Upvotes: 0

Views: 920

Answers (1)

Robert Noack
Robert Noack

Reputation: 1306

It's because your for loop does not escape when it finds a match. As long as the LAST object in lines does not equal stringToWrite, it will write. Change the code to the following:

dontWrite=false;
for(var chunkName in lines) {
    if(chunkName.Equals(stringToWrite)) {
        Debug.LogError("dontwrite = true");
        dontWrite = true;
        break;
    }
}

Upvotes: 2

Related Questions