Confusion about pathfinding code

image
I was reviewing the code to make sense of it but this part isn’t making too much sense to me, it’s creating a variable called “searchCenter” that’s holding the function of dequeuing things from the queue, I think. and so is it taking what was removed from the queue and marking it as explored, and then telling you that it’s searching from that coordinate?

And then also for the “ExploreNeighbours” part, are we taking the “searchCenter” and equaling it to the new “Waypoint from”?

Hi,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

it’s creating a variable called “searchCenter” that’s holding the function of dequeuing things from the queue

queue.Dequeue() returns an object. We assign that object to searchCenter, not the method. We access that object and set its isExplored variable to true.

And then also for the “ExploreNeighbours” part, are we taking the “searchCenter” and equaling it to the new “Waypoint from”?

Could you share the code please to ensure we are talking about the same?

Hope this helps :slight_smile:


See also;

Here we go, but I think I get what your saying? I’m mostly just confused on the var searchCenter = queue.Dequeue(); part, so to reiterate, queue.Dequeue() removes the thing in the earliest part of the queue and puts it into a variable rather then just deleting it all together, and then is used for functionality later in the code. Please correct me if I’m wrong. -

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pathfinder : MonoBehaviour {

[SerializeField] Waypoint startWaypoint, endWaypoint;
Dictionary<Vector2Int, Waypoint> grid = new Dictionary<Vector2Int, Waypoint>();
Queue<Waypoint> queue = new Queue<Waypoint>();
bool isRunning = true;

Vector2Int[] directions =
{
    Vector2Int.up, //same as (0,1)
    Vector2Int.right, //same as (1,0)
    Vector2Int.down, //same as (0,-1)
    Vector2Int.left // same as (-1,0)
};

void Start ()
{
    LoadBlocks();
    ColorStartAndEnd();
    Pathfind();
    //ExploreNeighbours();
}

private void LoadBlocks()
{
    var waypoints = FindObjectsOfType<Waypoint>();
    foreach (Waypoint waypoint in waypoints)
    {
        var gridPos = waypoint.GetGridPos();
        if (grid.ContainsKey(gridPos)) //proofing for duplicate blocks
        {
            Debug.LogWarning("Skipped overlapping block at " + waypoint);
        }
        else
        {
            grid.Add(gridPos, waypoint);
        }
    }
}

private void ColorStartAndEnd()
{
    startWaypoint.SetTopColor(Color.green);
    endWaypoint.SetTopColor(Color.red);
}

private void Pathfind()
{
    queue.Enqueue(startWaypoint); //what starts the queue
    
    while(queue.Count > 0 && isRunning)
    {
        var searchCenter = queue.Dequeue();
        searchCenter.isExplored = true;
        print("Searching from: " + searchCenter); //todo remove logging
        HaltIfEndFound(searchCenter);
        ExploreNeighbours(searchCenter);
    }

    // todo work-out path
    print("Finished pathfinding job?");
}

private void HaltIfEndFound(Waypoint searchCenter)
{
    if (searchCenter == endWaypoint)
    {
        print("Current waypoint equals end waypoint, stopping queue"); // todo remove logging
        isRunning = false;
    }
}

private void QueueNewNeighbours(Vector2Int neighbourCoordinates)
{
    Waypoint neighbour = grid[neighbourCoordinates];
    if (neighbour.isExplored)
    {
        // do nothing
    }
    else
    {
        neighbour.SetTopColor(Color.blue); //todo move code later
        queue.Enqueue(neighbour);
        print("Queueing " + neighbour);
    }

}

private void ExploreNeighbours(Waypoint from)
{
    if (!isRunning) { return; }

    foreach (Vector2Int direction in directions)
    {
        Vector2Int neighbourCoordinates = from.GetGridPos() + direction;
        try
        {
            QueueNewNeighbours(neighbourCoordinates);
        }
        catch
        {
            // do nothing
        }
    }
}

}

You are right. queue.Dequeue() removes the oldest element in the Queue and returns that object. When you assign the object to a variable, you keep a reference (“link”) to that object. Otherwise, the object reference will get lost. Depending on the case, it might be impossible to find the object again in the memory.

Since we want to use the Waypoint object to check its neighboughs, we need a reference. That’s what we do with queue.Dequeue(): getting a reference to a Waypoint object.


See also:

1 Like

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

Privacy & Terms