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 in the group.

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 button = $button var group = ??? if group.get_pressed_button() == button:

I tried the group name and path in file system, but I get identifier not found.

Upon further testing I've gotten a little further. My variables:

onready var group = $genderButtons/maleButton.group
onready var test = group.get_pressed_button()
onready var mButton = $genderButtons/maleButton
onready var fButton = $genderButtons/femaleButton

Then I have this function:

func _UpdateItems(): if group.get_pressed_button() == mButton: print(str(test)) print(str(group)) elif group.get_pressed_button() == fButton: print(str(test)) print(str(group)) print("if fbutton pressed")

But it still doesn't seem to be working. I still don't get the female button output. They are in toggle mode so I assume clicking female button should make the male button not pressed. Then on some tests I get the complete opposite and only female button output works. I guess I'm still not setting this up correctly BUT it's still printing out if group.get_pressed_button() == mButton: so I assume I'm on the right track.

If anyone has experience with getting outputs from buttongroup's in 3.0 I'd greatly appreciate any input.

I am not pretty sure, what you want to do. Eventually does this screenshot help you?

Thank you for your input, that seems to work better than what I have. I originally use ButtonGroup parameter in inspector: which is why I used var group = buttonName.group instead to get group instead of make a new one. I assume those work the same.

Anyways I seem to have come across one more issue. I have two buttons: One must always be on so I need to use toggle mode. With toggle mode on, your example script prints multiple outputs. Like 3 separate strings at once ...

So I try to use toggle signal instead.

Now the issue here is since I have two button, I don't want to write code twice so I write custom function. So I try this: But when I try this I get random outputs on button press. Sometimes "1" ... sometimes "2" ... I assume issue is with toggle mode but I see no other option in docs.

I don't know well how the ButtonGroup work, but I made a simple toggle button test and it work well.

The code:

extends Control

var group = preload("res://buttongroup.tres")
onready var btn_a = $btn_a
onready var btn_b = $btn_b

func _ready():
	btn_a.group = group
	btn_b.group = group
	_check()
	
func _on_btn_a_toggled( button_pressed ):
	if button_pressed:
		print("a")

func _on_btn_b_toggled( button_pressed ):
	if button_pressed:
		print("b")

func _check():
	var current = group.get_pressed_button()
	var name = TYPE_STRING
	
	match current: # 'match' it’s the equivalent of the 'switch'
		btn_a:
			name = "A"
		btn_b:
			name = "B"
	
	$label.text = "The button " + name + " is pressed"

The File:

Maybe using of "pressed" signal will do the job?

BTW I don't think that it makes any difference if you set the button group via editor or script.

5 years later