Problem with my code

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:
image
Here are how my blocks look:
image

Here is how Ben’s console looks:
image
And his blocks:
image

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
}

Hi Raza,

Is the setting exactly the same as Ben’s? Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

Yes I have compared my code. And what do you mean by setting?

I have pinpointed the problem, the ExploreNeighbours() method doesnt seem to run when called.

I meant: Did you place your waypoints exactly as Ben? Did you do everything exactly as him?

What do you mean by the ExploreNeighbours method did not seem to run? Did you test that with a Debug.Log? Doesn’t the method get called at all or is isRunning false?

Yes I have checked the setting, all the blocks are in the correct order in the hierarchy (although I don’t know why that would affect it). I was wrong when I said when I found the problem was the ExploreNeighbours not being called; using the console and print messages I figured out the code stops running after the print("loading1"); line in the picture below. This means the if statement causes the method to return meaning the isRunning bool is false. But as you can see in the third picture I have set this bool to true at the top.


image
image

Fantastic. I’m sure we are almost there.

Since you narrowed down the problem to isRunning, check where and when the variable gets set to false, and if that’s correct.

For example, your HalfIfEndFound sets isRunning to false. I’m noticing a little typo in the if-condition. There is a = instead of an ==. Didn’t the compiler complain?

It worked! Thank you for helping me so many times, you’re a life saver :star_struck:! I don’t know why my editor didn’t pick it up as an error, I’m sure it sent a warning to the log that I didn’t notice. One question, do we use == for any if statement circumstance?

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms