Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by khaledyasser · Jul 24, 2019 at 11:08 AM · scripting problemrecursion

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vega4Life · Jul 24, 2019 at 03:01 PM 0
Share

What does the build function look like?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

258 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges