Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by Nickshepp · Aug 08, 2021 at 07:55 PM · pathfindinginfinitewhile

Anyone help with an infinite loop problem on a while list.count >0 condition

Ok, this is a slightly messy, massively unoptimized bit of code. My problem is that whenever I run it, it goes infinite and I get nothing output in the console log at all. Have to quit unity to get back in.. The code is part of a pathfinding testing process based on grid movement.

The core loop is an empty list with a single start location added to process from. The loop is a while loop (while list.count>0), and if movement is possible (up,down,left,right only) then those cells are added to the open list for further processing. Except it doesn't work and I just can't figure out why. The codes not finished yet, but I need to figure out why this isn't working, so this isn't the endgoal (need a closed list and a bunch of other stuff, but this ought to be working in its current state)

Gamemanager is a separate gameobject and script with an array called GridContents which is how in this test project I'm tracking the state of the grid and whether each square is empty or not (non walkable).

WorldTile is a custom class representing each game square, which I'll be using later on, if I can get this infinite loop thing sorted. Ok here's the code...

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class PathFinding : MonoBehaviour
 {
     public GameObject GameController;
 
     public void Start()
     {
         GameController gm = GameController.GetComponent<GameController>();
     }
     public void PathfindingTest()
 
     {
         Debug.Log("This is the START of the pathfinding test PATHFINDING function");
         GameController gm = GameController.GetComponent<GameController>();
         
         List<WorldTile> openSet = new List<WorldTile>();
         HashSet<WorldTile> closedSet = new HashSet<WorldTile>();
         
         // TESTING ONLY Add the 0,0 location to the open set
         WorldTile StartingSearchTile = new WorldTile(0, 0);
         openSet.Add(StartingSearchTile);
         Debug.Log("The starting search Tile is " + openSet[0].gridX + "," + openSet[0].gridY);
   
        while (openSet.Count>0)
        {
             WorldTile currentnode = openSet[0];
             int tempx = currentnode.gridX;
             int tempy = currentnode.gridY;
             Debug.Log("The current active node is " +tempx + "," + tempy);
             Debug.Log("New open set count:"+openSet.Count);
 
             // TESTING ONLY - just manual checks based on a 20x7 grid (0,0 BottomLeft > 19,6 UpperRight)
 
             // Check to the left
             if (tempx > 0)
             {
                 if (gm.GridContents[(tempx - 1), tempy] == "empty")
                 {
                   WorldTile wtleft = new WorldTile(tempx - 1, tempy);
                openSet.Add(wtleft);
                 }
             }
 
             // Check to the right, invalid if x>=19 (last column in grid)
             if (tempx < 19)
             {
                 if (gm.GridContents[(tempx + 1), tempy] == "empty")
                 {
                   WorldTile wtright = new WorldTile(tempx + 1, tempy);
                 openSet.Add(wtright);
                 }
             }
 
             // Check above, invalid if y=6 (top row)
             if (tempy < 6)
             {
                 if (gm.GridContents[tempx, (tempy + 1)] == "empty")
                 {
                  WorldTile wtup = new WorldTile(tempx, tempy + 1);
                 openSet.Add(wtup);
                 }
             }
 
             // Check below, invalid if y=0 (bottom row)
             if (tempy>0)
             {
                 if (gm.GridContents[tempx, (tempy - 1)] == "empty")
                 {
                   WorldTile wtdown = new WorldTile(tempx, tempy - 1);
                  openSet.Add(wtdown);
                 }
             }
 
             // We should now have checked all four possible directions, including checking for invalid values, and new items have been added to the open set.
             Debug.Log("All routes checked from: " + tempx + "," + tempy);
             Debug.Log("Removing current item from open list");
                 
             openSet.Remove(currentnode);
             Debug.Log("The open list now has " + openSet.Count + " items");
       }
         Debug.Log("The While loop has exited");
     }
 }


Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Aug 08, 2021 at 08:30 PM

It doesn't look like you have a closed set. At least it's not clear if you somehow magically change the state of your "gm.GridContents" array. If you don't you will always add the same tiles to your open list over and over again. Since you always go left, right, up and down from your current position, it means when you are at position x and you add a node at pos x+1 to the open list, when you finally process that node you will also add node x - 1 to the open list which is the same you started with.


So unless your "WorldTile" constructor does directly modify your GridContents, you'll end up with an endless growing openlist. The processing of that list takes a long time since you're constantly allocating new memory. However sooner or later you would probably run out of memory and your application and Unity would actually crash (getting terminated by the OS).

Comment
Add comment · Show 1 · Share
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 Nickshepp · Aug 08, 2021 at 08:59 PM 0
Share

Yes, this was the problem and I am a donkey! Thanks Bunny83 for confir$$anonymous$$g. Yes once I actually populated the closed list and checked before adding further items it started working correctly. Cheers...

avatar image
0

Answer by Nickshepp · Aug 08, 2021 at 08:19 PM

Ok, possibly I'm an idiot. After three hours staring at it (and of course five minutes after posting) I'm wondering if this is because I'm probably creating an infinite list, as squares are probably being referenced multiple times. I'll add code to add checked nodes to a list and check that list before adding anything more items. I'll report back if that fixes it, but it seems likely.....

Comment
Add comment · Share
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

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

128 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

Related Questions

Problem with loop the get better path to goal 1 Answer

A While Loop in a path finding system is freezing, how can I fix this? 0 Answers

A* Pathfinding Bugs 0 Answers

Path finding on a procedural terrain 0 Answers

check if an object is reachable 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