Array in order

How is the array when we find it with the “Path” tag directly in the right order the enemy has to follow?

Hi,

Welcome to our community! :slight_smile:

A list contains elements in a sorted order. When we add elements to the list with the Add() method, the elements get “stacked”. The first element we add is at index 0, the second element at index 1, the third element at index 2, and so on.

Then we iterate over the list starting at index 0, which returns the first element, which is our starting point.

Is this what you wanted to know?


See also:

Thanks for the reply!

But i still have one question. We used GameObject[] waypoints = GameObject.FindGameObjectsWithTag("Path"); to add all the objects in our world wich got the Path tag on them to a Array called waypoints. Then we use a foreach loop to add waypoint.GetComponent<Waypoint>() to our path array. But were in the code do we sort it so that the code knows that we want to go to a specific tile tagged with the Path tag? Because earlier we dragged it in order in the array with a [SerializeField].

Also your amazing Nina! If i have a question or someone else you always respond fast and very clearly!

Admittedly, Gary’s solution is fairly complex, so it might be that I did not understand your question correctly because we are working with “path” in multiple places.

In the ExploreNeighbors method in the Pathfinder class, we iterate over the neighbouring tiles of the “current tile”. And we have the following line: neighbor.connectedTo = currentSearchNode;

Each Node object has got a connectedTo field. To this field, we assign another Node object. And that other Node object has got a connectedTo field too. This way, we create a chain. A chain contains elements in a fixed order.

In the BuildPath method, we have these two lines of code:

currentNode = currentNode.connectedTo;
path.Add(currentNode);

We basically uncoil the aforementioned chain and add each element to our path list.

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

Privacy & Terms