I think we are taking the same course. Are you making a tense top-down stealth game too?
I ran into this same problem, but the if condition didn't make it work. After some searching I found the reason behind this error.
navigation.get_simple_path() is returning an empty array because it can't find a way to reach the destination point from the starting point.At some point, the code tries to get the element at index 0 of this empty array and that's when the error occurs.
For me, this happened because that I screwed up the tile map. I forgot to add navigation in some of the floors tiles I was using.
In this game we are trying to make a guard walk to a certain point in the map (a
We get the guard position, and the position of this destination point the guard needs to walk to.
navigation.get_simple_path() tries to generate a new path from the guard's position (
position) to some destination point (
new_destination.position). If it can't find a valid way between those two points, then
navigation.get_simple_path(position, new_destination.position) will return an empty array and the variable
path will have that as a value.
So, just like @archeron said, the error occurs because the variable
PoolVector2Array) is empty.
navigate we try to get the first element of
path, resulting in an error.
In my case, the error was because I was trying to make tiles for 2 types of floors, in one of them, I did everything right, but for the other type of floor, I forgot to add navigation:
Floor 2 has navigation
So, my guard NPC couldn't walk where Floor1 was placed in the map.
I hope this helps.