Attention | Topic was automatically imported from the old Question2Answer platform. | |
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/PlayerDetection.gd"
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():
randomize()
possible_destinations = destinations.get_children()
make_path()
func _physics_process(delta):
navigate()
func navigate():
var distance_to_destination = position.distance_to(path[0]) #HERE
if distance_to_destination > minimum_arrival_distance:
move()
else:
update_path()
func move():
look_at(path[0])
motion = (path[0] - position).normalized() * (MAX_SPEED * walk_speed) # Max speed is in the inhereted script
move_and_slide(motion)
func update_path():
if path.size() == 1 and $Timer.is_stopped():
$Timer.start
else:
path.remove(0)
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():
make_path()
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