How can I use nodeName.connect("signalName", self, "functionName") if the nodeName is unknown?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster
:warning: Old Version Published before Godot 3 was released.

I’m building breakout. So i have multiple balls that can spawn/instance.

I want to send a signal from the ball, when it’s time to remove a brick. The problem is, there are multiple ball instances and each with different names.

In ball.gd I use emit_signal("destroyBrick")

So in the brick.gd file, if I try and use: ball.connect("destroyBrick", self, "destroyThisBrick") it won’t work as ball changes its name each time it’s added to the scene.

Does anyone know how to use a signal when the receiving node doesn’t know the name of the emitting node?

:bust_in_silhouette: Reply From: mateusak

You really just need to check body_enter on the ball, and see if the body has destroyThisBrick, if so, destroy it.

connect("body_enter", self, "destroy_brick")

func destroy_brick(body):
    if (body.has_method("destroyThisBrick")):
       body.destroyThisBrick

Responding you other question too, in the brick you need to just

func destroyThisBrick():
   numOfHitsRequired--
   
   if numOfHitsRequired <= 0:
      #code

Thank you for that. It solved the problem. For those in the future I did have to add the get_parent() part as below:

if (entity.get_parent().has_method("destroyThisBrick")):
    entity.get_parent().destroyThisBrick

Thanks again, much appreciated.

Robster | 2017-04-24 06:37

:bust_in_silhouette: Reply From: MrMonk

just guessing here.
1.maybe you create a node with a known name add ball instances to this node and then use: get_children() to find the names of all instances.
2. after creating the instance, use: ball.set_name("ball" + str(ball.get_instance_ID), store names in an array for later use.
Anyway interesting to see how you solve this.