Using the GetNode() method versus making a dictionary reference

I came up with a different way to do what Gary did in the lecture that I think is pretty good. It differs in that Gary made a reference variable using the dictionary (did I say that right?) and searched using a key, whereas I used the GetNode() method through the gridManager reference made earlier. Why would someone choose one way versus the other? Does it matter much at all? My code is below; thanks for your time in helping me understand.

P.S. There seems to be some weirdness of the formatting of my code, sorry if it’s difficult to read.

> 
> using System.Collections;
> using System.Collections.Generic;
> using UnityEngine;
> 
> public class PathFinder : MonoBehaviour
> {
>     [SerializeField] Node currentSearchNode;
>     Vector2Int[ ] directions = { Vector2Int.right, Vector2Int.left, Vector2Int.down, Vector2Int.up };
>     GridManager gridManager;
>     //Dictionary<Vector2Int, Node> grid = new Dictionary<Vector2Int, Node>();
>     private void Awake()
>     {
>         gridManager = FindObjectOfType<GridManager>();
>         
> 
>     }
>     void Start()
>     {
>         ExploreNeighbors();    
>     }
> 
>     void ExploreNeighbors()
>     {
>         
>         List<Node> neighbors = new List<Node>();
>         
> 
>         foreach (Vector2Int direction in directions)
>         {
>             Vector2Int neighborCoordinates = currentSearchNode.coordinates + direction;
> 
>             if (gridManager.GetNode(neighborCoordinates) != null)
>             {
>                 Node nodeToBeAdded = gridManager.GetNode(neighborCoordinates);
>                 neighbors.Add(nodeToBeAdded);
> 
>                 //TODO: Remove after testing
>                 nodeToBeAdded.isExplored = true;
>                 nodeToBeAdded.isPath = true;
>             }
>             
>         }
>    
>     }    
> 
> 
> }

Hi Michael,

Good job on challenging yourself. I’ve moved your thread to our Show forum to allow other people to discuss your code (and maybe also their own solutions).

I edited your post and formatted your code by highlighting it and by pressing the </> button. I think it is more readable now. :slight_smile:


See also:

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

Privacy & Terms