0

Using unity to create a procedural map I'm getting the Error "Cs0161: not all code paths return a value" , I'm still pretty new to coding and I may have made some mistake.

I've tried google but the answers don't even make sense to me yet

MapData GenerateMapData()
{
    float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize, mapChunkSize, seed, noiseScale, octaves, lacunarity, persistance, offset);

    Color[] colourMap = new Color[mapChunkSize * mapChunkSize];

    for (int y = 0; y < mapChunkSize; y++)
    {
        for (int x = 0; x < mapChunkSize; x++)
        {
            float currentHeight = noiseMap[x, y];
            for (int i = 0; i < regions.Length; i++)
            {
                if (currentHeight <= regions[i].height)
                {
                    colourMap[y * mapChunkSize + x] = regions[i].colour;
                    break;
                }
            }
        }

        return new MapData(noiseMap, colourMap);
    }
}

Error CS0161 'MapGenerator.GenerateMapData()': not all code paths return a value Assembly-CSharp

Stefan
  • 652
  • 5
  • 19
Pike
  • 31
  • 5
  • 1
    The error actually already tells you what's wrong. Look at the last line, what if ```mapChunkSize``` is 0? You don't return anything in that case. – devsmn Oct 21 '19 at 08:57
  • 1
    I *suspect* you just meant to have your `return` statement outside the top level loop. It would be odd to have a loop that only executed once. – Jon Skeet Oct 21 '19 at 08:58
  • 2
    Possible duplicate of [c# returning error "not all code paths return a value"](https://stackoverflow.com/questions/21197410/c-sharp-returning-error-not-all-code-paths-return-a-value) – Egbert Oct 21 '19 at 09:08
  • a method with a type other than void must return something –  Oct 21 '19 at 11:20

3 Answers3

1

After the for loops you should also return a value. See the code below and the comment at the bottom of the method:

MapData GenerateMapData()
{
    float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize, mapChunkSize, seed, noiseScale, octaves, lacunarity, persistance, offset);

    Color[] colourMap = new Color[mapChunkSize * mapChunkSize];

    for (int y = 0; y < mapChunkSize; y++)
    {
        for (int x = 0; x < mapChunkSize; x++)
        {
            float currentHeight = noiseMap[x, y];
            for (int i = 0; i < regions.Length; i++)
            {
                if (currentHeight <= regions[i].height)
                {
                    colourMap[y * mapChunkSize + x] = regions[i].colour;
                    break;
                }
            }
        }

        return new MapData(noiseMap, colourMap);
    }

    // You should also return something here
}

It is theoretically possible that mapChunkSize can have a value of 0 (zero) and it will not enter the first for loop at all. That's why you need a return statement at the bottom as well.

Stefan
  • 652
  • 5
  • 19
0

The reason why you are receiving that error is because your return statement is inside the for loops. More specifically, it can happen that neither of the loops execute (e.g. mapChunkSize == 0. Then 0 isn't less than 0 and the program doesn't enter the for loops) and that thus nothing gets returned. So either move the return outside the loops or add another return outside the loops.

MapData GenerateMapData()
    {
        float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize, mapChunkSize, seed, noiseScale, octaves, lacunarity, persistance, offset);

        Color[] colourMap = new Color[mapChunkSize * mapChunkSize];

        for (int y = 0; y < mapChunkSize; y++)
        {
            for (int x = 0; x < mapChunkSize; x++)
            {
                float currentHeight = noiseMap[x, y];
                for (int i = 0; i < regions.Length; i++)
                {
                    if (currentHeight <= regions[i].height)
                    {
                        colourMap[y * mapChunkSize + x] = regions[i].colour;
                        break;
                    }
                }
            }

            /*return new MapData(noiseMap, colourMap); //uncommenting this might still leave you with the functionality you want */
        }

        return new MapData(noiseMap, colourMap);

}
Nikola Petrovic
  • 101
  • 2
  • 9
0

you are not returning any thing outside the first loop, try to evaluate values inside the loops and then return from outside the loop.

for (int y = 0; y < mapChunkSize; y++)
        {
            for (int x = 0; x < mapChunkSize; x++)
            {
                float currentHeight = noiseMap[x, y];
                for (int i = 0; i < regions.Length; i++)
                {
                    if (currentHeight <= regions[i].height)
                    {
                        colourMap[y * mapChunkSize + x] = regions[i].colour;
                        break;
                    }
                }
            }

            return new MapData(noiseMap, colourMap);
        }

       add return statement here
saqib amin
  • 41
  • 1
  • 10