Topic was automatically imported from the old Question2Answer platform.
Asked By
Titox
I have a control node with over 100 button children.How can I simply check if any of the button children are clicked or do I have to manually attach a signal to each one?
You can use a short script to automatically connect them at startup:
func _ready():
var button_parent = $Node2D/.../Control # path to parent node
for b in button_parent.get_children():
b.connect("pressed", self, "_on_Button_pressed")
func _on_Button_pressed():
print ("One of the buttons is pressed.")
Nice but is there a way I could chnage the scenE according to which BUTTON WAS PRESSED
Titox | 2019-09-05 00:19
Yes, that’s possible by passing the sender as parameter:
func _ready():
var button_parent = $Node2D/.../Control # path to parent node
for b in button_parent.get_children():
b.connect("pressed", self, "_on_Button_pressed", [b])
func _on_Button_pressed(which : Button):
print ("Button " + which.text + " was pressed.")
Thomas Karcher | 2019-09-05 09:40
I can change that which to anything right?
Titox | 2019-09-06 00:33
Sure. It’s just an arbitrary name for the variable receiving the value defined in the connect method. Did you notice I added a fourth parameter (“[b]”) in the connect method above? That’s how the callback function receives additional information about the sender (or any other additional information you want to transmit with the signal).