Invalid Get Index: '1' on base: Array

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Nizil

hi, can anyone help me?
I have a little problem, for some reason it throws an error when I try to use get_simple_path() in the generate_path() function. The path, for some reason, is not written to the array and gives an error when used in the navigate() function.

extends KinematicBody2D

export var mob_speed = 200
var velocity = Vector2()
var path : Array = []
var navigation : Navigation2D = null
var target = null

func _ready():
	navigation = get_tree().current_scene.get_node("Navigation")


func _physics_process(delta):
	initiate_target()
	generate_path()
	navigate()
	chase()

func initiate_target():
	if get_tree().current_scene.get_node_or_null("Player/HitBox") != null:
		target = get_tree().current_scene.get_node("Player/HitBox")
	else:
		target = null
		
func navigate():
	velocity = global_position.direction_to(path[1]) * mob_speed
	if position == path[0]:
			path.pop_front()

func generate_path():
	if navigation != null and target != null:
		path = navigation.get_simple_path(position, target.global_position, false)


func chase():
	velocity = move_and_slide(velocity)
:bust_in_silhouette: Reply From: SteveSmith

You’re removing entries from path, but you’re not checking if there’s none left.

BTW, according to the docs, get_simple_path is deprecated.

Thank you so much!

Nizil | 2022-11-14 17:27