Problem with Array.size()

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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

:bust_in_silhouette: Reply From: Zylann

If the returned array is empty, your for loop will have a wrong range:

for i in range(path.size() - 1, 0, -1):

Will be:

for i in range(-1, 0, -1):

Also I see you are removing elements from the array while you are iterating on it. That can work, but make sure you know what you are doing.

if path.size() > 0: # Error triggered here

The error you quoted doesn’t happen here, there is no way. Maybe the debugger reports the wrong line?

You are correct in your first observation, but I think that range(-1,0,-1) returns an empty list of integers, so the for loop would be skipped. I think I know what I’m doing with the for loop. I want to remove elements, but as I do so, the indices will change, unless I use a reverse for loop. I agree that the debugger could reference the wrong line, but it makes me a bit angry that it’s incorrect.

Magnus Winkel Peders | 2019-12-10 17:52