0 votes

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:
          print('hey')

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.

in Engine by (49 points)

1 Answer

+1 vote
Best answer

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 ^_^
by (1,193 points)
selected by

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

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.