Inventory Item Images Help

Godot Version

Godot 4.3

Question

I’m having trouble with making an inventory system. I’m following DevWorms video. https://youtu.be/X3J0fSodKgs?si=l2rjRD21QTh5mRUB

The problem that I’m having rn, is that the image for the item in the inventory slot is not showing up. I made a .tres file for the item (LemonPepperWings) and a .tres file for storing the players inventory (PlayerInvi).

Here’s the code for the scripts that I made:

Inventory.gd:

extends Resource

class_name Invi

@export var items: Array[InviItem]

InventoryItem.gd:

extends Resource

class_name InviItem

@export var name: String = ""
@export var texture: Texture2D

inventoryUI.gd:

extends Control

@onready var invi: Invi = preload("res://Player/UI/Inventory/PlayersInvi.tres")
@onready var slots: Array = $MarginContainer/VBoxContainer/GridContainer.get_children()

var is_open = false 
var speler = AudioStreamPlayer.new();

func _ready():
    update_slots()
    close()
    
func update_slots(): 
    for i in range(min(invi.items.size(), slots.size())):
        slots[i].update(invi.items[i])
    
func _process(delta):
    if Input.is_action_just_pressed("Inventory"):
        if is_open:
            close()
            Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
            self.add_child(speler);
            speler.stream = load("res://Sounds/Inventory.wav");
            speler.play();
        else:
            open()
            Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
            self.add_child(speler);
            speler.stream = load("res://Sounds/Inventory.wav");
            speler.play();
            
func open():
    self.visible = true
    is_open = true
    
func close():
    visible = false
    is_open = false

inventory_slot.gd:

extends Panel


@onready var item_visual: Sprite2D = $CenterContainer/Panel/item_display

func update(item: InviItem):
    if item:
        item_visual.visible = false
    else:
        item_visual.visible = true
        item_visual.texture = item.texture

Can you show screenshots of your scene structure and the setups of your .tres files? You can add a couple of print statements, e.g. here to see what’s printed (if anything)

func update(item: InviItem):
    print(item)
    if item:
        item_visual.visible = false
    else:
        print(item.texture)
        item_visual.visible = true
        item_visual.texture = item.texture

I can show screenshots and the .tres files. Sorry for not showing them up at first.

Inventory.tscn

inventory_slot.tscn
Screenshot (296)

LemonPepperWings.tres

SOOOOO sorry for the multiple posts, but I’m a new user and I wasn’t able to post multiple images in one post.

PlayerInvi.tres

I don’t see anything particularly wrong with your setup.
Did you try the code I suggested previously?

I have tried it, it did output the word “item” eight times. However, while looking at this, I noticed that I was getting errors in the debug.

Here’s what they look like:

W 0:00:01:0362 The parameter “event” is never used in the function “_input()”. If this is intended, prefix it with an underscore: “_event”.
UNUSED_PARAMETER
flashlight.gd:5

W 0:00:01:0399 The parameter “delta” is never used in the function “_process()”. If this is intended, prefix it with an underscore: “_delta”.
UNUSED_PARAMETER
inventoryUI.gd:17

E 0:00:09:0997 inventoryUI.gd:22 @ _process(): Can’t add child ‘@AudioStreamPlayer@2’ to ‘Inventory UI’, already has a parent ‘Inventory UI’.
<C++ Error> Condition “p_child->data.parent” is true.
<C++ Source> scene/main/node.cpp:1568 @ add_child()
inventoryUI.gd:22 @ _process()

E 0:00:13:0048 inventoryUI.gd:28 @ _process(): Can’t add child ‘@AudioStreamPlayer@2’ to ‘Inventory UI’, already has a parent ‘Inventory UI’.
<C++ Error> Condition “p_child->data.parent” is true.
<C++ Source> scene/main/node.cpp:1568 @ add_child()
inventoryUI.gd:28 @ _process()

These 2 are not errors, only warnings. These are pretty self explanatory.

These are errors that you should ideally handle

You can change your inventoryUI.gd script to avoid these errors

extends Control

@onready var invi: Invi = preload("res://Player/UI/Inventory/PlayersInvi.tres")
@onready var slots: Array = $MarginContainer/VBoxContainer/GridContainer.get_children()

var is_open = false 
var speler = AudioStreamPlayer.new();

func _ready():
    update_slots()
    close()
    add_child(speler);
    speler.stream = load("res://Sounds/Inventory.wav");

func update_slots(): 
    for i in range(min(invi.items.size(), slots.size())):
        slots[i].update(invi.items[i])
    
func _process(delta):
    if Input.is_action_just_pressed("Inventory"):
        if is_open:
            close()
            Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
        else:
            open()
            Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
        speler.play();
            
func open():
    visible = true
    is_open = true
    
func close():
    visible = false
    is_open = false

After you run the game, try to look in the Remote tab to see how it all looks like and if your textures are properly loaded in the slots.
obraz

this may sound like a stupid question, but how do I see if the textures are loading correctly within the Remote tab? I’m still a little new to godot.

You’re loading the texture through code, so after starting the game go to the Remote tab, find your “inventory slots” in the scene tree and check if the correct texture is loaded in the texture property of the node. Something like here, but your tree will be much bigger:
obraz

Okay, I looked it up, and noticed that there was absolutely NO texture in the display sprite. So that means that the image isn’t showing up at all.

I just noticed an error in the inventory_slot.gd script. You had your if statement reversed, should be this way:

extends Panel

@onready var item_visual: Sprite2D = $CenterContainer/Panel/item_display

func update(item: InviItem):
    if item:
        item_visual.visible = true
        item_visual.texture = item.texture
    else:
        item_visual.visible = false

Try now

HOLY CANOLI IT WORKS! THANK YOU SO MUCH

Legit, this was a giant problem for me since I started working on the inventory system for so long now. I’m very grateful you helped me work this out. I can’t believe the error was such a simple solution, lesson learned I guess.

Thank you so very very very much. :heart::heart::heart::heart:

1 Like