How to detect a Bullet

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

I have a Player who can shoot instanced Bullets. On the Bullet, there is a Area2D Node. Now I want to print a message when I hit another Body with my Bullet. But the message must be printed in the Player Node. How can I conect the Bullet node with the Player node, because I cant emit a signal, because they arent in the same scene, because I instance the Bullets with the script?

Thanks for answers and sorry for bad english.

:bust_in_silhouette: Reply From: denxi

You can connect a signal through the script, as long as you know the path to the bullet.

Something like this could be on the player:

func connect_to_bullet(bullet_node):
    bullet_node.connect("bullet_hit", self, "on_bullet_hit")

You’d run this every time you instance a bullet, substituting the bullet node reference in.

I’m assuming the player node is the one doing the instancing, but if it isn’t it’s still a simple matter to pass the bullet reference to the player after its been instanced.

I tried this:

This is the Bullet Script:

extends KinematicBody2D

var movement = Vector2(0,0)
var speed = 150
signal Hit


func _ready():
    emit_signal("Hit")     #This is the signal for the Player


func _physics_process(delta):
    movement = Vector2(speed,0)
    move_and_collide(movement * delta)

And this is the Script for the Player:

extends KinematicBody2D

onready var BULLET = preload("res://Bullet.tscn")

func _process(delta):
         if Input.is_action_just_pressed("ui_right"):
	         var bullet =  BULLET.instance()
             bullet.transform = transform
	         get_node("/root/World").add_child(bullet)
	         connect_to_bullet(BULLET)         #Here I conect the new Bullet
	


func connect_to_bullet(BULLET):     #Here is the function as you sad
          BULLET.connect("Hit",self,"on_BULLET_Hit")	

func _on_BULLET_Hit():         #And here is the conection from the Bullet.
          print("Hello")

But it doesnt print Hello. Do you know my mistack?

Godot_Starter | 2020-02-18 19:07

Try not reusing variable names, and changing it to this:

extends KinematicBody2D

onready var BULLET = preload("res://Bullet.tscn")

func _process(delta):
     if Input.is_action_just_pressed("ui_right"):
         var new_bullet =  BULLET.instance()
         new_bullet.transform = transform
         get_node("/root/World").add_child(new_bullet)
         connect_to_bullet(new_bullet)         #Here I conect the new Bullet



func connect_to_bullet(bullet_node):     #Here is the function as you sad
      bullet_node.connect("Hit",self,"on_BULLET_Hit")    

func _on_BULLET_Hit():         #And here is the conection from the Bullet.
      print("Hello")

I swapped out the reused variable names, and put in the correct variable referencing the bullet when the connect function is called. I think this should work.

EDIT: I just realized as well you have the hit signal code in the _ready() section of your bullet. This will cause the bullet to print(“Hello”) when it’s spawned, not when it hits something. Although you might just being doing that for testing.

EDIT2: Because you have the bullet emitting the signal on its _ready(), it’ll end up emitting its signal before the player has had a chance to connect the two, as `_ready() is called after the node’s been added to the scene tree

denxi | 2020-02-18 19:19

Thank you, now it works!

Godot_Starter | 2020-02-19 06:20