Some Help with an Enemy Wave Spawner

Godot Version

4.3.3

Question

Hello there, thank you for taking a bit of Your time to read this but I hit a small wall in the Enemy Wave Spawner that I am trying to create. Managed to fix up my arrays and now the only issue that I somehow can’t get around is with the spawning, more importantly the level for the spawner.
I managed to somehow stich the code together by experimenting a lot in order to get it to work for the 4 waves I have and it’s good the enemies spawn correctly for the array and the HUD correctly shows the given wave. The problem I have is in the code since on some parts the Current_Level becomes null basically while the code continues to function.

Starting from my Wave Spawner I have this button here that when pressed gets the info from the Wave and WaveInfo and spawns the enemies.
My code works by extending from WaveInfo>Wave>WaveSpawner and each of them work on their own part.

func _on_start_wave_button_pressed():  # When We press the start Wave Button 
	if not wave_in_progress: # If the Wave isn't in progress 
		var waves = get_waves_for_level(current_level) # get waves for the current level 
		var total_waves = waves.size()  # Get the total number of waves for the current level
		wave_started.emit(current_wave, total_waves)  # Emit the current wave and total waves as integers
		print("Current Wave: %d, Total Waves: %d" % [current_wave, total_waves])
		spawn_enemies_for_current_wave()  # spawn the enemies for the current wave
		print("THIS IS THE CURRENT LEVEL BEFORE NEXT WAVE" ,current_level)
		next_wave()  # Start the Next Wave Function
		print("THIS IS THE CURRENT LEVEL AFTER NEXT WAVE" ,current_level)

The second part is the Wave which has the NextWave function

func next_wave():
	print("THIS IS THE CURRENT LEVEL", current_level)
	var level_key = get_current_level()   # GET THE CURRENT LEVEL VARIABLE 
	print("ANOTHER LEVEL", level_key)
	if wave_data.has(level_key):    # We check if the WAVE DATA has the LEVEL
		var waves = wave_data[level_key]  # Variable Waves and we check the Data for the LEVEL
		if current_wave < waves.size():   # IF the Current Wave we check the Wave Size
			current_wave += 1   # WE ADD +1 To the Current Wave
		else:
			print(" YOU FINISHED THE WAVES FOR THE LEVEL")  # When we finish the waves for the level
			current_wave = 1  # WE RESET THE CURRENT WAVE TO 1 SO WE CAN SWITCH LEVELS
	else:
		print("No more levels available") # WE GET THIS MESSAGE IF THE LEVEL IS NOT FOUND 

and also the Current_Level set

func set_current_level(): # We use the signal in the manual ready to set the current level
	current_level = GameManager.level_set # make the current level equal to the GameManager - level set
	print(" THE LEVEL IS LOADED ", current_level)

and the last part is the GameScene which handles the mechanics and communication with the HUD

func _on_enemy_death(value : int):  # When an enemy Dies
	add_gold(value)   # We add gold to the player 
	active_enemies -= 1  # decrease the active enemies
	enemy_number_updated.emit(active_enemies)
	if active_enemies <= 0:  # we check if the active enemies are below 0
		_on_wave_completed()   # start the wave completed function
		
# ==============================================================================
# Wave Functions
# ==============================================================================
func _on_wave_started(current_wave: int, total_waves: int):
	self.total_waves = total_waves # Set the total_waves in the GameScene
	self.current_wave = current_wave # Set the current_wave in the GameScene
	wave_updated.emit(current_wave - 1, total_waves) # WaveUpdate Signal the current wave has a - 1 to corretly send it to the HUD

func _on_wave_completed():  # ON WAVE COMPLETE
	wavespawner.wave_in_progress = false # When The Wave is complete we set the Wave In Progress to False
	if current_wave < total_waves:   # We check the current Wave to the number of total waves
		#current_wave += 1  # we add +1 to the wave counter # Need to figure out the issue with the level
		#level_placeholder.next_wave() # we start it from the button press
		wavespawner._disable_start_wave_button(false) # WAVESPAWNER BUTTON RE-ENABLING
		emit_signal("wave_updated", current_wave, total_waves)  # UPDATE THE WAVE
		print("Wave %d completed. Ready for wave %d." % [current_wave, current_wave + 1])
		print("The Current Level: ", current_level)
	else:
		print("All waves completed!")

I tried the whole day to figure out the issue where the Current_Level somehow reset at the point where we pressed the button and I couldn’t figure out what could be the issue. But I removed it and still there was a part of the code that didn’t read the current_level, especially in the GameScene in the WaveComplete function.
One thing that I thought could be the issue was the Wave.new that was added to the spawner in the ready.

func _ready() -> void:
	print("WaveSpawner _ready() called")
	wave_instance = get_parent().get_parent()
	#wave_instance = Wave.new()
	wave_instance._manual_ready()  # Call a custom method to simulate _ready()
	print("THIS IS THE READY SPAWNER CURRENT LEVEL: ", current_level)

The funny thing is that now somehow it works, like it starts from the first wave till the forth and it increments it correctly just the way I wanted but some of the prints I have don’t print out the Current_Level and I don’t know if that could be an issue further along.
These are all of the code functions that hold the Current_Level code and the GameManager which at the start just sets it to the current level. Any help is appreciated and also if You have some suggestions on improving this please do tell, have been working on this for a couple of days now but since I am new with Godot I am open to any advice. Thanks again!

Could you upload your project to github or a zip on google drive or something similar so I can have a look for you.

I just saw the reply after a while and thank you, but I managed to find the issue which was staring me in the face the whole time…

#wave_instance = Wave.new()

It turned out the current_level that printed out null wasn’t being set due to getting the new wave_instance variable which wasn’t being set as I mentioned before, but also since the array starts from 0 I needed to make some prints with current_wave + 1 in order to get the exact prints and it worked. I just went from the start of the code and followed the flow until I got to the issue. Took me a whole 2 days to get this right.

1 Like