- Home /
This question was
closed Nov 22, 2020 at 02:05 PM by
Bobnotarobot for the following reason:
Other
Question by
Bobnotarobot · Nov 24, 2020 at 04:00 PM ·
arraysdictionarykeys
unity dictionary with array as keys causes problems
I am using a dictionary to store coordinates and their respective heights to generate random terrain using a diamond square algorithm. However, when I try accessing keys of the dictionary, I keep getting "KeyNotFoundException: The given key was not present in the dictionary.". Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class TerrainFractal : MonoBehaviour
{
private int SizePower = 3;
private int height = 0;
private int width = 0;
private float seed = 0f;
private float randomHeight = 0;
Dictionary < List<int>, float> WorldMap = new Dictionary< List<int>, float>();
// Start is called before the first frame update
void Start()
{
height = Mathf.RoundToInt(Mathf.Pow(2, SizePower)) + 1;
width = height;
GenerateFractalMap();
}
public void GenerateFractalMap() {
for (int x = 0; x < width; x ++ ) {
for (int y = 0; y < height; y ++ ) {
var coordinates = new List<int> {x, y};
WorldMap[coordinates] = 0;
}
}
randomHeight = Random.Range(0f, 10f);
WorldMap[new List<int> {0, 0}] = randomHeight;
WorldMap[new List<int> {0, height - 1}] = randomHeight;
WorldMap[new List<int> {width - 1, 0}] = randomHeight;
WorldMap[new List<int> {width, height - 1}] = randomHeight;
for (int i = 0; i < 1; i++ ) {
DiamondStep(i);
Debug.Log(WorldMap);
}
}
public void SquareStep(int SizePower, Dictionary <int[], float> Map, int itteration) {
int numberOfPoints = Mathf.RoundToInt(Mathf.Pow(2, itteration));
int NumberOfLines = numberOfPoints * 2 - 1;
int coordX = 0;
int coordY = 0;
}
public void DiamondStep(int itteration) {
int numberOfPoints = Mathf.RoundToInt(Mathf.Pow(2, itteration));
int NumberOfLines = numberOfPoints;
int coordX = 0;
int coordY = 0;
var coords = new int[2];
int displacement = Mathf.RoundToInt(Mathf.Pow(2, SizePower - itteration - 1));
int multiple = Mathf.RoundToInt(Mathf.Pow(2, SizePower - itteration));
float newHeight = 0;
float randomDisplacement = 0;
for (int l = 0; l < NumberOfLines; l ++) {
coordX = displacement + l * multiple;
for (int p = 0; p < numberOfPoints; p ++) {
coordY = displacement + p * multiple;
Debug.Log(WorldMap[new List<int> {0,0}]);
newHeight += WorldMap[new List<int> {coordX - displacement, coordY - displacement}];
newHeight += WorldMap[new List<int> {coordX - displacement, coordY + displacement}];
newHeight += WorldMap[new List<int> {coordX + displacement, coordY - displacement}];
newHeight += WorldMap[new List<int> {coordX + displacement, coordY + displacement}];
newHeight = newHeight / 4;
randomDisplacement = Random.Range(-1f, 1f);
newHeight += randomDisplacement;
WorldMap[new List<int> {coordX, coordY}] = newHeight;
}
}
}
// Update is called once per frame
void Update()
{
}
}
The error occurs at line 66: Debug.Log(WorldMap[new List {0,0}]); I have checked, and the dictionary seems to contain a key of the form [0, 0], so this leaves me very confused.
Thank you for your help.
Comment