Not understanding the script

I can’t understand this script, mostly the exploreneighbors method, could someone explain me?

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

public class PathFinder : MonoBehaviour
{
    [SerializeField] Node currentSearchNode;
    Vector2Int[] directions = {Vector2Int.right, Vector2Int.left, Vector2Int.up, Vector2Int.down };
    GridManager gridManager;
    Dictionary<Vector2Int, Node> grid;

    private void Awake()
    {
        gridManager = FindObjectOfType<GridManager>();
        if (gridManager != null)
        {
            grid = gridManager.Grid;
        }
    }

    void Start()
    {
        ExploreNeighbors();
    }

    void ExploreNeighbors()
    {
        List<Node> neighbors = new List<Node>();
        foreach(Vector2Int direction in directions)
        {
            Vector2Int neighborsCords = currentSearchNode.coordinates + direction;
            if (grid.ContainsKey(neighborsCords))
            {
                neighbors.Add(grid[neighborsCords]);

                //to do: remove after testing
                grid[neighborsCords].isExplored = true;
                grid[currentSearchNode.coordinates].isPath = true;
            }
        }
    }

}

Hi tudimaxi,

Welcome to our community! :slight_smile:

Admittedly, pathfinding algorithms are not easy to grasp. Have you already rewatched the video at least one more time?

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

public class PathFinder : MonoBehaviour
{
    [SerializeField] Node currentSearchNode;
    Vector2Int[] directions = {Vector2Int.right, Vector2Int.left, Vector2Int.up, Vector2Int.down };
    GridManager gridManager;
    Dictionary<Vector2Int, Node> grid;

    private void Awake()
    {
        gridManager = FindObjectOfType<GridManager>();
        if (gridManager != null)
        {
            grid = gridManager.Grid;
        }
    }

    void Start()
    {
        ExploreNeighbors();
    }

    void ExploreNeighbors()
    {
        List<Node> neighbors = new List<Node>(); 
        //Initializes a list of nodes and assigns it to local variable neighbours
        foreach(Vector2Int direction in directions) //goes through each direction in directions
        {
            Vector2Int neighborsCords = currentSearchNode.coordinates + direction; 
            //assigns current direction vector2 coordinates to neighborsCords
            if (grid.ContainsKey(neighborsCords)) // Checks if direction coordinate is in gridManager
            {
                neighbors.Add(grid[neighborsCords]); //If it is adds it to the list of nodes in neighbours

                //to do: remove after testing
                grid[neighborsCords].isExplored = true; 
                grid[currentSearchNode.coordinates].isPath = true;
            }
        }
    }

}


Yes, I watched, but this morning when I watched the third time, I understood. Thanks for helping😀.
I think the problem was just the tired me.

Thank you very much. With your help I understood a little more better after I watched a few more times.

I’m glad rewatching and reading @Willrun’s explanation helped. And yes, when you are tired, learning new things becomes difficult. My personal advice is: Don’t try to understand concepts while being tired because that’s usually a waste of time. Learn when you are not tired. :slight_smile:

2 Likes

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

Privacy & Terms