TEXTFILE I / O:不要写一样的东西要写、东西、TEXTFILE

2023-09-04 03:38:42 作者:血溅╭彼岸花

我有一个功能,存储的坐标和块的名字,但问题是,这个函数被调用获取生成每次新的块,但旧的继续加载,如果他们不远处。因此结果是,在文本文件中,函数写入组块有时2倍。 我不希望这种情况发生,并有功能只写每块一次。 主要的问题是,我不能用StreamwWriter和StreamReader的在同一时间。

这是我的code:

 函数saveLoadedChunk(){
    VAR loadedChunks:游戏对象[] = FindObjectsOfType(游戏对象)作为游戏对象[];
    VAR文件名=C:/堆游戏/ chunks.txt;
    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 +);
                sw.WriteLine(stringToWrite);
            }
        }
    }
    sw.Flush();
    sw.Close();
}
 

解决方案

preamble

我主要是用C#的工作,所以有可能在语法一些错误。

解决方案

关于我国数据治理法治构建的几点思考

您可以将您在内存中设置的所有块。里面saveLoadedChunk你要更新这一套,然后写它的内容到文件:

  VAR块:HashSet的< Vector3类型> =新的HashSet< Vector3类型>();

传播saveLoadedChunk(){
    VAR loadedChunks:游戏对象[] = FindObjectsOfType(游戏对象)作为游戏对象[];
    对于(VAR I = 0; I< loadedChunks.length;我++)
        如果(loadedChunks [I] .name.Substring(0,5)==块|| loadedChunks [I] .name.Substring(0,5)==_TERR)
            如果(loadedChunks [I] .TAG!=玩家)
                chunks.Add(loadedChunks [I] .transform.position);
    VAR文件名=C:/堆游戏/ chunks.txt;
    VAR南方周末:System.IO.StreamWriter =新System.IO.StreamWriter(文件名,错误);
    对于(VAR块:Vector3类型的块){
        变种stringToWrite =组块(+ chunk.x +,+ chunk.y +,+ chunk.z +);
        sw.WriteLine(stringToWrite);
    }
}
 

这里的缺点是:

您从空白设置每次应用程序启动。 您保存在内存中的所有块COORDS。但我不会说这是缺点,直到你真的,真的好多块。

为了解决您需要填写与存储在 corrds C首先的问题:/堆游戏/ chunks.txt 一旦应用程序已启动。

I have a function that stores the coordinates and the name of that chunk, but the problem is that this function gets called everytime new chunks get generated, but the old ones keep being loaded if they are not far. So the result is that in the textfile, the function writes the chunks sometimes 2 times. I don't want this to happen and have the function only write every chunk once. The main problem is that I can't use StreamwWriter and StreamReader at the same time.

This is my code:

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, 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 + ")";
                sw.WriteLine(stringToWrite);
            }
        }
    }
    sw.Flush();
    sw.Close();
}

解决方案

Preamble

I mostly work with C#, so there could be some mistakes in syntax.

Solution

You can store all your chunks in memory set. Inside saveLoadedChunk you are going to update this set and then write it's contents to file:

var chunks: HashSet<Vector3> = new HashSet<Vector3>();

function saveLoadedChunk() {
    var loadedChunks : GameObject[] = FindObjectsOfType(GameObject) as GameObject[];
    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")
                chunks.Add(loadedChunks[i].transform.position);
    var fileName = "C:/Reactor Games/chunks.txt";
    var sw : System.IO.StreamWriter = new System.IO.StreamWriter(fileName, false);
    for (var chunk: Vector3 in chunks) {
        var stringToWrite = "Chunk (" + chunk.x + ", " + chunk.y + ", " + chunk.z + ")";
        sw.WriteLine(stringToWrite);
    }
}

The drawback here is:

That you start with blank chunks set every time app is launched. You store all the chunks' coords in memory. But I wouldn't say that this is drawback until you have really-really lot of chunks.

In order to solve first problem you need to fill chunks with corrds stored in C:/Reactor Games/chunks.txt once the app is started.