Click then change item

extends Control

class_name CraftingGui

var isOpen : bool = false

@onready var inventoryCrafting : Inventory = preload("res://objects_items/items/ItemsCrafting/Inventory_crafting.tres")
@onready var inventoryShowItem : Inventory = preload("res://UI/crafting_gui/SlotShowItem.tres")
@onready var itemStackGUIClass = preload("res://UI/itemStackGUI.tscn")
@onready var slotsInventoryCrafting : Array = $NinePatchRect/GridContainerInventory.get_children()
@onready var slotShowItem : Array = $NinePatchRect/RecipeContainer.get_children()

var locked : bool = false
var itemSelected : Item

func _ready():
	connectSlots(slotsInventoryCrafting)
	inventoryCrafting.updated.connect(update)
	inventoryShowItem.updated.connect(update)
	update(inventoryCrafting, slotsInventoryCrafting)
	update(inventoryShowItem, slotShowItem)

func _input(event):
	pass

func connectSlots(slots):
	for i in range(slots.size()):
		var slot = slots[i]
		slot.index = i
		
		var callable = Callable(onSlotClicked)
		callable = callable.bind(slot)
		slot.pressed.connect(callable)
		
func update(inventory : Inventory, slot):
	
	# update inventorySlots
	for i in range(min(inventory.slots.size(), slot.size())):
		var inventorySlot : InventorySlot = inventory.slots[i]
		
		if !inventorySlot.item : continue
		
		var itemStackGui : ItemStackGUI = slot[i].itemStackGui
		if !itemStackGui:
			itemStackGui = itemStackGUIClass.instantiate()
			slot[i].insert(itemStackGui)
			
		itemStackGui.inventorySlot = inventorySlot
		itemStackGui.update()
		
func onSlotClicked(slot):
	if locked : return
	if slot.isEmpty() :
		print("Slot index null : ",slot.index)
		return
	else :
		itemSelected = slot.itemStackGui.inventorySlot.item
		setShowItem(slotShowItem)
		print(itemSelected.display_name, " : ",slot.index)
	

func setShowItem(slot) :
	if !itemSelected :
		return

	slot.itemStackGui.inventorySlot.item = itemSelected
	
func open():
	visible = true
	isOpen = true

func close():
	visible = false
	isOpen = false

in function setShowItem() I have a problem which is

Invalid get index ‘itemStackGui’ (on base: ‘Array[Node]’).

Screenshot 2024-02-04 174723

my object is to clicked item on right side and its change item in slot on left

slot in this gui is the same script


and this my node

i really need some help T_T

Your slotShowItem variable is an array of nodes, so it is not indexable with dot notation like a dictionary.
get_children() returns an array of nodes. Did you mean to fetch a particular one? Did you forget to loop over the array?

what u mean is my slotShowItem is not an Array right? i tried to use slowShowItem[0] but its not working or am i misunderstood?

You literally typed it as an array, tho.

but i dont understand it why slotsInventoryCrafting is working but slotShowItem is not working as i thought. the difference is slotsInventoryCrafting have too many slot but slotShowItem just only have 1 slot

In this function

slot comes from slotShowItem, which is an array, but you’re indexing it like it’s a dictionary.
Instead do

slot[0].itemStackGui.inventorySlot.item = itemSelected
func onSlotClicked(slot):
	if locked : return
	if slot.isEmpty() :
		print("Slot index null : ",slot.index)
		return
	else :
		itemSelected = slot.itemStackGui
		setShowItem(slotShowItem)
		print(itemSelected.inventorySlot.item.display_name, " : ",slot.index)
	

func setShowItem(slot) :
	if !itemSelected :
		return
		
	slot[0].itemStackGui.inventorySlot.item = itemSelected.inventorySlot.item

its still got error

Invalid get index ‘inventorySlot’ (on base: ‘Nil’).

or maybe im doing it a wrong way at the start

Well, that is a DIFFERENT error, so it’s progress. That means the itemStackGui you stored in slot[0] is null. This means the children of the “RecipeContainer” node are not getting initialized the way you expect, but they are the type of object you want, so you’re a bit closer.

oh maybe i get it. i will tried it later. hope it gonna work for me cause its my project for study

Guys i think i found a problem why its Invalid get index “inventorySlot”. its because
if !inventorySlot.item : continue this line at first i set my slotShowItem[0] was null that mean its doesn’t have any item so when its come to this line its skip the loop

var itemStackGui : ItemStackGUI = slot[i].itemStackGui
		if !itemStackGui:
			itemStackGui = itemStackGUIClass.instantiate()
			slot[i].insert(itemStackGui)
			
		itemStackGui.inventorySlot = inventorySlot
		itemStackGui.update()

its skill all here so inventoySlot not initialized. but i dont know how to fix.

i can change it to item now but the texture not appear


You can’t “add” something that already has a parent. Use reparent().

oh thank you so much

right now im trying to show a material with text but its appear like this. it can run but i think its not a good idea to have a error like this

Click on the error to see what line of what file causes it.

its can’t click at the error. im very confusing

Yes, you are very confusing. Click on the arrow to the left of the error line to see the stack trace.