Sprites not visible in Inventory

Godot Version

Godot 4.4.1

Question

` Hello, everyone. I’m currently trying to make a game, but have a problem. I’ve been trying to make an Inventory GUI, following the MakerTech series of tutorials. I’ve succesfully made the Inventory GUI popup at the push of a button (complete with it pausing the game) and also make the slots be either filled or empty. I also added the code and the nodes so the Inventory items show their 2D Sprite. However, no matter what I do, the Items don’t appear in the slots. What is going on? Here is the code for “slot_ui”. If you need me to elaborate, I will gladly do so.
(I lament if I offend anyone with this post.)

extends Control

signal opened
signal closed

var isopen: bool = false

@onready var inventory: Inventory = preload(“res://inventory/playerInventory.tres”)
@onready var slots: Array = $NinePatchRect/GridContainer.get_children()

func _ready():
_update()

func _update():
for i in range(min(inventory.items.size(), slots.size())):
slots[i].update(inventory.items[i])

func open():
visible = true
isopen = true
opened.emit()

func close():
visible = false
isopen = false
closed.emit()

Hi,

The issue is likely related to the slot update function that you’re calling here:

slots[i].update(inventory.items[i])

Could you share that function?
I don’t see any issue with the code you’re shared already.

So sorry, I made a mistake… that was the code for “inventory_gui”.

Here’s the code for “slot_gui”:

extends Panel

@onready var backgroundSprite: Sprite2D = $background
@onready var itemSprite: Sprite2D = $CenterContainer/Panel/item

func update(item: InventoryItem):
if !item:
backgroundSprite.frame = 2
itemSprite.visible = true
else:
backgroundSprite.frame = 0
itemSprite.visible = false
itemSprite.texture = item.texture

Here’s the code for “inventory.gd”:

extends Resource

class_name Inventory

@export var items: Array[InventoryItem]

Seems to me that you’re showing itemSprite when item is null. You should swap your item.visible instructions like this:

if !item:
    backgroundSprite.frame = 2
    itemSprite.visible = false  # <-- here you had 'true' although the item is null
else:
    backgroundSprite.frame = 0
    itemSprite.visible = true # <-- here you had 'false' although the item is NOT null
    itemSprite.texture = item.texture

It worked, thank you!!!