+1 vote

I am finishing up the spawning script for my enemies at the moment and have run into a problem with my shooting enemies.
Originally they emitted a shoot signal which I had connected to the scene of the map, this way the bullets they fired would be handled by the map node instead of the enemy itself.

It worked fine when I was testing the individual enemy, but now that I am having the enemies instanced in through the spawning script, I am not sure how I would connect my shoot signal to the map's bullet handling function (onTank_shoot).

TLDR: How do I connect a signal from an instanced child node to a function in the parent node.

Link to code for Map scene
Map Scene Here

in Engine by (39 points)

1 Answer

+5 votes
Best answer

So if I understand you correctly you have a function on the map scene and spawn in enemy scenes and you want to be able to trigger a function on the map scene (with the map scene being the parent).

Here is actually a nice tutorial on this: https://youtu.be/IfPnpKcg47Y

Roughly, here are some things you can do:

  1. Use groups: You put the map scene into one group and then from each enemy scene you call gettree().callgroup("groupname","functionyouwantto_start"). This works across scenes and you could run any function on your map from any enemy.

  2. You can use signals with arguments. I am not sure how your enemy scene looks like, but if your bullet is an area2d node, you could use the signal bodyentered(physicsbody) and inside of that function use the body argument to run a function on the other scene with something like physicsbody.insertyourmethodhere.
    (Both of these are explained in the video above in much more detail)

  3. You could spawn a signal along with the enemies and connect that to the map scene; all of this can be done in code. The syntax for that is:

    <emitting_node>.connect("signal_name", <target_node>, "target_method_name")
    

Hope that helps :)

by (242 points)
selected by
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.