Invalid get index '0' (on base: 'PoolVector2Array'). (Complete Begginer

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Undeyapper812
    I am following a tutorial that is in 3.1 (I'm in 3.2.3) and his script worked perfectly and if it's an array why is the index 0 invalid 
    #Error is here Invalid get index '0' (on base: 'PoolVector2Array').

extends "res://Characters/NPCs/"

onready var navigation = get_tree().get_root().find_node("Navigation2D", true, false)
onready var destinations = navigation.get_node("Destinations")

var motion
var possible_destinations
var path

export var minimum_arrival_distance = 5
export var walk_speed = 0.5

func _ready():
	possible_destinations = destinations.get_children()

func _physics_process(delta):

func navigate():
	var distance_to_destination = position.distance_to(path[0]) #HERE
	if distance_to_destination > minimum_arrival_distance:

func move():
	motion = (path[0] - position).normalized() * (MAX_SPEED * walk_speed) # Max speed is in the inhereted script 

func update_path():
	if path.size() == 1 and $Timer.is_stopped():

func make_path():
	var new_destination = possible_destinations[randi() % possible_destinations.size() -1]
	path = navigation.get_simple_path(position, new_destination.position)

func _on_Timer_timeout():

One reason for the error message might be that the PoolVector2Array is empty - and therefore it doesn’t have any items you can index, so index 0 is invalid.

But it’s hard to tell from the code you provided. For example, WHICH LINE of your code actually produces the error message? That’s important information, so it would improve your question if you provided it.

archeron | 2021-02-18 23:49

:bust_in_silhouette: Reply From: Undeyapper812

Thank you I ended up settling with doing an if statement so that the function navigate only occurs when path.size() is larger than 0 and it stopped crashing

:bust_in_silhouette: Reply From: mauricio_marquessm

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.

Short Answer

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.

Long Answer

In this game we are trying to make a guard walk to a certain point in the map (a Position2D node).

Guard trying to walk to a destination point

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 path (PoolVector2Array) is empty.

At function 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:
Floor2 has navigation

Floor 2 has navigation

Floor1 doesn't

Floor1 doesn’t

So, my guard NPC couldn’t walk where Floor1 was placed in the map.
I hope this helps.