I am making a pathfinding script for my AI but i need to understand something about the foreach loop because i think it is causing my script to cause problems.
I want to know if the return is activated after the for loop has searched all of the tiles or does it return after each iteration of the loop?
Example:
foreach(Tile tile in AllTiles){
closestTile = tile;
}
return closestTile;
And here is my actual script:
Tile GetTileValues(){
float minFvalue = Mathf.Infinity;
Tile candidate = null;
foreach(Tile t in open){
//return abs(toCoord.x - fromCoord.x) + abs(toCoord.y - fromCoord.y);
float hValue = Mathf.Abs(endTile.transform.position.x -
t.transform.position.x +
Mathf.Abs(endTile.transform.position.y -
t.transform.position.y));
float gValue = Mathf.Abs(startTile.transform.position.x -
t.transform.position.x +
Mathf.Abs(startTile.transform.position.y -
t.transform.position.y));
float fValue = gValue + hValue;
if(fValue < minFvalue){
minFvalue = fValue;
candidate = t;
}
}
open.Remove(candidate);
if(!closed.Contains(candidate)){
closed.Add(candidate);
}
return candidate;
}