How to send a signal to an instance that it was created by an enemy/player?

Godot Version

4.2.2

Question

How can I tell a created object who created it, without specifying the path to the creator?
The creator object - enemy or the player. The object being created - projectile. Each object has its own scene.
The enemy object does not have a fixed path name and can exist in a huge number of copies.
The current view of the creation script, somewhere inside it you need to add a signal.

		if !gun_animation.is_playing():
			gun_animation.play("shoot")
			instance = bullet.instantiate()
			get_tree().current_scene.add_child(instance)
			instance.global_transform = gun_barrel.global_transform

use global event bus should be quicker and easier

in your bullet.gd:

func _ready():
	EventBus.signal_the_projectile.connect(func():print("do something"))

in your creator.gd when you want to try to emit the signal:

EvenBus.signal_the_projectile.emit()

in the EventBus script AutoLoad:

extends Node

signal signal_the_projectile
1 Like