Is there a better way to make waves?

Godot Version

v4.2.2.stable.official [15073afe3]

I’m working on a wave-based top down game. I started this awhile ago and just getting back in to it. My issue is the wave system I came up with. Here is the code attached to the root node on the main scene:

extends Node2D

var player_score = 0

var has_been_called = false

@onready var ghost = preload("res://ghost.tscn")

var ghosts_on_screen = 0

var wave_number = 0


func wave():
	var spawn_ghost = load("res://ghost.tscn").instantiate()
	spawn_ghost.position.x = randi_range(0, 1280)
	spawn_ghost.position.y = randi_range(0, 720)
	add_child(spawn_ghost)
	print("working")
	print(ghosts_on_screen)
	
func _process(delta):
	if ghosts_on_screen <= 0 and has_been_called == false:
		wave_number += 1
	
	if !has_been_called and wave_number == 1: #wave 1
		wave()
		wave()
		wave()
		has_been_called = true
		
	if !has_been_called and wave_number == 2: #wave 2
		wave()
		wave()
		wave()
		wave()
		wave()
		wave()
		has_been_called = true

Is there a better way to make waves?

You can use a for loop to reduce repeating code.

# wave 1
for i in range(3):
   wave()

# wave 2
for i in range(6):
    wave()

You can use an array to store data about each wave too.

var waves: Array[int] = [3, 6, 9, 11, 32, 398]

func spawn_wave():
    for i in range(waves[wave_number]):
        wave()
    wave_number += 1
1 Like

Sorry for the late reply, I never really used or knew that much about loops. I used arrays once for a scrapped object and that’s about it.

Anyway It’s still not spawning the next wave and here is my current script:

extends Node2D

var current_wave: Array[int] = [1, 2, 3]

var player_score = 0

@onready var ghost = preload(“res://ghost.tscn”)

var ghosts_on_screen = 0

var wave_number = 1

func _ready():
spawn_wave()

func wave():
var spawn_ghost = load(“res://ghost.tscn”).instantiate()
spawn_ghost.position.x = randi_range(0, 1280)
spawn_ghost.position.y = randi_range(0, 720)
add_child(spawn_ghost)
print(“working”)
print(ghosts_on_screen)

func spawn_wave():
for i in range(current_wave[wave_number]):
wave_number += 1
wave()

Am I missing something?

In my example, incrementing wave_number came after the entire for loop, once it was finished calling each wave. This is denoted by less indentation.

func spawn_wave():
    for i in range(waves[wave_number]):
        wave()

    # after for-loop, not part of it.
    wave_number += 1

Your pasted code is ill-formatted but I know it cannot be correct because wave_number += 1 is before wave() and must be at least part of the for loop.

Use the </> button to add three ticks and paste like so

```
type or paste code here
```

Sorry about the formatting I clicked the wrong button:

extends Node2D

var current_wave: Array[int] = [1, 2, 3]

var player_score = 0

@onready var ghost = preload("res://ghost.tscn")

var ghosts_on_screen = 0

var wave_number = 1

func _ready():
	spawn_wave()

func wave():
	var spawn_ghost = load("res://ghost.tscn").instantiate()
	spawn_ghost.position.x = randi_range(0, 1280)
	spawn_ghost.position.y = randi_range(0, 720)
	add_child(spawn_ghost)
	print("working")
	print(ghosts_on_screen)
	
func spawn_wave():
	for i in range(current_wave[wave_number]):
		wave()

	wave_number += 1

Is this better? please keep in mind that I’m just a beginner and I have only some idea of what I’m doing.

that looks correct! It will spawn every enemy at the same time, If you want a delay between enemies we can use the await keyword and create a timer

func spawn_wave():
	for i in range(current_wave[wave_number]):
		wave()
		# wait half a second
		await get_tree().create_timer(0.5).timeout

	wave_number += 1