i get in itchy feeling that this is gonna take me several days to figure out but im simply not understanding what i did wrong even after watching both videos again. In this section at 5:30 in the video something went wrong where my game just wasnt matching up with the video. for some reason not only are my blue tops not showing up but for some reason when i hit play my isRunning is false and my searchCenter and endwaypoint are always equal to eachother even if its (0,0) (4,3)
here is my code:
public class PathFinder : MonoBehaviour
{
[SerializeField] WayPoint startWayPoint, endWayPoint;
Dictionary<Vector2Int, WayPoint> grid = new Dictionary<Vector2Int, WayPoint>();
Queue<WayPoint> que = new Queue<WayPoint>();
[SerializeField]bool isRunning = true;
Vector2Int[] directions =
{
Vector2Int.up,
Vector2Int.right,
Vector2Int.down,
Vector2Int.left
};
void Start()
{
LoadBlocks();
colorStartAndEnd();
PathFind();
//ExploreNeighbors();
}
private void PathFind()
{
que.Enqueue(startWayPoint);
while(que.Count > 0 && isRunning)
{
var searchCenter = que.Dequeue();
print("searching from " + searchCenter);
HaltIfEndFound(searchCenter);
ExploreNeighbors(searchCenter);
}
print("Finished path finding?");
}
private void HaltIfEndFound(WayPoint searchCenter)
{
if (searchCenter == startWayPoint)
{
print("stop console");
isRunning = false;
}
}
private void ExploreNeighbors(WayPoint from)
{
if(!isRunning) { return; }
foreach(Vector2Int direction in directions)
{
Vector2Int neighbourCordinates = from.GetGridPos() + direction;
try
{
WayPoint neighbour = grid[neighbourCordinates];
neighbour.SetTopColor(Color.blue);
que.Enqueue(neighbour);
print("queing " + neighbour);
} catch
{
//todo
}
}
}
private void colorStartAndEnd()
{
startWayPoint.SetTopColor(Color.green);
endWayPoint.SetTopColor(Color.red);
}
private void LoadBlocks()
{
var waypoints = FindObjectsOfType<WayPoint>();
foreach(WayPoint waypoint in waypoints)
{
var gridPos = waypoint.GetGridPos();
if (grid.ContainsKey(gridPos))
{
Debug.LogWarning("skipping over lapping block " + waypoint);
} else
{
grid.Add(gridPos, waypoint);
}
}
}
Edit: the engine crashed right after posting this lol