How to use ButtonGroup get_pressed_button ( )?

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

When using buttongroups to easily make a set of buttons toggleable one at a time, I get strange behavior. When using button functions like is_pressed() on a button node of a group, I get the same result as if pressing any button as if the buttongroup as a whole is what’s signaling the pressed function.

I assume for button groups I have to use get_pressed_button() like the docs say but I’m not sure how to use this function. How do I identify the group to use this on?
var group = ???
I tried the group name and path in file system, but I get identifier not found.

Want to second that question.

Maxpilot | 2018-02-20 16:22

@Maxpilot Did that for you!

SingingApple | 2018-02-21 10:02

I flagged up this question as well. I think the ButtonGroup won’t be very useful until it gets some sort of “button_pressed_changed” signal.

glaforte | 2018-07-15 18:49

:bust_in_silhouette: Reply From: Maxpilot

A little bit further now. Each BaseButton (BaseClass of all Buttons) has a member variable “group”, which is the ButtonGroup. So I was able to identify the pressed button simply with:


inside of each button - behaviour of the group.
Didn’t use that in the end, rather used a parent VBoxContainer, each button signaling itself and going on with:

func on_button_pressed():

How does knowing the name help? What would be actually useful is if the group returned the index of the button pressed, in the order they appear in the list. Otherwise we have to resort to all sorts of chicanery. What I’ve done is maintain a Dictionary mapping the buttons to my ‘items’ (data). Then when a button is pressed I get my item by doing this (C# sorry)

_dictionary.Add(checkBox, item);
checkBox.Connect("pressed", this, nameof(ItemChecked));

then in my ItemChecked() method:

CheckBox pressedButton = _buttonGroup.GetPressedButton() as CheckBox;
var myItem = _dictionary(pressedButton).Value

However: there seems to be a bug. I can click on different buttons and have the same Button returned from GetPressedButton()… causing me much grief.

edit: oh wait the button’s name, sorry was thinking the group’s name or something. Anyway due to the bug I think I’ve found, it still wouldn’t help. But I’m investigating.

duke_meister | 2018-06-18 09:31