Invalid get index in code

Godot Version

4.1

Question

im getting an error on a piece of code

the piece of code navigation_area_2d.target_position = movement_targets.at_home_targets[0].position gets an error saying “Invalid get index ‘0’ (on base: ‘Array[Node2D]’).”

here are the relating scripts incase you need it to fix it

the script that the piece of code is in

extends Area2D

class_name Ghost

enum GhostState {
	SCATTER,
	CHASE,
	RUN_AWAY,
	EATEN
}

signal direction_change(current_direction: String)

var current_scatter_index = 0
var direction = null
var current_state: GhostState
var is_blinking = false


@export var speed = 65
@export var movement_targets: Resource
@export var tile_map: MazeTileMap
@export var image: CompressedTexture2D
@export var chasing_target: Node2D
@export var points_manager: PointsManager
@onready var update_chasing_target_position_timer = $updateChasingTargetPositionTimer
@onready var body_sprite = $theghost as BodySprite
@onready var eyes_sprite = $ghosteyes as EyesSprite
@onready var navigation_area_2d = $NavigationAgent2D
@onready var scatter_timer = $scatterTimer
@onready var run_away_timer = $runAwayTimer
@onready var points_label = $pointsLabel

func _ready():
	navigation_area_2d.path_desired_distance = 4.0
	navigation_area_2d.target_desired_distance = 4.0
	navigation_area_2d.target_reached.connect(on_position_reached)
	
	call_deferred("setup")
 



 
func _process(delta):
	if !run_away_timer.is_stopped() && run_away_timer.time_left < run_away_timer.wait_time / 4 && !is_blinking:
		start_blinking()
	move_ghost(navigation_area_2d.get_next_path_position(), delta)
 

func move_ghost(next_position: Vector2, delta: float):
	var current_ghost_position = global_position
	var new_velocity = (next_position - current_ghost_position).normalized() * speed * delta
	
	caculate_direction(new_velocity)
	position += new_velocity

func caculate_direction(new_velocity: Vector2):
	var current_direction
	
	if new_velocity.x > 1:
		current_direction = "right"
	elif new_velocity.x < -1:
		current_direction = "left"
	elif new_velocity.y > 1:
		current_direction = "up"
	elif new_velocity.y < -1:
		current_direction = "down"
		
	if current_direction != direction:
	
		direction = current_direction
		direction_change.emit(direction)


func setup():
	print(tile_map.get_navigation_map(0))
	navigation_area_2d.set_navigation_map(tile_map.get_navigation_map(0))
	NavigationServer2D.agent_set_map(navigation_area_2d.get_rid(), tile_map.get_navigation_map(0))
	scatter()
 
 
func scatter():
	scatter_timer.start()
	current_state = GhostState.SCATTER
	navigation_area_2d.target_position = movement_targets.scatter_targets[current_scatter_index].position
 
func on_position_reached():
	if current_state == GhostState.SCATTER:
		scatter_position_reached()
		speed = 65
	elif current_state == GhostState.CHASE:
		chase_position_reached()
		
	elif current_state == GhostState.RUN_AWAY:
		run_away_from_pacman()


func chase_position_reached():
	print("kill pacman")


func scatter_position_reached():
	if current_scatter_index < 3:
		current_scatter_index += 1
	else:
		current_scatter_index = 0
	navigation_area_2d.target_position = movement_targets.scatter_targets[current_scatter_index].position
	
	








func _on_scatter_timer_timeout():
	starting_chasing_pacman()

func starting_chasing_pacman():
	if chasing_target == null:
		print("no chasing target, chasing wont work obviously lol")
	current_state = GhostState.CHASE
	update_chasing_target_position_timer.start()
	navigation_area_2d.target_position = chasing_target.position


func _on_update_chasing_target_position_timer_timeout():
	navigation_area_2d.target_position = chasing_target.position


func run_away_from_pacman():
	if run_away_timer.is_stopped():
		body_sprite.run_away()
		eyes_sprite.hide_eyes()
		run_away_timer.start()
		speed = 60
	current_state = GhostState.RUN_AWAY
	
	# stop timers
	update_chasing_target_position_timer.stop()
	scatter_timer.stop()
	
	var empty_cell_position = tile_map.get_random_empty_cell_position()
	navigation_area_2d.target_position = empty_cell_position

func start_blinking():
	body_sprite.start_blinking()




func _on_run_away_timer_timeout():
	is_blinking = false
	eyes_sprite.show_eyes()
	body_sprite.move()
	starting_chasing_pacman()
	speed = 75
	

func get_eaten():
	body_sprite.hide()
	eyes_sprite.show_eyes()
	points_label.show()
	await points_manager.pause_ghost()
	points_label.hide()
	run_away_timer.stop()
	current_state = GhostState.EATEN
	navigation_area_2d.target_position = movement_targets.at_home_targets[0].position
	speed = 100


func _on_body_entered(body):
	var player = body as Player
	if current_state == GhostState.RUN_AWAY:
		get_eaten()
	elif current_state == GhostState.CHASE || current_state == GhostState.SCATTER:
		set_collision_mask_value(1, false)
		update_chasing_target_position_timer.stop()
		await points_manager.pause_ghost()
		player.die()
		body_sprite.hide()
		eyes_sprite.hide_eyes()
		scatter_timer.wait_time = 20
		scatter()

the script where the code is built from:

extends Resource

@export var scatter_targets: Array[Node2D]
@export var at_home_targets: Array[Node2D]

thank you

Have you made sure that the array is not empty aswell as movement_targets? Try this:

	print("Movement_targets: ", movement_targets)
	print("Array: ", movement_targets.at_home_targets)
	navigation_area_2d.target_position = movement_targets.at_home_targets[0].position

i think its not empty but ill still use that code

btw do i put that in the script that the error is happening on or the one where the code is built from?

@herrspaten waiting for a reply

put it in this method. Sorry i went to sleep afterwards :sweat_smile:

alright

but do i put it in here?:

extends Resource

@export var scatter_targets: Array[Node2D]
@export var at_home_targets: Array[Node2D]

no just use this method in your area2d script:

func get_eaten():
	body_sprite.hide()
	eyes_sprite.show_eyes()
	points_label.show()
	await points_manager.pause_ghost()
	points_label.hide()
	run_away_timer.stop()
	current_state = GhostState.EATEN
	print("Movement_targets: ", movement_targets)
	print("Array: ", movement_targets.at_home_targets)
	navigation_area_2d.target_position = movement_targets.at_home_targets[0].position
	speed = 100

And then check the output to see what gets printed

i tested out that code but it still gives me the error on the line navigation_area_2d.target_position = movement_targets.at_home_targets[0].position

You need to fill out the @export variables in the inspector. Your resource is lacking at_home_targets

oh okay

ok i filled it out

but the error still keeps occouring on navigation_area_2d.target_position = movement_targets.at_home_targets[0].position

Might be the wrong resource. Could you show your inspector for this Ghost object?

just checked and now i realized that i didnt fill out the at home targets
Captura de tela 2024-10-09 125213

isn’t movement_targets suppose to contain at_home_targets? This doesn’t line up with the Ghost script. What is contained in the “BLINKYmovementTarget” resource?

the scatter target and the at-home target
btw i fixed the error (i also filled out the at home targets box)