[C#] Not all code paths return a value
Hey there,
So I'm working on some random dungeon-gen, and up until now it's being going great.
I am getting the error Not all code paths return a value I looked into the error, tweaked my code a little and I'm still not making any progress. Let me post my code.
using UnityEngine;
using System.Collections.Generic;
public class DTileMap
{
protected class DRoom
{
public int left;
public int top;
public int width;
public int height;
public int right {
get {return left + width - 1;}
}
public int bottom {
get { return top + height - 1; }
}
public bool CollidesWith(DRoom other) {
if( left > other.right-1 )
return false;
if( top > other.bottom-1 )
return false;
if( right < other.left+1 )
return false;
if( bottom < other.top+1 )
return false;
return true;
}
}
int size_x;
int size_y;
int[,] map_data;
List<DRoom> rooms;
public DTileMap (int size_x, int size_y)
{
this.size_x = size_x;
this.size_y = size_y;
rooms = new List<DRoom> ();
map_data = new int[size_x, size_y];
for (int i=0; i < 30; i++) {
int rsx = Random.Range (4,8);
int rsy = Random.Range (4,8);
DRoom r = new DRoom();
r.left = Random.Range(0, size_x - rsx);
r.top = Random.Range(0, size_y-rsy);
r.width = rsx;
r.height = rsy;
if(!RoomCollides(r)){
rooms.Add (r);
MakeRoom(r);
}
}
}
bool RoomCollides(DRoom r)
{
foreach (DRoom r2 in rooms)
{
if(r.CollidesWith(r2))
{
return true;
}
}
}
public int GetTileAt(int x, int y){
return map_data [x, y];
}
void MakeRoom(DRoom r)
{
for (int x=0; x < r.width; x++)
{
for (int y=0; y < r.height; y++)
{
if(x==0 || x == r.width -1 || y==0 || y==r.height -1)
{
map_data[r.left+x, r.top+y] = 3;
}
else
{
map_data [r.left + x, r.top + y] = 1;
}
}
}
}
}
// Id's
// 0 = Void
// 1 = Floor
// 2 = Random
// 3 = Wall
Now, the error code is pointing towards line 69, but I check all of my value on lines 21 - 34.
If you need any extra information, let me know. I've tried to figure this out, but I'm stumped...
Thanks in advance!
You missing a " return " in DTile$$anonymous$$ap method
return null; return size_x; return whatever;
@Positve7 this was close! But thanks to your answer I realised how blind I was, I've figured it out. I'll post the answer just in case others have the same problem. Thanks for your help!
Also you got the class name and method with same name "DTile$$anonymous$$ap" that could be the problem too
The function DTile$$anonymous$$ap doesn't need to explicitly return anything. It's a constructor. The return type and function name are the same as class name.
Answer by TheBlackBox · Aug 23, 2015 at 10:38 PM
Thanks to @Positive7 I managed to find the answer, and now I feel like an idiot...
This is the code snippet where the answer lay...
bool RoomCollides(DRoom r)
{
foreach (DRoom r2 in rooms)
{
if(r.CollidesWith(r2))
{
return true;
}
}
This is the code, fixed...
bool RoomCollides(DRoom r)
{
foreach (DRoom r2 in rooms)
{
if(r.CollidesWith(r2))
{
return true;
}
}
return false;
And now, I feel like an idiot...
Thanks for your help @positive7
Hehh, interesting ... DTile$$anonymous$$ap should return something too cause its not void , but your class name is DTile$$anonymous$$ap too, As far as I Remember that's not good. I always avoid it so I might be wrong. Glad you fixedd it. $$anonymous$$eep up the good work!
The function DTile$$anonymous$$ap doesn't need to explicitly return anything. It's a constructor. The return type and function name are the same as class name.
Answer by ronaldosilva · Aug 23, 2015 at 10:39 PM
Hi! Looks like the problem is that the function public DTileMap (int size_x, int size_y) is not marked as void for the return type. So the compiler is waiting for something to be returned from this function. Try to put void as the return type of the function as you did in other places and it should be fixed ;)
That function is a constructor. It implicitly returns the object of the its class (type).
Your answer
![](https://koobas.hobune.stream/wayback/20220612001139im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I reference an incremented int with one button, that was incremented from another button? 1 Answer
The body of '' cannot be an iterator block because 'void' is not an iterator interface type??? 2 Answers
Coding Errors 1 Answer
A namespace cannot directly contain mambers such as fields or methods... 2 Answers
Flashlight script help! 2 Answers