Hi,
How to toggle one TextureButton instance and untoggle the other? I've added them to a gridContainer for my Inventory.

@onready var item = preload("res://texture_button.tscn")
var inv = 3
func _ready():
	for i in inv:
		var itemTemp = item.instantiate()
		add_child(itemTemp)

  • Gowydot Sure it can. Just make sure that ButtonGroup resource in the slot scene has its Local So Scene flag turned OFF, so it's shared among all buttons.

I think the root of my problem is I don't know which one of the instanced buttons is clicked so I learned to add name ID to it.

But now how can I use the name ID to make that corresponding button do something?
#my failed code

@onready var item = preload("res://Slot.tscn")
var inventorySize = 3
var itemTemp

func _ready():
	for i in inventorySize:
		var itemTemp = item.instantiate()
		add_child(itemTemp)
		itemTemp.name = "itemTemp%d" % i
		itemTemp.gui_input.connect(self.printt)
func printt():
	for i in get_children():
		if i.name == "itemTemp1": #Something like this
			print(i)

This could be:

itemTemp.name = "itemTemp" + str(i)

But really you can set a local function on the button that responds to the gui signal.

OK I finally got it working

BUT I feel like it gotta be easier way. Here's what I did:

My child tree:
-Panel
--TextureButton

My basic steps:
-Add_child to gridContainer
-Create ID for each child
-Create function to change texture_normal.jpg based on ID
-When pressed, it first resets every child to normal texture.jpg (via a chain of 2 signals)
-Then the function to change texture_normal kicks in.

gridContainer:

extends GridContainer

@onready var item = preload("res://Slot.tscn")
@onready var _normal = preload("res://icon.jpg")
var inventorySize = 3

func _ready():
	for i in inventorySize:
		var itemTemp = item.instantiate()
		add_child(itemTemp)
		itemTemp.name = "itemTemp" + str(i)
		itemTemp.gui_input.connect(self.reset_texture)
		
func reset_texture():
	for i in get_children():
		i.get_node("Icon").texture_normal = _normal

TextureButton:

extends TextureButton

signal gui

@onready var _pressed = preload("res://icon1.jpg")


func _on_pressed():
	emit_signal("gui")
	if get_parent().name == "itemTemp0":
		texture_normal = _pressed
	elif get_parent().name == "itemTemp1":
		texture_normal = _pressed
	elif get_parent().name == "itemTemp2":
		texture_normal = _pressed

Panel:

extends Panel

func _ready():
	$Icon.gui.connect(self.emitsig)

func emitsig():
	emit_signal("gui_input")

Anyone knows a better way to do this? 😆

ps. I use changing texture_normal.jpg since I read you can't change the button states of TextureButton(?)

  • xyz replied to this.

    Gowydot Anyone knows a better way to do this? 😆

    I do 😉
    No need for any code at all. A button group can handle this logic for you.
    Create a button and assign normal state and pressed state images. Turn on button's Toggle Mode property and assing a new ButtonGroup to Button Group property. Now duplicate this button as many times as you need. All of them will now be part of the same button group and properly toggle each other.

      xyz

      I did try assign ButtonGroup on my child.tscn but could never got it working 😥 . I'm not sure if it can work with add_child instances. Either that or I did it wrong.

      • xyz replied to this.

        Gowydot Sure it can. Just make sure that ButtonGroup resource in the slot scene has its Local So Scene flag turned OFF, so it's shared among all buttons.

          xyz

          Ok that works. I feel like just finished building a staircase then found out there is a button for elevator smh.