+8 votes

Hi all,

I have 26 buttons (A..Z in the alphabet).

I want to connect them to a function so that when any button is pressed, the value of the text of the button is added to an array. In other words, I press the A button and the "A" of the text value is added to an array.

This question is about connections and functions though.

So I've started a test with my first three buttons by doing this:

func _ready():

    #make the button connections
    get_node("Input/Button").connect("released", self, "_on_Button_released")
    get_node("Input/Button1").connect("released", self, "_on_Button_released")
    get_node("Input/Button2").connect("released", self, "_on_Button_released")

The function onButton_released is as such for now for testing purposes:

func _on_Button_released():
    print(self)

Currently the print outputs [Node572] no matter which of the three buttons I press.

What I'm trying to do is to to pass the button ID (or some such identifier) to the function _on_Button_released so that it can then process the text of that button.

Am I going about this the best way? I am new to it all and am a little stumped.

Thanks a tonne... Rob

in Engine by (838 points)

2 Answers

+17 votes
Best answer

From /u/CowThing on reddit who helped me solve the same question:

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

func _some_button_pressed(button):
    print(button.name)
by (94 points)
selected by

Ho MAN!

That's amazing. I was about to go down an epic route of writing 26 small functions that fed into a single function to do the works. I can't believe how efficient that is. The get_nodes_in_group function is incredibly powerful now I've done some reading on it.

I really want to thank you for that. My game is now working in its bare bone state. Now to finesse it!

Thanks again.

It worked for me , thanks.

Is there a way to identify the buttons apart from by their name though, e.g. attach some data to them?

Edit: Toa nswer my own question, subclass the Button and add whatever you want.

0 votes

To build upon the answer by Geaxle, in Godot 4 there's a new recommended syntax to connect signals, using Signal.connect() instead of Object.connect().

So, assuming there's a GridContainer we could add buttons to, the code could look like this:

func _ready():
    for letter in "ABCD":
        var button : Button = Button.new()
        button.text = letter
        button.pressed.connect(_button_pressed.bind(letter))
        $GridContainer.add_child(button)

func _button_pressed(letter : String):
    print("Button %s was pressed" % letter)
by (14 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.