How do I connect a signal from inside an instanced scene to the main scene's main node?

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

Been trying for hours, can’t figure it out. I’m making a golf game, and I’m instancing a Hole scene and a Ball scene to the Main scene. I want the Hole scene’s Area2D node to send a signal when a physics body (the ball) enters it, and I want the main scene to listen to it.

On the Hole scene’s script, I wrote

func _ready():
     $Area2D.connect("body_entered", self, "_on_Area2D_body_entered")
     if $Area2D.is_connected("body_entered", self, "_on_Area2D_body_entered") == true:

and my engine prints “hey”. However, none of my tests on the Main Node script with func _on_Area2D_body_entered(): works. I feel like I’m not connecting the right parts in the right order or something. I know this is supposed to be obvious, but none of the references I’ve found online (including the docs) are specific enough for me to understand.

:bust_in_silhouette: Reply From: rustyStriker

if you want to connect a signal to a different scene you need to change the second variable(where you placed self)…
The connect function works something like this:
connect( Signal to connect of the object itself, who owns the script for the function you want to use , the function’s name, [ variables you want to send])

so lets say i want an area2d to activate a function is it’s parent scene named “hello_body” i will write the following(in the area2d’s script):

connect("area_entered",get_parent(), "hello_body")

and in the parent scene i will need a function that goes as follows:

func hello_body(body):
     # Some awesome code ^_^

It worked! I was putting the func in the wrong script. Thanks a ton!

simonmakesgoodgames | 2019-11-24 17:09