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.

4
  • 5
    cache[i] = noiseMap, remove [0] Commented Jun 6, 2016 at 18:44
  • 1
    What do you mean you cannot reset the enumerator for a List? Commented Jun 6, 2016 at 18:53
  • Arturo how does one declare the cache then? Cause I still get errors. My definition looks like this: float[][,] cache; Commented Jun 6, 2016 at 18:57
  • juharr List<T> in System.Collections.Generic can only be iterated using the enumerator was my belief. This enumerator has no reset implementation and I couldn't create a new enumerator since it started at the end of the list still? Commented Jun 6, 2016 at 19:00

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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[]'
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.
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: I thought about the same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.