The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

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.

in Engine by (378 points)

1 Answer

+1 vote
Best answer

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.

by (449 points)
selected by

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
             connect_to_bullet(BULLET)         #Here I conect the new Bullet

func connect_to_bullet(BULLET):     #Here is the function as you sad

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

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

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
         connect_to_bullet(new_bullet)         #Here I conect the new Bullet

func connect_to_bullet(bullet_node):     #Here is the function as you sad

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

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

Thank you, now it works!

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.