Godot Version
4.2.1
Question
Hi everyone! I’m working on a tower defense game and am trying to make an enemy spawner. What I’d like to do is have it so that when the function is called, it repeats count
times, and after each spawned enemy, it will wait delay
seconds until spawning the next one. The function will only spawn one enemy though. I’m not sure if I’m using the for loops wrong which is probably the case because I don’t fully understand them. Anyone have any ideas? Here’s the code for reference:
extends Path2D
@onready var Rattata = preload("res://Scenes/Enemies/Rattata.tscn")
@onready var Spearow = preload("res://Scenes/Enemies/Spearow.tscn")
var waiting = false
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func SpawnEnemy(name, count: int, delay: float):
if waiting == false:
for i in range(0, count):
if i < count:
i = i + 1
add_child(name.instantiate())
waiting = true
print(i)
$Timer.wait_time = delay
$Timer.start()
print("timer on")
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
if Input.is_action_just_pressed("1"):
SpawnEnemy(Rattata, 3, 1.0)
print("Rattata spawned")
if Input.is_action_just_pressed("2"):
SpawnEnemy(Spearow, 100, 0.6)
print("Spearow spawned")
func _on_timer_timeout():
if waiting == true:
waiting = false
print("timer off")