+1 vote

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.

in Engine by (20 points)

1 Answer

+3 votes
Best answer

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')
by (20,895 points)
selected by

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

p.s. I love your videos!

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.