I made an infinite recursion but I don't know how to fix it.
So, I'm making a procedurally generated roguelike rpg game and I'm stuck trying to get procedural generation. I've never made a procedurally generated game before so I don't know if there is a better way to do this but this is how I did it:
Basically I'm trying to generate rooms in random configurations by making a linked list (or tree?) class. So I have a class Room with reference to 4 rooms, topRoom, rightRoom, leftRoom and bottomRoom. The Room class also has a vector 3 for position and that's all the significant properties it has right now. The constructor takes in just a vector 3 for position. Then I have a static class called LevelBuilder which does what it says, builds levels out of this room class. Here it is. The basic premise is:
1- Get an array of all the possible locations for rooms from a single room (top, right, bottom, left) 2- Loop through the array and find if there is a room at any of those locations 3- If there is not a room, generate a probability of spawning one and test it, the probability will be dependent on the distance from the origin in a particular direction. So the chance of creating another room to the top goes down the further up we go. Same with right, left, etc. 4- If a room has been created, repeat from step (1) on that room
public static class LevelBuilder
{
public static void build(Room source)
{
Room[] clockwise = { source.top, source.right, source.left, source.bottom };
int checker = 0;
Vector2 possibleRoom = Vector2.zero;
for (int i = 0; i < clockwise.Length; i++)
{
Room current = clockwise[i];
if (current is null)
{
if (i == 0)
{
possibleRoom = source.position + new Vector3(0, 10, 0);
float topChance = Mathf.Pow(0.7f, possibleRoom.y / 10) * 100;
checker = Random.Range(0, 100);
if (topChance > checker && Physics2D.OverlapPoint(possibleRoom) == null)
{
source.top = new Room(possibleRoom);
build(source.top);
}
}
if (i == 1)
{
possibleRoom = source.position + new Vector3(10, 0, 0);
float rightChance = Mathf.Pow(0.7f, possibleRoom.x / 10) * 100;
checker = Random.Range(0, 100);
if (rightChance > checker && Physics2D.OverlapPoint(possibleRoom) == null)
{
source.right = new Room(possibleRoom);
build(source.right);
}
}
if (i == 2)
{
possibleRoom = source.position + new Vector3(0, -10, 0);
float bottomChance = Mathf.Pow(0.7f, -possibleRoom.y / 10) * 100;
checker = Random.Range(0, 100);
if (bottomChance > checker && Physics2D.OverlapPoint(possibleRoom) == null)
{
source.bottom = new Room(possibleRoom);
build(source.bottom);
}
}
if (i == 3)
{
possibleRoom = source.position + new Vector3(-10, 0, 0);
float leftChance = Mathf.Pow(0.7f, -possibleRoom.x / 10) * 100;
checker = Random.Range(0, 100);
if (leftChance > checker && Physics2D.OverlapPoint(possibleRoom) == null)
{
source.left = new Room(possibleRoom);
build(source.left);
}
}
}
}
}
}
It's either this code is wrong or the part where I instantiate the rooms goes into an infinite loop (also recursion)
I get this error: The requested operation caused a stack overflow and it points to the build function
Your answer
Follow this Question
Related Questions
Why ain't my Code working? 2 Answers
Character Animation 1 Answer
Why this makes Unity crash? 2 Answers
Start() and Awake() not called on MonoBehaviour 1 Answer
Using variables from different scripts in Void Update(); 2 Answers