Hey folks. I'm trying to use a ButtonGroup, but I can't seem to add items to the Group, or get a test print out when the buttons are pressed. I'm attaching a .zip of my test project. Can anybody work out what's going on here?

loadcommandrepotest.zip
19kB

EDIT: It turns out my main problem is with load(). This line...
@onready var buttonGroupColour : ButtonGroup = load("res://ButGro1.tres") # Getting ButtonGroup by Load
...returns a ButtonGroup, but one which doesn't seem to have anything in the buttonGroupColour.get_buttons() array. Any ideas would be appreciated!

I haven't used ButtonGroup before, and I don't understand why the get_buttons() method returns an empty Array.

However, the button presses are detected. I changed your code in buttonGroupTest01.gd to the following for testing purposes:

extends Control

#@onready var buttonGroupColour : ButtonGroup = load("res://ButGro1.tres") # Getting ButtonGroup by Load
#@export var buttonGroupColour : ButtonGroup # Getting ButtonGroup by exported variable

# Called when the node enters the scene tree for the first time.
func _ready():
#	print(str("ButtonGroup type is: ", ButtonGroup))
#	print(str("Buttons in Group array include: ", buttonGroupColour.get_buttons()))
#	for but in buttonGroupColour.get_buttons():
#		print(str("Button name is : ", but.name))
#		but.pressed.connect(self._on_button_colour_pressed)
	$Button01.pressed.connect(_on_button_colour_pressed.bind($Button01))
	$Button02.pressed.connect(_on_button_colour_pressed.bind($Button02))
	$Button03.pressed.connect(_on_button_colour_pressed.bind($Button03))
	$Button04.pressed.connect(_on_button_colour_pressed.bind($Button04))

func _on_button_colour_pressed(btn: Button):
#	print(str("Button '", buttonGroupColour.get_pressed_button().name, "' in the ButtonGroup has been pressed."))
	print_debug("Button pressed: ", btn)

Instead of loading the .tres file, you could try creating the ButtonGroup with code, and setting the Buttons' button_group property to that group in code, and see if that works.

    DaveTheCoder Instead of loading the .tres file, you could try creating the ButtonGroup with code, and setting the Buttons' button_group property to that group in code, and see if that works.

    I just tested that, and it worked:

    extends Control
    
    #@onready var buttonGroupColour : ButtonGroup = load("res://ButGro1.tres") # Getting ButtonGroup by Load
    #@export var buttonGroupColour : ButtonGroup # Getting ButtonGroup by exported variable
    var buttonGroupColour : ButtonGroup
    
    # Called when the node enters the scene tree for the first time.
    func _ready():
    	buttonGroupColour = ButtonGroup.new()
    	$Button01.button_group = buttonGroupColour
    	$Button02.button_group = buttonGroupColour
    	$Button03.button_group = buttonGroupColour
    	$Button04.button_group = buttonGroupColour
    	print(str("ButtonGroup type is: ", ButtonGroup))
    	print(str("Buttons in Group array include: ", buttonGroupColour.get_buttons()))
    	for but in buttonGroupColour.get_buttons():
    		print(str("Button name is : ", but.name))
    		but.pressed.connect(self._on_button_colour_pressed)
    
    func _on_button_colour_pressed():
    	print(str("Button '", buttonGroupColour.get_pressed_button().name, "' in the ButtonGroup has been pressed."))

    I ran the scene and pressed the buttons in sequence. Here's the output:

    ButtonGroup type is: <GDScriptNativeClass#-9223372029254696538>
    Buttons in Group array include: [Button01:<Button#23286776961>, Button02:<Button#23353885829>, Button03:<Button#23404217480>, Button04:<Button#23454549131>]
    Button name is : Button01
    Button name is : Button02
    Button name is : Button03
    Button name is : Button04
    Button 'Button01' in the ButtonGroup has been pressed.
    Button 'Button02' in the ButtonGroup has been pressed.
    Button 'Button03' in the ButtonGroup has been pressed.
    Button 'Button04' in the ButtonGroup has been pressed.

    Looks like you didn't get something and don't have a clear vision of what you're trying to achieve.
    What is your goal in the first place ?

    Regarding button group, to create some radio buttons, you just have to create a new group in the editor for the first button in it then use the same group for others buttons, in the code, you juts have to deal with signals as usual and nothing more.
    Official demos have some useful case like "control gallery" and some include interesting UI code like the 2d/3d physics test which use an option menu for instance.

      So, after a lot of investigation, it turns out that my 'problem' is related to the "local to scene" flag on the ButtonGroup (which is mostly a 'symbol').

      Loaded independently of the scene with 'local to scene' set to true, the ButtonGroup resource doesn't contain references to the relevant buttons.

      Loading the resource independently with 'local to scene' set as false will also load the references to the buttons, but then that data will persist across scenes, which may cause trouble.

      Ultimately, it seems as though the best solution is simply to export a variable for ButtonGroup in the script for the editor, as the editor will update the links between nodes and scripts if objects are re-arranged, etc.

      @DaveTheCoder
      Hey, I dig your first option. I'm not completely clear on what's happening with the .bind, but are you using the get_node shortcut to just grab the nodes relative to the current node?

      OHH, I see, creating the buttongroup with code! That makes a ton of sense! That's certainly the clearest way I've found for resolving this issue in code. Thank you for thinking about this.

      JusTiCe8
      Hmm. My intention was to make a character-creator and I wanted to use radio buttons for options like hair colour. I created the attached project as I had encountered this issue and I wanted to address it in isolation.

      Thanks for the information on the button groups! I'll take a look at "control gallery"!

        4 months later

        Reverend_Speed

        I got the same issue i think. All buttons have the same button group but in different scenes the button group gets instantiated newly:

        https://imageupload.io/en/u8qNMjceT1QD0FL

        But after toggeling the Local to Scene Resource flag in the button group, everything works fine.

        Thank you so much, I really struggled with this.