How do the coodrinatelabler change color?

Hi my code is working but i dont understand how the coordinate labeler is changing color, can someone please explain?

its these two lines for the testing that do it but as i understand, they just set isExplored and isPath in the local neigbor list? how and when do the coordinate labeler get this info?
grid[neigborCoords].isExplored = true;
grid[currentSearchNode.coordinates].isPath = true;

The CoordinateLabeler is continuously running and checking what those values are and setting the colors accordingly.

but i still dont understand how coordinate labeler gets the data from these two lines, are they not a local dictionary in the pathfinder script?

grid[neigborCoords].isExplored = true;
grid[currentSearchNode.coordinates].isPath = true;

The CoordinateLabeler has a reference to the GridManager that it got through
gridManager = FindObjectOfType<GridManager>();.

The GridManager exposes the dictionary as a public property through
public Dictionary<Vector2Int, Node> Grid { get { return grid; } }

But the CoordinateLabeler accesses the data through GetNode() to retrive the node and read the data

Ok, can you explain how the isExplored and IsPath true values that are added to the grid dictionary that is in the pathfinder class is set in the grid dictionary that is in the GridManager class because thats wehere the CoordianteLabeler gets the value from but i dont understand how the grid dictionary in gridmanager got the true value from the pathfinder script

Sure.

The Pathfinder gets a reference to GridManager using FindObjectOfType<GridManager>() and keeps it in gridManager. Then it gets a reference to the grid that’s in the GridManager with grid = gridManager.Grid. Now it is sharing the grid dictionary with the grid manager and when it sets the values, it is setting it on the same dictionary that the grid manager holds.

Thank you for the help, now i understand. i was under the impression that
after this is run grid = gridManager.Grid that grid becomes its own item that has copied everything from gridManager.Grid

Dictionaries are reference types

Value types will create new instances and reference types will be shared. Normally classes are reference types, and structs are value types. Reference types mean you are passing a reference (address essentially) around and every change goes to the same ‘address’. Value types are passing the value instead.


Edit
for reference - in case you run into these terms in the future; Reference types are also called mutable types, and value types are immutable types

1 Like

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

Privacy & Terms