How to connect _on_body_entered function from an instanced bullet scene to an instanced asteroid scene?

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

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 _on_body_entered on the Bullet script, but the only workable function I found uses get_child(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!

:bust_in_silhouette: Reply From: Inces

There are built_in 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.

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

Ombre | 2022-07-25 18:48