0 votes

I'm programming some navigations for a kinematic body 2d, and I suspect that the path (yielded by calling getsimplepath() 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

in Engine by (12 points)

Well ..getsimplepath returns PoolVector3Array according to doc
so..try

path :PoolVector3Array  = nav.get_simple_path(position, goal, false)

see if it is working

As mentioned in the post, I'm using a Navigation2D, and according to the godot documentation, (cf. https://docs.godotengine.org/en/3.1/classes/class_navigation2d.html#class-navigation2d-method-get-simple-path), getsimplepath 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.

oh yeah...I just did a quick search on the getsimplepath, 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

1 Answer

0 votes

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?

by (28,791 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.