Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+9 votes

I have a node with an Area2D inside of it that I am instantiating into another node. How would I go about connecting the Area2D's body_entered signal to the parent?

asked in CSC380Jan2024 by (1 point)

5 Answers

+4 votes

You can connect it in the _ready function (or whenever the node gets created) with node_name.connection_name.connect(function).

answered by (1 point)
0

What if the node isn't being instantiated through code? I have a main game scene and an instantiated node in it called "beacon". So I want to have a function in the games script get called when the beacon's Area2D is entered.

0

You would hook it up regularly then via the nodes signals panel. You could also use (in your case) $beacon.signal_name.connect(function_name) within your code. Honestly if it's already instantiated makes life a heck of a lot easier.

+3 votes

I think you can do this by right-clicking on the instance and choosing "editable children", so you can get inside the instance to find the Area2D and make the signal connection.

Alternatively, you could create a new signal inside a script on the root of the scene, and in that script you also connect the Area2D's body_entered signal to some code that emits the new signal. Since that new signal is on the parent/root of that scene, it will show up in the signals available for each instance of the scene you add elsewhere. If you do this, you can also give your new signal a more meaningful/descriptive name, for improved readability. I think this is the more elegant approach with good encapsulation, although it's a bit more work to set up?

answered by (3.4k points)
+3 votes

Click on your area2d and then find the node tab next to inspector on the right side of your screen and select the node you want. If you’re referring instead to an area you create during runtime like in a script then preload it at the top of your script and then instantiate the node as a variable and add it to your scene with add_child. Hope this helps

answered by (1 point)
+3 votes

yea you should be able to click editable children and select the area 2d node and connect the signal from there. Quick note if you unselect editable children it might disconnect your signal since all changes made to a child node are reverted when editable children is deselected

answered by (1 point)
0 votes

if it's a child node you can either enable editable children, select it and go to the node tab and connect it from there or you could also do $parent_node/child_node.signal.connect

answered by (1 point)
...