weird instancing issue with buttons

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

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

Have since tried

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

which has made no difference

Squatnet | 2019-06-20 19:46

:bust_in_silhouette: Reply From: Thomas Karcher

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.

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

Squatnet | 2019-07-08 17:23