Multiple PathFinders?

I’ve been trying to have multiple enemy bases so that more than one stream of enemies can try to attack the friendly base but have been having trouble accomplishing this. What happens is that when I have multiple pathfinders, one of the bases pulls data from the other base and they go on the same path. Is it possible to have more than one pathfinder the way that it is setup right now?

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

public class PathFinder : MonoBehaviour
{

    Dictionary<Vector2Int, CheckPoint> grid;
    Queue<CheckPoint> checkPointQueue = new Queue<CheckPoint>();
    public List<CheckPoint> path = new List<CheckPoint>();

    [SerializeField] CheckPoint start;
    [SerializeField] CheckPoint end;


    bool queueIsRunning = true;
    CheckPoint searchBase;

    Vector2Int[] directions = {

        Vector2Int.up,
        Vector2Int.down,
        Vector2Int.right,
        Vector2Int.left


    };


    // Start is called before the first frame update

    void Start()
    {

        grid = FindObjectOfType<GridGetter>().ReturnGrid();

    }

  

    public void MakePath()
    {
 
        path.Add(end);
        end.isUsable = false;
        

        while (path[path.Count - 1] != start)
        {
            CheckPoint nextPoint = path[path.Count - 1].exploredFrom;
            nextPoint.isUsable = false;
            path.Add(nextPoint);

        }

        path.Reverse();

        
    }

    public List<CheckPoint> GetPath()
    {
        if(path.Count == 0)
        {

            BreadthFirstSearch();
            MakePath();

        }
        
        return (path);

    }

    public void BreadthFirstSearch()
    {
        checkPointQueue.Enqueue(start);
        while (checkPointQueue.Count > 0 && queueIsRunning)
        {

            searchBase = checkPointQueue.Dequeue();//dequeue deletes first thing from queue
            CheckIfEndFound();
            FindNeighbors();
            searchBase.isExplored = true;
        }

    }

    private void CheckIfEndFound()
    {

        if (searchBase == end)
        {
            print("Found end.");
            queueIsRunning = false;

        }


    }

    private void FindNeighbors()
    {

        if (!queueIsRunning) { return; }

        

        foreach (Vector2Int direction in directions)
        {

            Vector2Int neighborCoords = searchBase.GetSnapLocation() + direction;

            if (grid.ContainsKey(neighborCoords))
            {

                QueueNeighbor(neighborCoords);

            }
        }
    }

    private void QueueNeighbor(Vector2Int neighborCoords)
    {

        CheckPoint neighborBlock = grid[neighborCoords];

        if (neighborBlock.isExplored || checkPointQueue.Contains(neighborBlock))
        {
        }
        else { 

            

            checkPointQueue.Enqueue(neighborBlock);

            neighborBlock.exploredFrom = searchBase;


        }
        
    }




}

That is my pathfinder script^^^^, how can I make it so both bases don’t pull the same data off the pathfinder. I’ve even gone as far to create another script that counts how many enemyspawners there are and makes that amount of pathfinders and sets a pathfinder to an enemy spawner, however that seems very complicated.

Hi Anish,

Since there are not any static variables in your PathFinder, and since your PathFinder is not a “singleton”, you should be able to simply assign it to your individual bases. Add the respective waypoints to start and end. Last but not least, assign your PathFinder objects to the respective enemy spawners.

FindObjectOfType will not work. Your enemy spawners need their PathFinder object.

I figured out why two pathfinders didn’t work for me. I thought that the pathfinder wasn’t a singleton like you said so I was weirded out when it didn’t work. I figured out in the pathfinder that when you set a block to not usable nothing can use it. So the pathfinder couldn’t find a path for one of the bases because the other base already took it. Thanks a lot!

I’m glad you were able to make your idea work. :slight_smile:

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