Connecting a signal from newly spawned node to its parent

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

I’m currently trying to create a projectile that will split and go to different direction if it detects a wall in front of it.

Here’s what I would like to do:

  1. Once the player presses a button, it will signal the main scene to spawn ( using add_child(projectile) ) a projectile at the player’s location.

  2. The projectile will go forward.

  3. if the projectile detects a wall in front of it, it will signal the main scene to create 2 new projectiles (different to current projectile) on its location, both going in a different direction.

The problem lies in 3, as I can’t find a way to connect the signal of the created projectile to the main scene. I was able to manually connect the signal of the player to the main scene’s script as it was already a child of the scene, but I can’t connect the signal of the projectile like the player’s signal as it doesn’t exist in the main scene until I start the game and press the button.

Long story short, the question is:
Is there a way to connect the signal of the spawned projectile (the node) to the main scene (parent) through a script? If it’s impossible / not efficient, is there another way?

Thanks, I hope I elaborated it well enough.

:bust_in_silhouette: Reply From: kidscancode

You can use connect() to connect the new projectile’s signal to the function in the parent::

new_projectile.connect('signal_name', get_parent(), 'some_function')

Thanks, now I solved it.
I think I was overcomplicating the whole situation .

p.s. I love your videos!

jsyune1213 | 2018-05-17 09:06

1 Like