Wave system for a tower defense game

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dingadev

Hello everyone !

I’m trying to implement a wave system for a Tower Defense game and I’m having trouble setting it up to work correctly. I’ll try to explain what i’m trying to achieve and where I’m having struggle.

I attached a script to a WaveSpawner node. The script contains

1: The number of waves before winning (aka. waves_count)

Then each waves contains :
2: The number of enemy groups in each wave.

And each enemy group contains:
3: Number of enemies per group
4: The path to the enemy group resource for spawning enemies
5: The spawn interval between each enemy spawn

I think it’s all the data I need for the system to work properly. When all the enemies from an enemy group are dead, the next enemy group is spawned until there is no enemies in any enemy group. Then the wave is finished.

I would like these values to be exported so I can change the waves info easily on each map.

Now here’s the code :

extends Node2D
var wave = 0
@export var waves : Array = []
@export var spawn_group : Array[Spawn_info] = []
@export var total_waves : int = 0

@onready var timer = $timer
@onready var enemy_path = get_tree().get_first_node_in_group("EnemyPath")

 var time = 0

 var enemy_amount

 func check_waves_finished():
if wave != total_waves:
	_wave_setup()
else:
    	print(str("Victory!"))

   func _on_timer_timeout():
time +=1
var enemy_spawns = spawn_group
for i in enemy_spawns:
		if i.spawn_delay_counter < i.enemy_spawn_delay:
			i.spawn_delay_counter += 1
		else:
			i.spawn_delay_counter = 0
			var new_enemy = i.enemy
			var spawned_enemies_counter = 0
			while spawned_enemies_counter < i.enemy_number:
				var enemy_spawn = new_enemy.instantiate()
				enemy_spawn.global_position = enemy_path.global_position
				enemy_path.add_child(enemy_spawn)
				spawned_enemies_counter += 1
pass

1st problem: Currently, in the editor I have three parameters : the total number of waves, the spawn group arrays, and the waves arrays. I would like the spawn group to be inside the wave arrays, but I haven’t managed to do that.

2nd : problem : Some of the values don’t work, no matter how much I set the enemies number in a spawn group, it spawns infinite number of enemies. And if I spawn no enemies in the first spawn group, it doesn’t spawn enemies from the other spawn groups.

I’m trying to print debug these issues and to solve them by myself, but figured I could ask for help since I’m pretty sure it’s a relatively easy issue to solve, I just haven’t a way to do it by myself yet.

Thanks in advance for your help and sorry for the long post!