Godot Version
4.2.1
Question
The problematic code:
extends State
class_name idle_calm
#When in this state, NPC would wander around it's spawn point with low speed
@export var target: CharacterBody2D
var search_range : int = 200
var wander_timer : float
func enter():
await target.navigator
target.entitySpeedCurr = 30
new_point()
func new_point():
var move_direction : Vector2
move_direction = target.spawn_point + Vector2(randf_range(-1, 1), randf_range(-1, 1)).normalized() * randf_range(30, search_range)
target.navigator.target_position = move_direction
wander_timer = 10
Within ‘target’ code:
@onready var navigator : NavigationAgent2D = $NavigationAgent2D
On line:
target.navigator.target_position = move_direction
It gives error:
Invalid set index 'target_position' (on base: 'Nil') with value of type 'Vector2'.
From what I understand, it is because this line runs before navigator
is properly initialized. Shouldn’t await target.navigator
have solved this issue?
If I try using await target.navigator.target_position
instead, it says:
Invalid get index 'target_position' (on base: 'Nil').
How can I solve it?