How to connect a signal to all enemy instances at once?

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

So I’m making levels for a 2D platformer. One of the things I decided to add at the beginning of a level is a Life Screen scene that plays before you can play the level. Basically like in Super Mario Bros., where there’s a screen that tells you what level you’re about to play, how many lives you have, current score, etc. While this screen is displayed, I don’t want enemies to move, so that if I have a level where an enemy is on-screen and on the same level as the player, the player doesn’t get hit at a time where player input is disabled. My solution to this is to auto-set the enemy script to idle, and only be given permission to move once the life screen is gone. Basically, here’s the logic:

  • Scene starts
  • Life Screen timer autostarts
  • Timer ends
  • Signal gets sent
  • Life Screen disappears

The issue: I have to individually connect the signal to each individual instance of an enemy. It’s not auto-connected to all enemies, so every time I generate a new enemy in a level, I have to connect the signal to the new instance, otherwise the new instance is stuck in idle mode. This can be time consuming, and frustrating when I forget to connect it to a new instance. I just want to know if there’s a way of automating the process so each new instance could be auto-connected to the signal.

:bust_in_silhouette: Reply From: Nathcra

Instead of sending signals to all the enemy instances at once, you can use groups! In the enemy code’s ready function, you can use


This line of code will initialize the group when it is called for the first time. If you have a game controller script of some sort (likely where you were calling the signal from), you can use

get_tree().call_group("enemies", "a function").

Obviously you replace “a function” with whatever you need to call. You can make a function for all the cases, such as disabling the collisions on start up, and so on.

Hope this helps!

1 Like