0

i get an error saying key could not be found in dictionary. even if i set that exact key with value right before checking that value; i am using irrlicht lime. im still new to this engine and irrlicht lime documentation is non existent so ive been guessing using the online irrlicht documentation. most classes and methods are the same but there are a few that are either spelled the same but work slightly differently or it got completely renamed in irrlicht lime. any way heres the code that has the error. please note that im just prototyping to learn irrlicht lime, so the code is ugly and ineffecient:

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

the error where i check the key is this:

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);
bard mask
  • 1
  • 1

1 Answers1

0

ok im still not sure why voxels[new Vector3Di(x, y, z)] doesnt work but i switched to a foreach loop that breaks at the end, i know that breaking at the end isnt good but it made it work for now. heres the foreach:

foreach (KeyValuePair<Vector3Di, Voxel> key in voxels)
{
    if (key.Value.VoxelType == VoxelType.BedRock)
    {
        SceneNode node = Program.device.SceneManager.AddCubeSceneNode();
        node.SetMaterialTexture(0, textures[0]);
        node.Position = new Vector3Df(x, y, z);
        meshes.Add(node);
    }
    else if (key.Value.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 (key.Value.VoxelType == VoxelType.Dirt)
    {
        SceneNode node = Program.device.SceneManager.AddCubeSceneNode();
        node.SetMaterialTexture(0, textures[2]);
        node.Position = new Vector3Df(x, y, z);
        meshes.Add(node);
        Console.WriteLine("added dirt mesh to array at position : " + new Vector3Di(x, y, z));
    }
    break;
}

i would still like to know how to access an exact key value pair without having to loop through them

bard mask
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 28 '23 at 12:24