How to connect a signal to multiple instances?

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

I hope I can articulate, what I want to achieve. I tried to find an answer by myself, but I just coulnd’t find anything, maybe I googled the wrong questions? I already asked on Discord, but there was a lot going on and I think this is a better place. It’s my first game, everything is coupled which is bad, I just wanted to get my hands dirty. But I’m struggling with a basic concept: signals

So I’m making an Earthbound-like RPG. You wander around in an Overworld and if you come close to an enemy, you enter a turn-based battle with said enemy. Let’s say, my Level has mutliple Rats (the enemy), which I placed there, so they are not instanced at runtime. Every Rat has a scene as a child: BattleStart. That scene is an Area2D and on entering, a little animation plays and when the animation is finished, the BattleStart scene emits a custom signal.

My problem: In my Main-script, I need to connect the signal, but in order to connect to the BattleStart scene of every Rat, i need to know their path. But what if there are multiple rats, then also skeletons and so on? Do I need to connect to every single one of them, since they all have a different name?

$World/Environment/Rat/BattleStart.connect("enter_battle", self, "_on_transition_animation_ended") $World/Environment/Rat2/BattleStart.connect("enter_battle", self, "_on_transition_animation_ended") $World/Environment/Skeleton/BattleStart.connect("enter_battle", self, "_on_transition_animation_ended") $World/Environment/Skeleton2/BattleStart.connect("enter_battle", self, "_on_transition_animation_ended")

Do I need to do that for tens or hundreds of enemies? I always want to connect to that one BattleStart scene, which is part of every enemy. Or shouldn’t I do that? If my approach is completely wrong, please tell me how to do it. I think this is actually a very common case, isn’t it? Because you often have multiple instances of scenes and all of them should send a signal to one parent somewhere up above. What do I miss? Please explain to me elaborately, I’m a beginner.
Thanks a lot in advance

:bust_in_silhouette: Reply From: Mrpaolosarino

Instead of creating many battlestart scene which is very tiring, autoload a one battlestart so you can use it anytime anywhere.
And then on the playing problem, create a method on the battlestart scene that you autoloaded and rename it to your custom signal.

And then on the ready function of your rat, connect your signal to autoload battle start

Thanks for your answer. The problem with autoload is, that this BattleStart scene should be a child of every enemy, since it has the CollisionShape and I need to adjust the CollisionShape for every enemy. The BattleStart scene is automatically part of every enemy anyways.
Thanks to you I came to some kind of a solution. BattleStart scene calls a function in my autoloaded Global scene which just calls a function in its sibling Main scene. That’s kind of a solution, but highly coupled…

Medioka | 2021-06-16 09:28