Enemy Spawner for Tower Defense Game

Godot Version



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
				waiting = true
				$Timer.wait_time = delay
				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")

What up i answered ur question on reddit

Thanks, I didn’t mean to ask in so many places I was just unsure of response time

In general wait a few hours or maybe a day before cross posting. As for response times - reddit is usually within a few hours, while the forum is usually within a day or two (but often has more detailed answers, and is better suited for complex questions)

1 Like