I ran into a problem with my code in the middle of the Running Manual Tests video. My console and block colors don’t seem to match up with Ben’s in the video. I know this is like one of those “plz fix my code” questions but this really stumped me the point that I couldn’t continue with the course.
Here is how my console looks when played:
Here are how my blocks look:
Here is how Ben’s console looks:
And his blocks:
Here’s how my code looks:
[SerializeField] Waypoint startWaypoint, endWaypoint; //Creates a start and end waypoint
//Dictionary of
Dictionary<Vector2Int, Waypoint> grid = new Dictionary<Vector2Int, Waypoint>(); //Creates a "grid" dictionary
Queue<Waypoint> queue = new Queue<Waypoint>(); //Creates a queue for waypoints
[SerializeField] bool isRunning = true;
Vector2Int[] directions =
{
Vector2Int.up,
Vector2Int.right,
Vector2Int.down,
Vector2Int.left
};
void Start()
{
LoadBlocks(); //Loads each waypoint in the world into a dictionary "grid"
ColorStartAndEnd(); //Colors start and end blocks
Pathfind();
//ExploreNeighbours();
}
private void Pathfind()
{
queue.Enqueue(startWaypoint); //Puts start in queue
while (queue.Count > 0 && isRunning) //begins pathfinding loop
{
var searchCenter = queue.Dequeue(); //2b
print("Searching from " + searchCenter); //TODO: Remove
HaltIfEndFound(searchCenter); //Ends pathfinding
ExploreNeighbours(searchCenter);
}
print("finished pfinding?");
}
private void HaltIfEndFound(Waypoint searchCenter) //parameter: search center of type waypoint
{
if (searchCenter = endWaypoint) //if the searchcenter/ the position = end
{
print("Searching from end node therefore stopping");
isRunning = false;
}
}
private void ExploreNeighbours(Waypoint from)
{
if (!isRunning) { return; } //stops the code if the search isn't running
foreach (Vector2Int direction in directions) //go round each direction
{
//since it is a foreach loop, every time it loops, it gets a new direction. This new direction leads the repeated search below
Vector2Int neighborCoordinates = from.GetGridPos() + direction; //replaces from.getgridpos() + direction, with explorationCordinates
try //performs the following operation if possible
{
Waypoint neighbor = grid[neighborCoordinates]; //replaces grid[neighbor] with neighbor
neighbor.SetTopColor(Color.blue); //finds the neighbor and sets its color to indicate it has been queued
queue.Enqueue(neighbor); //the neighbor has been queued
print("Queued " + neighbor);
}
catch //performs if the above try operation fails
{
//do nothing
}
}
}
private void ColorStartAndEnd()
{
startWaypoint.SetTopColor(Color.green); //sets startwaypoint's color to green
endWaypoint.SetTopColor(Color.red); //sets endwaypoint's color to red
}
private void LoadBlocks()
{
var waypoints = FindObjectsOfType<Waypoint>(); //Replaces FindObjectsOfType<Waypoint(); with waypoints
//Always remember the S after FindObject otherwise it can't look for multiple items.
foreach (Waypoint waypoint in waypoints)
{
var gridPos = waypoint.GetGridPos();
/*
The above line of code makes the rest of the code in LoadBlocks() simpler to read
by substituting waypoint.GetGridPos(); with gridPos.
*/
bool isOverlapping = grid.ContainsKey(gridPos);
/*
Could remove the line above and copy
grid.ContainsKey(gridPos) into the if statement parameter below
*/
if (isOverlapping)
{
Debug.LogWarning("Skipping overlapping block: " + waypoint); //Logs warning to console if block is overlapped
} //If the block is overlapping don't add it to the dictionary
else
{
grid.Add(gridPos, waypoint); //Adds waypoint to grid Dictionary
}
}
print("Loaded " + grid.Count + " blocks"); //Prints amount of blocks to console
}