![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Magnus Winkel Peders |
I’m programming some navigations for a kinematic body 2d, and I suspect that the path (yielded by calling get_simple_path() on a navigation2d) is the empty array for whatever reason. When I want to check if the array is indeed empty the game crashes with the reason "Invalid get index ‘0’ (on base ‘Array’).
According to Godot’s debugger tool, the error is thrown in the second to last line in the attached.
path = Array(nav.get_simple_path(position, goal, false))
# Remove points on the path that make detours.
for i in range(path.size() - 1,0,-1):
if i == 0 or i == path.size() - 1:
continue
if path[i - 1].distance_to(goal) > path[i].distance_to(goal):
path.remove(i - 1)
if path.size() > 0: # Error triggered here according to debugger
(some code here that assumes that path is not an empty array).
I don’t get why the debugger would crash the game by asking for the size of the array. If anyone can help, that would be greatly appreciated
Well …get_simple_path returns PoolVector3Array according to doc
so…try
path :PoolVector3Array = nav.get_simple_path(position, goal, false)
see if it is working
lowpolygon | 2019-12-10 07:19
As mentioned in the post, I’m using a Navigation2D, and according to the godot documentation, (cf. Navigation2D — Godot Engine (3.1) documentation in English), get_simple_path should return a poolVector2Array, but fact is that it is empty, and for some reason, the compiler thinks it is a coding error.
I tried what you suggested and it didn’t work. Assigning path to be a PoolVector2Array did not work either.
Magnus Winkel Peders | 2019-12-10 18:07
oh yeah…I just did a quick search on the get_simple_path, and that’s what I got. Anyway, if I may suggest a really dumb way to test to loop, add
for i in range(path.size() - 1,0,-1):
if i == 0 or i == path.size() - 1:
continue
if path[i - 1].distance_to(goal) > path[i].distance_to(goal):
path.remove(i - 1)
print(path.size()) #Add this line
if it still crashes , it just mean something is wrong on the loop if not, at least you will know something else is going on. And you would have to comment out the line that would trigger the error . It is a dumb way, but give it a shot
lowpolygon | 2019-12-11 01:55