Instanced object doesn't emit signal

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

I have a button scene and main scene. Every X seconds an instance of the button (Bonus) is added to the main Scene. Whenever the button is pressed, it emits signal “bonus_pressed”. Except it doesn’t.

Here is the button code:

Bonus.gd

signal bonus_pressed

func _on_TouchScreenButton_pressed():
	print("this one works!")
	emit_signal("bonus_pressed")

Here is the main code:

Scene.gd

var Bonus = preload("res://Bonus.tscn")

func _on_TimerBonus_timeout():
	var bonus = Bonus.instance()
	add_child(bonus)

func _on_Bonus_bonus_pressed():
	print("works as well!")

all the Godot signals are connected via editor and are connected correctly. Everything works as intended except the last function. If I place a button at main scene manually (in editor), then everything workes as it should, both messages are printed. However, when trying to press instanced buttons, only “this one works!” is printed.

Output:

--- Debugging process started ---
Godot Engine v3.5.2.stable.flathub.170ba337a - https://godotengine.org
OpenGL ES 3.0 Renderer: GeForce GT 630M/PCIe/SSE2
Async. shader compilation: OFF
 
this one works!
works as well!
this one works!
works as well!
this one works!
works as well!
this one works!
this one works!
this one works!
this one works!
this one works!
this one works!
--- Debugging process stopped ---
:bust_in_silhouette: Reply From: godot_dev_

It appears you are missing logic that connects the _on_Bonus_bonus_pressed function to your bonues_pressed signal. Try changing your logic as follows to connect to the signals of your new button:

func _on_TimerBonus_timeout():
   var bonus = Bonus.instance()
   add_child(bonus)
   bonus.connect("bonues_pressed",self,"_on_Bonus_bonus_pressed")

I thought that I didn’t have to do that if I had already connected that manually placed button. Now I understand that it connected only one exact copy. Thanks!

KitePZ | 2023-04-04 20:21