How to find index of a specific node in a path of nodes?


Hi there! I am trying to figure out how to get a certain index from a path list of nodes. Say the path is 20 nodes but the enemy AI can only move 5 nodes in one turn. I am trying to figure out how to get the 5th index of the path list.

I tried IndexOf but it says cannot convert int to mapnavkit.mapnavnode.

//pseudo code, path exists from the enemy to the player

public moveableNode MapNavNode;

void ChooseAction()
moveableNode = path.IndexOf(path[5]); // it doesn’t like this part and I am stumped 0_o

I know I am probably way off but any help would be a huge boost for me, thank you Leslie!

(Leslie Young) #2

The Path function returns a List of node indices or nodes (depending on which variation of the function you used). If you want the 5th you simply do myList[4] (4 since indexing starts at 0).

So, something like this

List<MapNavNode> path = Path<MapNavNode>(....);
MapNavNode node = path[4];

Of course you need to make sure there are 5 or more entries in path before trying to access index 4 since you will get an exception error if there are fewer entries. path.Count


Cool it works great, thank you very much as always. Hope to show my project to all of the map navers out there once I make a bit more progress!