I want to send an emit from an instantiate() scene created by addchild() to the main scene. And print “Hi” But i can't manage to add something via code and then connect it.
I can do it when everything is in the same scene with the node signal links by hand.
My setup is simple. I have a main scene with a timer that looks like main-Timer
Timer is linked to main and the code looks like this. Script on main
`xtends Node2D
var enemy_scene = preload("res://emit 2 image instance/enemy.tscn")
func _ready():
pass
func _process(delta):
pass
func _on_timer_timeout():
var enemy = enemy_scene.instantiate()
add_child(enemy)
func _sayhi(msg):
print("hi")
`
The other scene is enemy and looks like this enemy icon and the code looks like this enemy-Icon. Script on Icon
`extends Sprite2D
signal msg
func _ready():
pass
func _process(delta):
pass
func _input(event):
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
if get_rect().has_point(to_local(event.position)):
msg.emit("hi")
queue_free()
`
everything works fine opponent is spawned after 2 seconds and when you click on it it is deleted. I just can't manage to transfer the hi or any variabale whatsoever.