0

I've tried using the generic list but my research said that the enumerator for the List could not be reset so that wont do since I need to iterate continuously over a list of float [,].

I essentially want to cache 10 different perlin noise maps that the game loop iterates over. Each perlin map is a float [,]. All maps are of same width and height.

I want to store these in some datastructure that I can continously iterate over, be it generic list or an array:

void BuildCache() {
    cache = new float[cacheSize][,];
    for(int i = 0; i < cacheSize; i++) {
        float[,] noiseMap = Noise.GenerateNoiseMap (width, height, seed, noiseScale, octaves, persistence, lacunarity, offset);
        cache [i] [0] = noiseMap;
        offset += speed;
    }
}

This results in this error: Assets/Scripts/FogGenerator.cs(51,36): error CS0022: Wrong number of indexes 1' inside [], expected2'

It seems like a basic thing, in Java I would use a generic list but since I can't reset C#'s generic list I'm at a loss here.

1 Answers1

3

In declaration of your array you explicitly declare that cache array is an array of multidimensional arrays. Error is in cache[i][0] = noiseMap; because it is similarly to two-dimensional array syntax in C/C++ based languages. You should use cache[i] = noiseMap because then you explicitly nofity compiler that you reference to two dimensional array in this array and write in this some value.

hubot
  • 393
  • 5
  • 18
  • Make sense, but then how do I define the actual cache? This is the way I was doing it: float[][,] cache; This produces: Assets/Scripts/FogGenerator.cs(51,25): error CS0029: Cannot implicitly convert type `float[,]' to `float[]' – swedish_fisk Jun 06 '16 at 19:07
  • 1
    Are you sure that you use array_name[index, index] instead of array_name[index][index]? I said you that replace cache[i][0] = noiseMap by cache[i] = noiseMap. – hubot Jun 06 '16 at 19:12
  • 1
    This works fine. Compare it with your code. static void BuildCache() { var cacheSize = 10; var cache = new float[cacheSize][,]; for (int i = 0; i < cacheSize; i++) { float[,] noiseMap = {{2.3f,4.5f},{2.3f,4.5f}}; cache[i] = noiseMap; } } – shfire Jun 06 '16 at 19:18
  • 1
    @shfire: I thought about the same – hubot Jun 06 '16 at 19:21