0 votes

Hi, I'm making an Asteroids clone with Godot. The MainScene generates a number of asteroids, making new ones appear when another one gets out of screen, and the Player script generates bullet when the shoot key is pressed. I've been trying to use signals to connect the onbodyentered on the Bullet script, but the only workable function I found uses getchild(0) which doesn't return the right instance. I also don't really know where to emit the signal from!
So far I have :

if Input.is_action_just_pressed("shoot"):
        b = Bullet.instance()
        owner.add_child(b)
        b.add_to_group("Bullets")
        b.transform = $Muzzle.global_transform
        get_parent().get_child(0).emit()

and on the Bullet scene :

func emit():
    emit_signal("bullet_collision")
    print("emission")

func _on_Bullet_body_entered(body):
    if body.is_in_group("Asteroids"):
        print("bullet collision")
        body.queue_free()
        queue_free()

and in the ready of Bullet :

connect("bullet_collision", self, "_on_Bullet_body_entered")

Oh and the collisions work fine when they're not instances. Thanks!

Godot version 3.4.4
in Engine by (15 points)

1 Answer

0 votes
Best answer

There are builtin signals in Godot, and "body_entered" is one of them. It is always emitted by default, whenever collision object collides with a body. You have to connect it, but You don't need to emit it.
What I see in your code - You created custom signal "bullet
collision". You can't expect it to trigger by itself. Just use "body_entered" signal, connect it instead.
You can see a list of built in signals in node documentation, as well as in editor signals tab.

by (7,305 points)
selected by

It's working thanks!! I've been racking my brain over this!

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 Frequently asked questions and 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 [email protected] with your username.