How to emit signal from 1 instanced node to another instanced node?

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


Lets say I have one scene called Ball.tscn . it has a script attached. Ball will be an area2D node with a collision shape node attached, and a sprite lets say.
Lets say I have another scene called Square.tscn . it has a script attached. Square will be an area2D node with a collision shape node attached, and a sprite lets say.

Upon loading my game, there will be 1 Node2D called Game, and in the ready function of a script attached to this node, I will have code that loads 5 Ball scene instances and 5 Square scene instances. Their initial positions of all of these instances will be bounded in a given area. Squares will remain in this position, while Balls will have a code giving them a linear movement away from where they start (with a random direction, lets say). The details don’t matter much here.

What I planned is the following:

  • For the Squares area2D, attach a signal area_entered to the Square’s script. In that signal, I wanted to emit a different signal to the script Ball that entered the Square’s area2D.

The main goal I want is for one instanced node to be able to identify another instanced node that entered its area. If there is a better way without signals that accomplishes this, that would also be equally helpful.

When nodes aren’t instanced, its easy enough to connect them via script or through the signal drop down, but for instanced nodes, I don’t know how to reference them. Usually I just use get_node(“/root/Game/_____”) to reference a node that is already in the game. Or if it is just one instance, usually it has the name of its root node, so I can “anticipate” what it will be called. But when I instance a bunch of nodes, they receive different names that start with @ symbols, and I don’t know how via code to reference these nodes. Knowing this would also be VERY helpful.

Finally, to clarify, I don’t want to send a signal to ALL Ball instances if the square’s area2D is entered. I only want the signal to send to the Ball instance that entered the square’s area2D.

Any help is greatly appreciated, this is a huge gap in my knowledge that is halting my progress with my game.


:bust_in_silhouette: Reply From: Wakatta


With multiple instances it’s easiest to mange their signals within that/those node(s) itself opposed to managing them from another node somewhere in the tree structure.

In the Square script

#body here is a reference to a ball instance or possibly another square instance and all it's members and functions can be accessed from this var
func detect(body):

func _ready():
    connect("body_entered", self, "detect")

You can also from the instancing block of your code use the current reference to make the connections

var ball = load("path/to/ball/node")
var square = load("path/to/square/node")

func _ready():
    for x in rang(5):
        var squ_ins = square.instance()

        #self is this node or you can use squ_ins
        #depending on where your code is
        squ_ins.connect("body_entered", self, "_on_body_entered") = + str(x)

func _on_body_entered(body):

More information