Hello!
I've got a question about some functions I've wrote while trying to figure out how to make pathfinding work. Alright so here's the deal. For testing and debugging purposes I'm using failsafe variable, which basically decides how many loops will be done. I noticed that if I'll do just one loop, everything goes fine, but when GetByPosition() is called first time in second loop - the application freezes.
So here's main method:
Code:
private IEnumerator Pathfinder(){
Place(open, new Node(0, 0, start, null));
Node current = null;
while(open.Count > 0 && failsafe > 0){ //While we have something in open dictionary
yield return new WaitForSeconds(.5f);
failsafe--;
current = Pick(open); //Set current node to best choice from open dictionary
RemoveByValue(open, current); //Remove current from open
Place(closed, current); //and place it in closed
if(current.position == target){
StartCoroutine("GeneratePath", current); //When target is reached, break loop and generate path from the last element
break;
}
GameObject tmp2 = (GameObject)Instantiate(player, current.position, Quaternion.identity);
tmp2.renderer.material.color = Color.yellow;
gScore = current.cost;
foreach(Node n in GetParentNodes(current)){
print ("Open: " + open.Count + " Closed: " + closed.Count + " For: " + current.position.ToString());
if(n != null){
if(obstacles.ContainsValue (n.position) == false){
GameObject tmp = (GameObject)Instantiate(player, n.position, Quaternion.identity);
tmp.renderer.material.color = Color.green;
float estmationOfGscore = current.cost + n.cost;
if(GetByPosition (n.position, closed) == null)
continue;
if(GetByPosition (n.position, open) == null && GetByPosition (n.position, closed) == null){
print ("move to open");
n.cost = current.cost + 1;
n.fScore = n.cost + Heuristic(n);
n.previous = current;
Place (open,n);
}
Destroy(tmp,0);
}
}
}
Destroy(tmp2,0);
}
}
and here is the one which seem to cause the problem
Code:
private Node GetByPosition(Vector3 pos, Dictionary<int, Node> d){
Node m = null ;
if(d.Count > 0){
foreach(KeyValuePair<int, Node> n in d){
if(n.Value.position == pos){
m = n.Value;
}
}
}
if(m == null)
print ("No Node in dict");
return m;
}