Hi! Deadlines for my project are coming in close and I can’t seem to figure this out. How can I make my enemies spawn in after one dies and the timer ends?
CODE:
extends CharacterBody2D
const SPEED = 400
const gravity = 100
var direction = 1
var object_scene = preload(“res://scenes/enemies/chicken.tscn”) @onready var timer = $“…/Timer”
func _on_hitbox_body_entered(body):
if body.is_in_group(“bullet”):
queue_free()
timer.start()
func reverse_direction():
if is_on_wall():
direction = -1
func add_gravity(delta):
if not is_on_floor():
velocity.y += gravity * delta
func _on_hitbox_area_entered(area):
if area.is_in_group(“swap”):
direction = 1
How would I do so? I tried this but that didn’t work either.
CODE:
extends Timer
@onready var chicken = $"../chicken"
@onready var chicken_instantiate = preload("res://scenes/enemies/chicken.tscn")
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
if chicken.queue_free():
start()
func _on_timeout():
stop()
add_child(chicken)
chicken_instantiate.instantiate()
var enemy_position = Vector2(-248, 352)
chicken_instantiate.enemy_position
chicken was just deleted, so this variable is now invalid, presumably it was already a child.
This creates a new instance, but without being assigned to a variable the new instance is lost forever. You probably want to use
var new_chicken = chicken_instantiate.instantiate()
And now you could modify the new chicken.
The second statement has no effect, you need to assign the chicken’s enemy_position (or probably just position?). The first line create a unrelated variable with the same name, the two do not interact in any way.
In total I would recommend these changes to your timer/spawner script.
extends Timer
@onready var chicken: Node2D = $"../chicken"
const chicken_instantiate: PackedScene = preload("res://scenes/enemies/chicken.tscn")
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
chicken.tree_exiting.connect(start)
func _on_timeout():
stop()
# assign `chicken` to a new chicken isntance
chicken = chicken_instantiate.instantiate()
# set new chicken's position
chicken.position = Vector2(-248, 352)
# start timer again when the new chicken is freed
chicken.tree_exiting.connect(start)
# add new chicken as a child
add_child(chicken)