Assign var "target_to_chase" through code

Godot Version

4.5

Question

Hello I’m trying to make a enemy spawner but i don’t know how to assign my “target_to_chase” through code

this is my enemy code


@onready var sprite: Sprite2D = $Sprite2D
@onready var timer: Timer = $Enemies_Area2D/Timer
@onready var navigation_agent: NavigationAgent2D = $NavigationAgent2D
@export var target_to_chase: = CharacterBody2D
var health = 100

const SPEED = 200.0


func _ready() -> void:
	set_physics_process(false)
	call_deferred("wait_for_physics")

func wait_for_physics():
	await get_tree().physics_frame
	set_physics_process(true)

func _process(delta: float) -> void:
	if health <= 0:
		queue_free()



func _physics_process(delta: float) -> void:
	if navigation_agent.is_navigation_finished() and\
		target_to_chase.global_position == navigation_agent.target_position:
		return
	navigation_agent.target_position = target_to_chase.global_position
	velocity = global_position.direction_to(navigation_agent.get_next_path_position()) * SPEED 
	move_and_slide() 



func _on_enemies_area_2d_area_entered(area: Area2D) -> void:
	if area.name == ("Enemy_Killzone"):
		sprite.modulate = Color.RED
		health -= 20
		timer.start()
		


func _on_timer_timeout() -> void:
	sprite.modulate = Color.WHITE

this is my spawner code

extends Node2D


const ENEMY = preload("res://Scenes/Enemies/enemy_test.tscn")

var time_in_seconds = 10

func _ready() -> void:
	while time_in_seconds < 11:
		await get_tree().create_timer(time_in_seconds).timeout
		var enemy_instiance = ENEMY.instantiate()
		get_tree().root.add_child(enemy_instiance)
enemy_instiance.target_to_chase = target

Btw that while shouldn’t be there.

It keeps giving me an error saying “Identifier ‘target’ not declared in the current scope.”
And I tried to change it to “enemy_instiance.target_to_chase = PLAYER”. Here is my code
(Also the while loop was to make it loop infinitely.)

extends Node2D

const PLAYER = preload("res://Scenes/player.tscn")
const ENEMY = preload("res://Scenes/Enemies/enemy.tscn")

var time_in_seconds = 10

func _ready() -> void:
	while time_in_seconds < 11:
		await get_tree().create_timer(time_in_seconds).timeout
		var enemy_instiance = ENEMY.instantiate()
		get_tree().root.add_child(enemy_instiance)
		enemy_instiance.target_to_chase = PLAYER

Please tell me anything i’m doing wrong.

Your PLAYER constant is a reference to the player’s scene, but you need a reference to the player scene’s instance. If the spawner and the player are inside the same scene, you could just export a variable and set the reference to the player in the editor (like you apparently did in the enemy scene before).

Well target in my example was obviously a placeholder for whatever your target is. Not to be literally copypasted. You need to assign a valid reference to the node you want to target there.

Man I’m so stupid xDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
all I had to do was to @onready var player: CharacterBody2D = $"../Player"
and then just enemy_instiance.target_to_chase = player XDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD