+1 vote

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.

in Engine by (13 points)

1 Answer

0 votes

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.

by (341 points)

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?

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 script --- Enemy
^
|--- signal automatically connecting via original script attached-- Duplicate Enemy
^
|--- signal automatically connecting via original script attached-- Duplicate Enemy
^
|--- signal automatically connecting via original script attached-- Duplicate Enemy

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

https://stackoverflow.com/q/68675099/3220811

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.