- Home /
 
Dictionary wont give me value with correct key
No matter what I try GetBlock always has neochunk return null, despite neochunk equaling the WorldPos put into the chunks Dictonary. For ex one of the chunks has a key of (0, 0, 4), but even when newpos is (0, 0, 4) neochunk is null.
 public Matrix4x4 id = Matrix4x4.identity;
 
     Chunk chunk;
     WorldPos pos;
 
     public Material material;
 
     public int ViewDist = 4;
 
     FindChunk chunkHelp;
 
     private int chunksize = Chunk.chunksize;
 
     public Dictionary<WorldPos, Chunk> chunks = new Dictionary<WorldPos, Chunk>();
     private void Start() {
         chunkHelp = new FindChunk();
         for (int x = 0; x < ViewDist; x++){
             for (int y = 0; y < ViewDist; y++){
                 for (int z = 0; z < ViewDist; z++){
                     chunk = new Chunk(new WorldPos(x * chunksize, y * chunksize, z * chunksize), this);
 
                     chunk.GenerateChunkArray();
 
                     chunks.Add(chunk.pos, chunk);
                 }
             }
         }
         foreach(WorldPos pos in chunks.Keys){
             Debug.Log(pos.x);
             Debug.Log(pos.y);
             Debug.Log(pos.z);
             Debug.Log("Next");
         }
         foreach(Chunk chunk in chunks.Values){
             chunk.GetMesh();
         }
     }
 
     private void Update() {
         foreach(Chunk chunk in chunks.Values){
             Graphics.DrawMesh(chunk.mesh, id, material, 0); 
         }
     }
 
     public Blocks GetBlock(WorldPos pos, int x, int y, int z) {
         WorldPos newpos = chunkHelp.ChunkToChunk(pos, x, y, z);
         Chunk neochunk;
         chunks.TryGetValue(newpos, out neochunk);
 
         if (neochunk == null){
             Debug.Log(newpos.x);
             Debug.Log(newpos.y);
             Debug.Log(newpos.z);
             Debug.Log("Null chunk spot");
             return Blocks.Air;
         }
 
         WorldPos nnpos = chunkHelp.ChunkToBlock(x, y, z);
 
         return neochunk.blocks[nnpos.x, nnpos.y, nnpos.z];
     }
 
              Answer by Esteem · Jan 24, 2020 at 11:40 PM
why don't you use Vector3 instead of WorldPos
If WorldPos coordinate values are floats, don't just debug them, you;re rounding the value. Float values can't be trusted when used with dictionaries like this.
do a Debug.Log(newpos.x.ToString("0.000000000000000"); to see if the value is actualy what you need it to be
if it's a float then sometimes newPos.x can be 5.99999999999999996 or whatnot instead of 6
The x, y, and z of WorldPos are all ints, so it shouldn't end up like that.
What exactly is WorldPos ? A struct? If it's not a struct, how do you ensure equality? Did you proberly implement GetHashCode and Equals in your custom type? Since you use a custom type as key in a dictionary, that custom type is the most important information. All the other code you've posted is pretty irrelevant.
I feel real dumb, but the problem was that WorldPos was a class ins$$anonymous$$d of a struct.
Your answer