+5 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 (824 points)

1 Answer

+13 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.

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 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 webmaster@godotengine.org with your username.