godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
89.44k stars 20.25k forks source link

ButtonGroup doesn't emit signal #70326

Closed boruok closed 1 year ago

boruok commented 1 year ago

Godot version

v4.0.beta8.official [45cac42c0]

System information

Ubuntu 22.10, GTX1060 Vulkan

Issue description

tried to connect multiple buttons with ButtonGroup, but unfortunately ButtonGroup doesn't emit pressed signal

extends Node

func _ready() -> void:
    var group := ButtonGroup.new()
#   var group : ButtonGroup = load("res://new_button_group.tres")  # this also doesn't work

    for c in get_children():
        c.button_group = group
    group.connect("pressed", _on_ButtonGroup_pressed)

func _on_ButtonGroup_pressed(button: BaseButton) -> void:
    print(1)

Steps to reproduce

  1. run attachment
  2. press any button to printout

Minimal reproduction project

New Game Project.zip

Sauermann commented 1 year ago

According to the documentation https://docs.godotengine.org/en/latest/classes/class_buttongroup.html

BaseButton.toggle_mode should be true.

So the intention of a ButtonGroup is to provide toggled buttons, of which only a single one can be pressed at the same time. In your MRP toggle_mode of the buttons is false, so it seems like you are using ButtonGroup for a non-intended use-case.

To make your use-case work, you could change your _ready-function to:

func _ready() -> void:
    for c in get_children():
        c.pressed.connect(_on_ButtonGroup_pressed.bind(c))

The description of ButtonGroup doesn't seem to make its use-case very clear, I believe that the documentation could be improved.

boruok commented 1 year ago

sry, missed line where said BaseButton.toggle_mode should be true