Unable to figure out why the signal isn't being received correctly

Godot Version

4.3

So, I have a area where, when the player enters, it instances 3 enemies and has a variable that ticks down with each one dead, at 0 left spawning a key to let the player leave. I am doing so by having the enemy emit a signal on death and having the area connect to the signal to trigger the function. Those all seem to work, but it never runs the function itself. Maybe I made a mistake with code? I’m not too sure and I’m a bit new to GDScript so maybe I missed something obvious.

Enemy Script:

func _on_hurtbox_area_entered(area: Area2D) -> void:
	if area.is_in_group("Attacks"):
		health_amount -= area.damage_amount
	if area.get_parent().has_method("get_damage_amount"):
		var node = area.get_parent() as Node
		health_amount -= node.damage_amount
	if health_amount <= 0:
		enemy_killed.emit("enemy_killed")
		var enemy_death_effect_instance = enemy_death_effect.instantiate() as Node2D
		enemy_death_effect_instance.global_position = global_position
		get_parent().add_child(enemy_death_effect_instance)
		queue_free()

fight_start area script:

func fight_start():
	if hasFightStarted == false:
		hasFightStarted = true
		collision_shape_2d.set_deferred("disabled", false)
		for spawn_lightning in 3:
			var lightning_ghost_instance = lightning_ghost.instantiate() as Node2D
			if spawn_lightning == 0:
				lightning_ghost_instance.position.x = 1360
				lightning_ghost_instance.position.y = -96
			elif spawn_lightning == 1:
				lightning_ghost_instance.position.x = 1552
				lightning_ghost_instance.position.y = -96
			elif spawn_lightning == 2:
				lightning_ghost_instance.position.x = 1456
				lightning_ghost_instance.position.y = -160
			get_parent().add_child(lightning_ghost_instance)
			lightning_ghost_instance.connect("enemy_killed", Callable(self, "on_enemy_killed"))
			await get_tree().create_timer(1.0).timeout
	else:
		return

func on_enemy_killed():
	enemiesLeft -= 1
	print("Enemies left:", enemiesLeft)
	
	if enemiesLeft == 0:
		player_camera_2d.limit_left = 32
		player_camera_2d.limit_right = 2304
		player_camera_2d.limit_top = -400
		player_camera_2d.limit_bottom = 0
		collision_shape_2d.set_deferred("disabled", true)
		spawn_key()

Essentially I know that its not triggering the on_enemy_killed function because it’s debug message isnt playing. But earlier I had a debug message for both emitting the signal in the enemy and the connection being successful in the fight_start script. I have no idea why it’s not emitting correctly

You are trying to connect a callable enemy_killed to an instance lightning_ghost_instance, it’s not a signal, you can’t do that.

lightning_ghost_instance.connect("enemy_killed", Callable(self, "on_enemy_killed"))

Do you have any signals in your “Lightning Ghost” script that you can connect to?

Here’s a neat article about using signals, that may nudge you in the correct direction in case you’re stuck:

i have the signal enemy_killed in the lightning_ghost script, and that is what is being emitted in the first code block of my post

Ah, ok, sorry, I misread this piece of code. You can use a better version of the connect function, which is type-safe and easier to read.

lightning_ghost_instance.enemy_killed.connect(on_enemy_killed)

The issue is probably because you have included a string parameter within your signal.emit() call, that the receiving callable is not expecting.

# includes a string parameter
enemy_killed.emit("enemy_killed")

# does not include a string parameter
func on_enemy_killed():

I believe you don’t need that string in the emit() function, just remove it.

enemy_killed.emit()

thank you! this worked !

1 Like

Great! Mark my previous response as a solution to the problem, so that others can reference it in case they ever encounter a similar problem.