GODOT- Mass connecting a lot of button to the same function


There are some cases in which we need to get the current object and manipulate the functions. Mass connecting a lot of button to the same function is one of such cases.

In GODOT to know which button is been pressed and manipulate the function accordingly, we need to first group buttons.

Click the particular button, and select node. Under node you have signals and groups. Add a new button group say “letter_button”. Repeat the same for every button you need to process.

Screenshot from 2018-05-17 12:13:46.png

Then in script add the following

func _ready():

 for button in get_tree().get_nodes_in_group("letter_button"):
 button.connect("pressed", self, "_which_button_pressed", [button])

func _which_button_pressed(button):
 button_name = button.name
 print(button_name

 

Now when you click button, it will print the name of currently pressed button.

Screenshot from 2018-05-17 12:21:41.png

Leave a comment