Hello all, I’m making an A* pathfinding system for a 2d top down game using a grid of nodes. I have a 2d array that stores the coordinates of each node (NodeGrid wich is in NodeManager). The problem is that it doesn’t find the adjacent nodes of the node the enemy is on because the function that has to find them (FindAdjacentNodes) throws an IndexOutOfRangeException and also it doesn’t remove the node from the open list when it is adding it to the closed list even if I wrote to remove it lol. Here’s the code
public class AIEn : MonoBehaviour {
public List<GameObject> open_list = new List<GameObject>(); //list of the nodes that have to be taken into account
public List<GameObject> closed_list = new List<GameObject>(); //list of the already considered nodes
public GameObject target;
public GameObject my_node;
GetClosestNode targ_node;//the target's closest node
GetClosestNode actual_node;//the enemy's closest node at the start
NodeManager node_manager;//it is the spawner of the nodes and contains the NodeGrid 2d array with all the nodes' coordinates
void Start () {
targ_node = target.GetComponent<GetClosestNode>();
actual_node = gameObject.GetComponent<GetClosestNode>();
node_manager = GameObject.FindGameObjectWithTag("nodes manager").GetComponent<NodeManager>();
}
// Update is called once per frame
void Update () {
if(node_manager.FinishedNodeGrid)
{
//at the start it is the closest node
my_node = actual_node.my_node;
if(!open_list.Contains(my_node))
{
open_list.Add(my_node);
}
if(open_list.Count>0)
{
//with this the node with the least movement cost is the first in the open list
open_list.Sort(delegate (GameObject x, GameObject y)
{
return (x.GetComponent<GetNodeMovementCost>().GetMovementCost(gameObject).CompareTo(y.GetComponent<GetNodeMovementCost>().GetMovementCost(gameObject)));
});
my_node = open_list.First();
if(!closed_list.Contains(my_node) && open_list.Contains(my_node))
{
closed_list.Add(my_node);
open_list.Remove(my_node);
}
FindAdjacentNodes(my_node);
}
}
}
void FindAdjacentNodes(GameObject node)
{
int my_node_coord_x = node.GetComponent<GetNodeCoord>().NodeCoordX;
int my_node_coord_y = node.GetComponent<GetNodeCoord>().NodeCoordY;
GameObject left_node;
GameObject right_node;
GameObject upper_node;
GameObject down_node;
for (int i = 0;i<node_manager.n_nodes_x;i++)
{
for (int j = 0; j < node_manager.n_nodes_y; j++)
{
//if NodeGrid[i,j] == the coordinates of the enemy's node gets the adjacent ones
if (node_manager.NodeGrid[i, j] == node_manager.NodeGrid[my_node_coord_x, my_node_coord_y])
{
//the error is here
left_node = node_manager.NodeGrid[i - 1, j];
right_node = node_manager.NodeGrid[i + 1, j];
upper_node = node_manager.NodeGrid[i, j + 1];
down_node = node_manager.NodeGrid[i, j - 1];
AddAdjacentNodes(left_node);
AddAdjacentNodes(right_node);
AddAdjacentNodes(upper_node);
AddAdjacentNodes(down_node);
}
}
}
}
void AddAdjacentNodes(GameObject adjacent_node)
{
if(!open_list.Contains(adjacent_node))
{
open_list.Add(adjacent_node);
}
}
}
What’s wrong? How can I fix it? If you have any suggestions or better methods to make it don’t hesitate to tell me!
Thank you in advance for your help.