New to Godot having problem understanding Button Group

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

Hi.

I’m having an issue trying to understand the ButtonGroup logic, totally new to GDScript, don’t have a clue of what I’m doing. Obviously is wrong.

I created a scene that I saved in a folder called “Interface”, here is how it looks. Sorry, I know it’s probably messy:



The scene is called “MenuButtons” and it looks as follow:



The GDScript attached to the scene looks as follow:


extends Control

export(ButtonGroup) var group

func _ready():
	print(group.get_buttons())
	for i in group.get_buttons():
		i.connect("pressed", self, "button_pressed")

func button_pressed():
	var pressed_button = str(group.get_pressed_button())
	var split_text = pressed_button.split(':')
	var button_method = str(split_text[0])
	print(button_method)
	
	if button_method == "NewGameButton": print('Restarting the game')
	if button_method == "OptionsButton": print('Opening the Options screen')
	if button_method == "ExitButton": print('Quiting the game')

When I execute the scene with F6 everything works as expected:


enter image description here


Still not what I’m expecting, but it prints something. But here is the situation, I instantiated the MenuButtons scene as a node in another scene called GameOverlay, which is the test game UI.


enter image description here


This scene as well is instantiated inside the Level Scene.


enter image description here


When the Level is run and the Game Over screen shows, neither group.get_buttons() method returns a list and of course the buttons do not initiate the events. It seems to me that the script from MenuButtons does not recognize the button group at all.


enter image description here


I was following two tutorials trying to match their insight about using buttons in Godot. Yeah, I have no idea what I’m doing as you can tell. Any help would be really appreciated.

These are the tutorials I’m following:

  1. GODOT Menu System tutorial using button themes 20 lines of code
  2. How to use a Button Group - Godot Engine
:bust_in_silhouette: Reply From: placroix74

I have the same issue. Debugging a bit, I find that:

  • When the HUD scene is run on its own, the group variable, and the group property of the butons point to the same instance, BUT
  • When the HUD is instanced within another scene, the group variable, and the group property of the butons point to different instances

(I’d very much like for someone who knows the engine better to tell us why that is.)

In the meantime, I’ve worked around this by avoiding the exported group script variable, and obtaining the group from the button themselves, going from this:

export(ButtonGroup) var group1
export(ButtonGroup) var group3
export(ButtonGroup) var group7

func _ready():
    for group in [group1, group3, group7]:
        var buttons = group.get_buttons()
        for button in buttons:
            button.connect("pressed", self, "_on_unit_selected", [group])

… to this:

func _ready():
    var groups = [
        $"TabContainerUnits/1 pt/ButtonBomb".get("group"),
        $"TabContainerUnits/3 pts/ButtonBalloon".get("group"),
        $"TabContainerUnits/7 pts/ButtonCrawler".get("group")
    ]

    for group in groups:
        var buttons = group.get_buttons()
        for button in buttons:
            button.connect("pressed", self, "_on_unit_selected", [group])

Just found out the saved button group has “Local to scene” enabled, which is probably why the instances were different.

placroix74 | 2022-10-05 00:30