How do I connect signals between scenes?

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

I am finishing up the spawning script for my enemies at the moment and have run into a problem with my shooting enemies.
Originally they emitted a shoot signal which I had connected to the scene of the map, this way the bullets they fired would be handled by the map node instead of the enemy itself.

It worked fine when I was testing the individual enemy, but now that I am having the enemies instanced in through the spawning script, I am not sure how I would connect my shoot signal to the map’s bullet handling function (_on_Tank_shoot).

TLDR: How do I connect a signal from an instanced child node to a function in the parent node.

Link to code for Map scene
Map Scene Here

:bust_in_silhouette: Reply From: 1izNoob

So if I understand you correctly you have a function on the map scene and spawn in enemy scenes and you want to be able to trigger a function on the map scene (with the map scene being the parent).

Here is actually a nice tutorial on this: https://youtu.be/IfPnpKcg47Y

Roughly, here are some things you can do:

  1. Use groups: You put the map scene into one group and then from each enemy scene you call get_tree().call_group(“group_name”,“function_you_want_to_start”). This works across scenes and you could run any function on your map from any enemy.

  2. You can use signals with arguments. I am not sure how your enemy scene looks like, but if your bullet is an area2d node, you could use the signal body_entered(physicsbody) and inside of that function use the body argument to run a function on the other scene with something like physicsbody.insert_your_method_here.
    (Both of these are explained in the video above in much more detail)

  3. You could spawn a signal along with the enemies and connect that to the map scene; all of this can be done in code. The syntax for that is:

    <emitting_node>.connect("signal_name", <target_node>, "target_method_name")

Hope that helps :slight_smile: