Godot listening to signals from multiple instances of the same scene

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

I have the following Scenes: Player Enemy Attack

When an Attack collides with the Enemy, the Enemy emits a “onHit” signal.

The Player listens for that signal and bounces back.

This is all working good, but now if I duplicate the enemy so there are multiple Enemy scenes, how do I listen to the signal on all of them? Is there a way to grab all instances of a Scene and connect to all of their signals? Is there a better way of sending this message back to the Player?

I’m very new to Godot so any advice is very helpful! Thanks.

:bust_in_silhouette: Reply From: Dumuz

You could have the signal connect() to the players method in the _ready() function. So when you instance that enemy all the signals from them will be connected to the same method in the player.

Thanks for the response, however I’m not following… how would the enemies be aware of the player object if they are instance of their own scene separate from the scene of the player?

DanielRead89 | 2021-08-06 03:18

If each of the separate scenes have their own respective script. Then when you duplicate/instance more enemies, they should be using that same enemy script. If you have the signal connect in the the ready() function for the Enemy script; when they ready up, they’ll all connect to whatever method you’re using on the player automatically ( and individually, because they’re separate instances.)

Player <—connect signal original scriptEnemy
^
|— signal automatically connecting via original script attachedDuplicate Enemy
^
|— signal automatically connecting via original script attachedDuplicate Enemy
^
|— signal automatically connecting via original script attachedDuplicate Enemy

Dumuz | 2021-08-06 03:35

Ah i see what you’re saying. I appreciate the answers! It seems a bit “hackey” though because if i make new levels, and add that enemy scene i have to bring that code to every one.

I also posted on stack overflow and think I’m gonna give the answer i got there a try

Godot listening to signals from multiple instances of the same scene - Stack Overflow

DanielRead89 | 2021-08-06 04:00