Vector3Int as dictionary key acting wierd

SOLVED

I have been stuck on this problem for quite some time now and I really can not get it to work. I’ll share the code first and then explain the problem as I believe that the problem only makes sence once you have seen the code.

 Vector3Int neighborChunk = m_chunkCoordinates;
 if (face == 0)
            {
                neighborChunk.x = m_chunkCoordinates.x - 1;
            }
            else if (face == 1)
            {
                neighborChunk.z = m_chunkCoordinates.z - 1;
            }
            else if (face == 2)
            {

                neighborChunk.x += 1;
            }
            else if (face == 3)
            {

                neighborChunk.z += 1;

            }

            if (m_worldManager.isChunkInWorld(neighborChunk))
            {
                Debug.Log("Chunk does not exist: " + neighborChunk +    " this chunk: " + m_chunkCoordinates + " face is " + face);
            }
            else {
                Debug.Log("Chunk does exist: " + neighborChunk + " this chunk: "+ m_chunkCoordinates);
            }

This is what is inside the m_worldManager.isChunkInWorld(neighborChunk) function:

    public bool isChunkInWorld(Vector3Int coord) {
        return m_chunkMap.ContainsKey(coord);
    }

The dictionary declaration is looks like this: Dictionary<Vector3Int, Chunk> m_chunkMap
The wierd thing is that the the chunk is not found whenever the code enters the if 1 or 0 branch but the same chunk will be found if it takes route 2 or 3. This applies to every chunk. Thank you for your help.
Here is a screenshot of a chunk both existing and not existing at the same time. I am never removing things from the dictionary. (See first and last outputs)

EDIT: The problem was that the m_worldManager captured in each chunk was a copy and not a reference to the class that I passed in the constructor of each chunk. The world manager was assigned only when the chunks were initialized so the dictionary had not been completely filled and since the dictionary was only a copy and not a reference new chunk entries were not added to chunks that already had been initiated. Thus making the chunks that were initiated afterwards invisible to the previous chunks. Sorry for the bad explenation.

How are you populating the dictionary?

Like this: m_chunkMap.Add(new Vector3Int((int)(x - worldSizeInChunks / 2), 0, (int)(z - worldSizeInChunks / 2)), new Chunk(this, new Vector3Int(x - worldSizeInChunks / 2, 0, z - worldSizeInChunks / 2)));

Is this in a loop somewhere or something? As it is that line will only add a single chunk to the dictionary.

1 Like

Oh yea sorry, my bad, I should have included it aswell. Here is the code:

        for (int x = 0; x < worldSizeInChunks; x++) {
            for (int z = 0; z < worldSizeInChunks; z++)
            {
                m_chunkMap.Add(new Vector3Int((int)(x - worldSizeInChunks / 2), 0, (int)(z - worldSizeInChunks / 2)), new Chunk(this, new Vector3Int(x - worldSizeInChunks / 2, 0, z - worldSizeInChunks / 2)));
            }
        }

World size in chunks is 5 so I get a 5x5 grid centered around xyz 0

You should add some debug logging. Maybe print out the entire chunk map and make sure it looks the way you want it to look. I think you may have some issues with the structure.

For example I think “x - worldSizeInChunks / 2” might not do what you want. You might want to add some parentheses to make sure the math is happening int he correct order. E.g. “x - (worldSizeInChunks / 2)”. Also you repeat that operation a lot. Maybe just do it once and store it in a variable for reuse. Same with the Vector3Int, it looks like you’re creating that thing twice every time you insert one. Create it once and reuse it for the key and the chunk itself.

I made a simple debug print like this and the chunks are being created as expected: all chunks from x: -3 to x: 2 and y: -3 to 2 are being created. Thank you so much for the optimization help, I will think about that when I write my future code.

        foreach(Vector3Int i in m_chunkMap.Keys) {
            Debug.Log(i + ", ");
        }

Thank you I solved it thanks to you mentioning how I added the chunks! (I wrote what the problem was in the thread header)

1 Like

Awesome glad you solved it! Sometimes it just takes looking at the data in a different way.