团结3D:在for循环比较数组 [英] Unity 3D : Compare array in for loop

查看:127
本文介绍了团结3D:在for循环比较数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个for循环至极​​得到我的场景中所有的豆腐块,并得到他们的X,Y,Z坐标,然后将其写入这些坐标再加上它的名字到一个文本文件中。它会为所有的块。这个循环是一个叫saveLoadedChunk函数内。这个函数被调用每分钟保存块。但问题是,当玩家站在同一区域10分钟前,将保存相同的块和导致成与所有的组块,但多次文本文件。我不希望这种情况发生,所以写了code稍微块放入数组中的文本文件的所有数据块,并比较那些有大块它会保存。而如果是这样的话,就不会再次保存该块。但它不工作,添加一些的debug.log()之后;功能,我发现,如果将在第20行声明中,始终把else分支。我不知道为什么和AP preciate所有帮助。

非常感谢你,

THE code:

 函数saveLoadedChunk(){
    VAR loadedChunks:游戏对象[] = FindObjectsOfType(游戏对象)作为游戏对象[];
    VAR文件名=C:/堆游戏/ chunks.txt;

    VAR FILETEXT = System.IO.File.ReadAllText(文件名);
    VAR线= fileText.Split(\ N[0]);

    VAR dontWrite = FALSE;

    VAR南方周末:System.IO.StreamWriter =新System.IO.StreamWriter(文件名,真实);
    对于(VAR I = 0; I< loadedChunks.length;我++){
        如果(loadedChunks [I] .name.Substring(0,5)==块|| loadedChunks [I] .name.Substring(0,5)==_TERR){
            如果(loadedChunks [I] .TAG!=玩家){
                VAR XCO = loadedChunks [I] .transform.position.x;
                VAR YCO = loadedChunks [I] .transform.position.y;
                VAR ZCO = loadedChunks [I] .transform.position.z;
                变种stringToWrite =组块(+ XCO +,+ YCO +,+ ZCO +);

                为(以线VAR chunkName){
                    如果(chunkName.Equals(stringToWrite)){
                        Debug.LogError(dontwrite =真正的);
                        dontWrite =真;
                    }
                    其他{
                        dontWrite = FALSE;
                    }
                }

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

文本文件的示例(如何它应该是,NO块是两次书面的):

 块(100,0,-100)
块(100,0,-50)
块(100,0,0)
块(100,0,50)
块(100,0,100)
块(50,0,-100)
块(50,0,100)
块(0,0,-100)
块(0,0,100)
块(-50,0,-100)
块(-50,0,100)
块(-100,0,-100)
块(-100,0,-50)
块(-100,0,0)
块(-100,0,50)
块(-100,0,100)
块(0,0,-50)
块(0,0,50)
块(-50,0,-50)
块(-50,0,0)
块(-50,0,50)
块(50,0,-50)
块(50,0,0)
块(50,0,50)
块(0,0,0)
 

示例文本文件,它去错了: 正如你可以在下面的文本文件看,你可以看到它一遍又一遍地写相同的数据块,每一次的saveLoadedChunk();函数被调用。

 块(0,0,-100)
块(0,0,100)
块(-50,0,-100)
块(-50,0,100)
块(-100,0,-100)
块(-100,0,-50)
块(-100,0,0)
块(-100,0,50)
块(-100,0,100)
块(0,0,-50)
块(0,0,50)
块(-50,0,-50)
块(-50,0,0)
块(-50,0,50)
块(50,0,-50)
块(50,0,0)
块(50,0,50)
块(0,0,0)
块(100,0,-100)
块(100,0,-50)
块(100,0,0)
块(100,0,50)
块(100,0,100)
块(50,0,-100)
块(50,0,100)
块(0,0,-100)
块(0,0,100)
块(-50,0,-100)
块(-50,0,100)
块(-100,0,-100)
块(-100,0,-50)
块(-100,0,0)
块(-100,0,50)
块(-100,0,100)
块(0,0,-50)
块(0,0,50)
块(-50,0,-50)
块(-50,0,0)
块(-50,0,50)
块(50,0,-50)
块(50,0,0)
块(50,0,50)
块(0,0,0)
块(100,0,-100)
块(100,0,-50)
块(100,0,0)
块(100,0,50)
块(100,0,100)
块(50,0,-100)
块(50,0,100)
块(0,0,-100)
块(0,0,100)
块(-50,0,-100)
块(-50,0,100)
块(-100,0,-100)
块(-100,0,-50)
块(-100,0,0)
块(-100,0,50)
块(-100,0,100)
块(0,0,-50)
块(0,0,50)
块(-50,0,-50)
块(-50,0,0)
块(-50,0,50)
块(50,0,-50)
块(50,0,0)
块(50,0,50)
块(0,0,0)
块(100,0,-100)
块(100,0,-50)
块(100,0,0)
块(100,0,50)
块(100,0,100)
块(50,0,-100)
块(50,0,100)
块(0,0,-100)
块(0,0,100)
块(-50,0,-100)
块(-50,0,100)
块(-100,0,-100)
块(-100,0,-50)
块(-100,0,0)
块(-100,0,50)
块(-100,0,100)
块(0,0,-50)
块(0,0,50)
块(-50,0,-50)
块(-50,0,0)
块(-50,0,50)
块(50,0,-50)
块(50,0,0)
块(50,0,50)
块(0,0,0)
块(100,0,-100)
块(100,0,-50)
块(100,0,0)
块(100,0,50)
块(100,0,100)
块(50,0,-100)
块(50,0,100)
块(0,0,-100)
块(0,0,100)
块(-50,0,-100)
块(-50,0,100)
块(-100,0,-100)
块(-100,0,-50)
块(-100,0,0)
块(-100,0,50)
块(-100,0,100)
块(0,0,-50)
块(0,0,50)
块(-50,0,-50)
块(-50,0,0)
块(-50,0,50)
块(50,0,-50)
块(50,0,0)
块(50,0,50)
块(0,0,0)
 

更新1: 我改变了我的code以下的感谢罗伯特。但它仍然没有调试dontwrite出于某种原因...帮助是极大的AP preciated,我会接受的答案时,它的工作原理+我举一个upvote每一个答案,可以帮助我。

 函数saveLoadedChunk(){
    VAR loadedChunks:游戏对象[] = FindObjectsOfType(游戏对象)作为游戏对象[];
    VAR文件名=C:/堆游戏/ chunks.txt;

    VAR FILETEXT = System.IO.File.ReadAllText(文件名);
    VAR线= fileText.Split(\ N[0]);

    VAR写= TRUE;

    VAR南方周末:System.IO.StreamWriter =新System.IO.StreamWriter(文件名,真实);
    对于(VAR I = 0; I< loadedChunks.length;我++){
        如果(loadedChunks [I] .name.Substring(0,5)==块|| loadedChunks [I] .name.Substring(0,5)==_TERR|| loadedChunks [I] .name.Substring (0,5)==_ACTI){
            如果(loadedChunks [I] .TAG!=玩家){
                VAR XCO = loadedChunks [I] .transform.position.x;
                VAR YCO = loadedChunks [I] .transform.position.y;
                VAR ZCO = loadedChunks [I] .transform.position.z;
                变种stringToWrite =组块(+ XCO +,+ YCO +,+ ZCO +);

                写= TRUE;
                为(以线VAR chunkName){
                    如果(chunkName.Equals(stringToWrite)){
                        Debug.LogError(dontwrite =真正的);
                        写= FALSE;
                        打破;
                    }
                }

                如果(写){sw.WriteLine(stringToWrite);}
            }
        }
    }
    sw.Flush();
    sw.Close();
}
 

解决方案

这是因为你的循环,当它找到一个匹配也没有放过。只要行的最后一个对象不等于stringToWrite,它会写。更改code以下内容:

  dontWrite = FALSE;
为(以线VAR chunkName){
    如果(chunkName.Equals(stringToWrite)){
        Debug.LogError(dontwrite =真正的);
        dontWrite =真;
        打破;
    }
}
 

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();
}

解决方案

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;
    }
}

这篇关于团结3D:在for循环比较数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆