![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | BillRogers |
Howdy,
I was working on a level script to respawn enemies once one dies. Each enemy would have an on_death signal which should emit to the level. Since each enemy is instantiated in the level scene, I figured I’d have to connect the signal to the level script each time an enemy was instantiated. This is what it looked like:
func spawn_enemy():
var new_enemy = enemy.instantiate()
add_child(new_enemy)
var randomizer = randi_range(0, 3)
var start_pos = start_positions[randomizer]
new_enemy.global_position = start_pos.global_position
new_enemy.connect("on_death", enemy_death())
Something like that, I don’t remember the exact wording of the “connect” part in spawn_enemy() because I’m no longer using it. Then a function called enemy_death() would simply call spawn_enemy.
Thing is, when spawn enemy was called, an infinite loop would start as though connecting the signal was simultaneously emitting the signal. Am I wrong to think connecting the signal is only to create the link between the emitted signal and the emitter, requiring emit_signal to be called before any action happens? It seems like emit_signal and connect are doing the same thing only connect specifies what the signal affects.
I think you got this right. The connect
method will connect a signal of an object to a method. And the emit_signal
method will emit a signal, triggering every method connected to this signal.
I don’t have much information here but for it to do an infinite loop, there would be an emit_signal
called somehow from the spawn_ennemy method.
Adab | 2023-01-13 10:21
If your enemy spawn in to a dangerous area and dies, your signal handling code will spawn a new enemy. If the enemy also spawns in a dangerous area and immediatly dies, then you have an infinite loop problem. I suggest you add break points and follow the logic to examine how creating an enemy signals an enemy death.
godot_dev_ | 2023-01-13 15:24
That’s what’s weird; I put a breakpoint at the beginning of the above code and followed it. It runs normally, initializing enemy variables and executing the ready function after the “add_child” part. Then it resumes at “var randomizer…” and, once it hits “new_enemy.connect”, it immediately executes the enemy_death() function. The code never reaches where the “enemy” scene emits its death signal. So as far as I can tell, “connect” is both connecting and emitting the signal.
I ended up having to use “await” in the above code to wait for the death signal instead of connecting it. It works but seems like it shouldn’t have to be that way.
BillRogers | 2023-01-13 23:29