Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Discussion Editor freezing when code runs

Discussion in 'Scripting' started by Mohammed778, Dec 21, 2022.

  1. Mohammed778

    Mohammed778

    Joined:
    Apr 23, 2022
    Posts:
    11
    The editor freezes when I call the "FindPath" function:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using UnityEngine;
    5.  
    6. public class pathFinding
    7. {
    8.     private Grid<pathNode> grid;
    9.  
    10.     public pathFinding(int width, int height, float nodeSizeX, float nodeSizeY)
    11.     {
    12.         grid = new Grid<pathNode>(width, height, nodeSizeX, nodeSizeY, default(pathNode));
    13.  
    14.         for (int x = 0; x < grid.width; x++)
    15.         {
    16.             for (int y = 0; y < grid.height; y++)
    17.             {
    18.                 pathNode Node = new pathNode(grid, x, y);
    19.                 Node.gCost = int.MaxValue;
    20.                 Node.calculateFCost();
    21.                 grid.setValue(x, y, Node);
    22.             }
    23.         }
    24.  
    25.         for (int x = 0; x < grid.width; x++)
    26.         {
    27.             for (int y = 0; y < grid.height; y++)
    28.             {
    29.                 grid.callInAll("findNeighbours");
    30.             }
    31.         }
    32.     }
    33.  
    34.     public List<pathNode> FindPath(int startX, int startY, int endX, int endY)
    35.     {
    36.         pathNode startNode = grid.getValue(startX, startY);
    37.         pathNode endNode = grid.getValue(endX, endY);
    38.         //Debug.Log($"{startNode.X}, {startNode.Y}, {endNode.X}, {endNode.Y}");
    39.         startNode.fCost = 0;
    40.         List<pathNode> openList = new List<pathNode>() {startNode};
    41.         List<pathNode> closedList = new List<pathNode>();
    42.  
    43.         while (openList.Count > 0)
    44.         {
    45.             pathNode curNode = leastFCost(openList);
    46.             openList.Remove(curNode);
    47.             closedList.Add(curNode);
    48.  
    49.             if(curNode == endNode)
    50.             {
    51.                 return calculatePath(endNode);
    52.             }
    53.  
    54.             foreach(pathNode n in curNode.neighbours)
    55.             {
    56.                 if (closedList.Contains(n)) continue;
    57.  
    58.                 n.gCost = curNode.gCost + CalculateDistanceCost(curNode, n);
    59.                 n.hCost = CalculateDistanceCost(n, endNode);
    60.                 n.calculateFCost();
    61.  
    62.                 if (openList.Where((x) => x.X == n.X && x.Y == n.Y).Count() > 0)
    63.                 {
    64.                     if (n.gCost > openList.Where((x) => x.X == n.X && x.Y == n.Y).ToList()[0].gCost) continue;
    65.                 }
    66.  
    67.                 n.cameFromNode = curNode;
    68.                 openList.Add(n);
    69.             }
    70.         }
    71.         return null;
    72.     }
    73.  
    74.     private pathNode leastFCost(List<pathNode> nodes)
    75.     {
    76.         return nodes.Where((x) => x.fCost == nodes.Min(i => i.fCost)).ToList()[0];
    77.     }
    78.  
    79.     private int CalculateDistanceCost(pathNode a, pathNode b) {
    80.         int xDistance = Mathf.Abs(a.X - b.X);
    81.         int yDistance = Mathf.Abs(a.Y - b.Y);
    82.         int remaining = Mathf.Abs(xDistance - yDistance);
    83.  
    84.         return 10 * remaining + 14 * Mathf.Min(xDistance, yDistance);
    85.     }
    86.  
    87.     private List<pathNode> calculatePath(pathNode endNode)
    88.     {
    89.         List<pathNode> path = new List<pathNode>();
    90.         path.Add(endNode);
    91.         pathNode current = endNode;
    92.         while(current.cameFromNode != null)
    93.         {
    94.             path.Add(current.cameFromNode);
    95.             current = current.cameFromNode;
    96.         }
    97.  
    98.         path.Reverse();
    99.         return path;
    100.     }
    101. }
    102.  
    This is A* search algorithm, I followed this tutorial to write the code: https://medium.com/@nicholas.w.swift/easy-a-star-pathfinding-7e6689c7f7b2

    The error should be in the while loop, as freezing errors usually happen when an infinite loop runs, but I could not Identify the error.

    Help would be appriciated.
     
  2. Mohammed778

    Mohammed778

    Joined:
    Apr 23, 2022
    Posts:
    11
    Ok, I am stupid
    Code (CSharp):
    1.         for (int x = 0; x < grid.width; x++)
    2.         {
    3.             for (int y = 0; y < grid.height; y++)
    4.             {
    5.                 grid.callInAll("findNeighbours");
    6.             }
    7.         }
    The error is in this code as the callfunction loops through all:
    Code (CSharp):
    1.     public void callInAll(string function)
    2.     {
    3.         for(int x = 0; x < width; x++)
    4.         {
    5.             for(int y = 0; y < height; y++)
    6.             {
    7.                 grid[x,y].GetType().GetMethod(function).Invoke(grid[x,y], null);;
    8.             }
    9.         }
    10.     }
    And I am calling it in each and every node, this is so stupid.