cant find key in dictionary, using irrlicht lime

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
bardmask
Posts: 6
Joined: Sun May 28, 2023 3:31 am

cant find key in dictionary, using irrlicht lime

Post by bardmask »

i get an error saying dictionary doesn't contain key, any key i check for manually does not exist even if i assign it right before checking eg:

this is inside 3 for loops

Code: Select all

Console.WriteLine("added dirt voxel at : " + new Vector3Di(x, y, z));
voxels.Add(new Vector3Di(x, y, z), new Voxel(VoxelType.Dirt, 2, 0, false, true));
Console.WriteLine(voxels[new Vector3Di(x, y, z)].VoxelType);
or even just manually checking an exact key:

Code: Select all

Console.WriteLine(voxels[new vector3Di(12,135, 7)].VoxelType.ToString());
ive also printed the voxels.Count and it wasnt 0 so it does contain Vector3Di and Voxel data
bardmask
Posts: 6
Joined: Sun May 28, 2023 3:31 am

Re: cant find key in dictionary, using irrlicht lime

Post by bardmask »

Heres the full code, please note that im very new to irrlicht and the documentation for irrlicht lime sucks so im having to guess from the irrlicht documentation. so far all most all classes and functions are very close.

i know that the if statements in the for loops is incorrect and all i get is dirt, but im working on the error first

Code: Select all

using IrrlichtLime.Core;
using IrrlichtLime.Scene;
using IrrlichtLime.Video;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Krutesia
{
    internal class Chunk
    {
        
        // holds the position and voxel data of chunk
        public Dictionary<Vector3Di, Voxel> voxels = new Dictionary<Vector3Di, Voxel>();
        public Vector3Di position;
        public Texture[] textures;
        public List<SceneNode> meshes = new List<SceneNode>();

        public Chunk (Vector3Di pos)
        {
            textures = new Texture[7];
            GetTextures();
            position = pos;
            GenerateVoxels();
        }

        public void GenerateVoxels()
        {
            // generate voxels in chunk
            for (int x = 0; x < WorldData.chunkWidth; x++)
            {
                for (int y = 0; y < WorldData.chunkHeight; y++)
                {
                    for (int z = 0; z < WorldData.chunkWidth; z++)
                    {
                        // set voxels based on layer
                        if (y == WorldData.chunkHeight)
                        {
                            voxels.Add(new Vector3Di(x, y, z), new Voxel(VoxelType.BedRock, 0, 0, true, false));
                            Console.WriteLine("added bedrock voxel at : " + new Vector3Di(x, y, z));
                            
                        }
                        else if (y > WorldData.rockRange.X && y < WorldData.rockRange.Y)
                        {
                            Console.WriteLine("added stone voxel at : " + new Vector3Di(x, y, z));
                            voxels.Add(new Vector3Di(x, y, z), new Voxel(VoxelType.Stone, 1, 100, true, false));
                        }
                        else if (y >= WorldData.rockRange.X && y < WorldData.terrainHeightMax)
                        {
                            Console.WriteLine("added dirt voxel at : " + new Vector3Di(x, y, z));
                            voxels.Add(new Vector3Di(x, y, z), new Voxel(VoxelType.Dirt, 2, 25, true, false));
                        }
                        else
                        {
                            Console.WriteLine("added dirt voxel at : " + new Vector3Di(x, y, z));
                            voxels.Add(new Vector3Di(x, y, z), new Voxel(VoxelType.Dirt, 2, 0, false, true));
                            Console.WriteLine(voxels[new Vector3Di(x, y, z)].VoxelType);
                            
                        }

                        
                    }
                }
            }
            GenerateMeshes();
        }

        public void DisplayMesh()
        {
            while (Program.device.Run())
            {
                Program.device.VideoDriver.BeginScene();
                Program.device.SceneManager.DrawAll();

                Program.device.VideoDriver.EndScene();
            }

            Program.device.Drop();
        }

        public void GenerateMeshes()
        {
            
            for (int x = 0; x < voxels.Count; x++)
            {
                for (int y = 0; y < voxels.Count; y++)
                {
                    for (int z = 0; z < voxels.Count; z++)
                    {
                        if(voxels.ContainsKey(new Vector3Di(x, y, z)))
                        {
                            if (voxels[new Vector3Di(x + 1, y + 1, z + 1)].VoxelType == VoxelType.BedRock)
                            {
                                Console.WriteLine(voxels[new Vector3Di(x, y, z)].VoxelType);
                                SceneNode node = Program.device.SceneManager.AddCubeSceneNode();
                                node.SetMaterialTexture(0, textures[0]);
                                node.Position = new Vector3Df(x, y, z);
                                meshes.Add(node);
                            }
                            else if (voxels[new Vector3Di((int)x, (int)y, (int)z)].VoxelType == VoxelType.Stone)
                            {
                                SceneNode node = Program.device.SceneManager.AddCubeSceneNode();
                                node.SetMaterialTexture(0, textures[1]);
                                node.Position = new Vector3Df(x, y, z);
                                meshes.Add(node);
                            }
                            else if (voxels[new Vector3Di((int)x, (int)y, (int)z)].VoxelType == VoxelType.Dirt)
                            {
                                SceneNode node = Program.device.SceneManager.AddCubeSceneNode();
                                node.SetMaterialTexture(0, textures[2]);
                                node.Position = new Vector3Df(x, y, z);
                                meshes.Add(node);
                            }
                        }
                    }
                }
            }

            //DisplayMesh();
        }

        public void GetTextures()
        {
                textures[0] = Program.device.VideoDriver.GetTexture("content/textures/blocks/cobble_stone.png");
                textures[1] = Program.device.VideoDriver.GetTexture("content/textures/blocks/stone.png");
                textures[2] = Program.device.VideoDriver.GetTexture("content/textures/blocks/dirt.png");
                textures[3] = Program.device.VideoDriver.GetTexture("content/textures/blocks/grass_side.png");
                textures[4] = Program.device.VideoDriver.GetTexture("content/textures/blocks/grass_top.png");
                textures[5] = Program.device.VideoDriver.GetTexture("content/textures/blocks/gravel.png");
                textures[6] = Program.device.VideoDriver.GetTexture("content/textures/blocks/sand.png");
        }
    }
}

bardmask
Posts: 6
Joined: Sun May 28, 2023 3:31 am

Re: cant find key in dictionary, using irrlicht lime

Post by bardmask »

i made it work using a foreach loop with a key value pair, but i still want to know how to access a specific key value without having to loop through the whole dictionary to get it.
Post Reply