Godot Version
4.3
Question
I made a basic script where i can pickup items off the ground and they will go into my inventory and i can drag those items around the inventory. But i have a problem where whenever i pickup an item it shows this error: "Invalid access to property or key 'invSlot’ on base object of type ‘Panel (ItemStackGui)’. "
The item_stack_gui script
extends Panel
class_name ItemStackGui
@onready var item_display: Sprite2D = $item_display
@onready var amount_text: Label = $Label
var InvSlot: InvSlot
func update():
if !InvSlot || !InvSlot.item: return
item_display.visible = false
amount_text.visible = false
item_display.visible = true
item_display.texture = InvSlot.item.texture
if InvSlot.amount > 1:
amount_text.visible = true
amount_text.text = str(InvSlot.amount)
inv_ui_slot script
extends Button
@onready var item_display: Sprite2D = $item_display
@onready var container: CenterContainer = $CenterContainer
@onready var inv = preload("res://inventory/playerinv.tres")
var itemStackGui: ItemStackGui
var index: int
func insert(isg: ItemStackGui):
itemStackGui = isg
container.add_child(itemStackGui)
if !itemStackGui.invSlot || inv.slots[index] == itemStackGui.invSlot:
return
inv.insertSlot(index, itemStackGui.invSlot)
func takeItem():
var item = itemStackGui
container.remove_child(itemStackGui)
itemStackGui = null
return item
func isEmpty():
return !itemStackGui
the inv_ui script
extends Control
@onready var inv: Inv = preload("res://inventory/playerinv.tres")
@onready var ItemStackGuiClass = preload("res://scenes/ItemStackGui.tscn")
@onready var slots: Array = $NinePatchRect/GridContainer.get_children()
var itemInHand: ItemStackGui
var is_open = false
func _ready():
connectSlots()
inv.update.connect(update_slots)
update_slots()
close()
func connectSlots():
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_slots():
for i in range(min(inv.slots.size(), slots.size())):
var invSlot: InvSlot = inv.slots[i]
if !invSlot.item: continue
var itemStackGui: ItemStackGui = slots[i].itemStackGui
if !itemStackGui:
itemStackGui = ItemStackGuiClass.instantiate()
slots[i].insert(itemStackGui)
itemStackGui.InvSlot = invSlot
itemStackGui.update()
func _process(delta):
if Input.is_action_just_pressed("toggle_inventory"):
if is_open:
close()
else:
open()
func close():
visible = false
is_open = false
func open():
visible = true
is_open = true
func onSlotClicked(slot):
if slot.isEmpty() && itemInHand:
insertItemInSlot(slot)
return
if !itemInHand:
takeItemFromSlot(slot)
func takeItemFromSlot(slot):
itemInHand = slot.takeItem()
add_child(itemInHand)
updateItemInHand()
func insertItemInSlot(slot):
var item = itemInHand
remove_child(itemInHand)
itemInHand = null
slot.insert(item)
func updateItemInHand():
if !itemInHand: return
itemInHand.global_position = get_global_mouse_position() - itemInHand.size / 2
func _input(event: InputEvent) -> void:
updateItemInHand()
and the Inv_slot script
extends Resource
class_name InvSlot
@export var item: InvItem
@export var amount: int
I hope thats all is needed to solve the issue i have no clue of why this error pops up if theres any more information needed ask, for me the error makes no sense.