Connect signal from different scenes which are dynamically instantiated in main node

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

hi guys,

i have 3 scenes
main.tscn player.tscn enemy.tscn(this have areadbody)

in enemy.gd script

signal exited
func _on_enemy_body_exited(body):
	emit_signal("exited")

in player.gd script

func test():
	print("XYZ")

but the connect function is not written in player script because both the enemy and player are dynamically instanced in the main node

in main.gd script

onready var player_scene = preload("res://player.tscn")
onready var enemy_scene = preload("res://enemy.tscn")

enemy_scene.connect("exited", player_scene, "test")

but its not working, how to connect the signal of enemy to player

:bust_in_silhouette: Reply From: kidscancode

I imagine you’re getting an error message. You should really include that in your post.

preload() loads a scene file from disk and returns a PackedScene object, which can be used to make instances.

connect() needs to use paths to the nodes that exist in the tree. Where is your code that’s instancing the player and enemy? That is where you need to call connect.