0 votes

I'm having a strange bug with instanced buttons.
I have a main scene that looks like

Control
 - Control
  - - HboxContainer
  - Control
  - - HboxContainer
 - Control
  - - HboxContainer

and i instance some buttons into each of the HBoxContainers
each of the buttons when pressed, should turn pressed = false for its siblings

for i in get_parent().get_children():
    i.pressed = false

For some strange reason, the buttons inside the other controls also turn off which is unexpected.
Control.tscn

Control (Main)
 - Control (BtnSet1)
 - - HboxContainer (HBoxContainer)
 - - - (Buttons here)
 - Control (BtnSet2)
 - - HboxContainer  (HBoxContainer)
 - - - (Buttons here)
 - Control (BtnSet3)
 - - HboxContainer  (HBoxContainer)
 - - - (Buttons here)

Control.gd

extends Control

var set1Opts = [1,2,3,4,5,6]
var set2Opts = [1,2,3,4]
var set3Opts = [1,2,3,4,5,6,7,8,9,10]
# Called when the node enters the scene tree for the first time.
func _ready():
    var packedBtn = load("res://Button.tscn")
    for i in set1Opts:
        var insBtn = packedBtn.instance()
        insBtn.setup(str(i),"set1")
        $BtnSet1/HBoxContainer.add_child(insBtn)
    for i in set2Opts:
        var insBtn = packedBtn.instance()
        insBtn.setup(str(i),"set2")
        $BtnSet2/HBoxContainer.add_child(insBtn)
    for i in set3Opts:
        var insBtn = packedBtn.instance()
        insBtn.setup(str(i),"set3")
        $BtnSet3/HBoxContainer.add_child(insBtn)

Button.tscn

Just a button, toggle mode on

Button.gd

extends Button
var type
# Called when the node enters the scene tree for the first time.
func _ready():
    pass
func setup(name, butType):
    text = name
    type = butType

func _on_Button_toggled(button_pressed):
    print(get_parent().get_parent().name)
    print(get_parent().get_children())
    for i in get_parent().get_children():
        i.pressed = false
        print(i.text)

Whats even stranger is that it only prints to console only the siblings as expected but does in fact turn all buttons off even those ones in different nodes

minimal reproduction project (made with 3.1.1 but occurs in 3.1 also)
https://github.com/Squatnet/GodotInstancingBugMinReproProj

asked Jun 20, 2019 in Engine by Squatnet (201 points)

Have since tried

  • Change toggle mode setting on button
  • swapping pressed() with toggled()

which has made no difference

1 Answer

+1 vote
Best answer

In your minimal example project, toggling didn't work at all due to i.pressed = false in Button.gd instead of if i != self: i.pressed = false. Once I fixed this, everything was working fine and I couldn't reproduce any of the problems you're describing: All other buttons in the same set were unpressed, and no other button set was changed.

answered Jun 21, 2019 by Thomas Karcher (1,046 points)
selected Jul 8, 2019 by Squatnet

Ah, That would make sense. Always find i miss the simplest things haha.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.