Issue with Inventory System in Godot 4.2 - Item adds to Slot 0 but Disappears upon Game Restart

Godot Version

v4.2.2.stable.official [15073afe3]

Question

Hey everyone!

Lately, I’ve been working on a game in Godot 4.2 and encountered an issue with the inventory system. So, when I add an item to Slot 0, everything seems to work fine - the item appears in the slot as it should. However, upon restarting the game, I find that Slot 0 is empty and has reverted to its default state.

Initially, I thought the problem might lie in how the game state is being saved, but upon checking the code, everything seems to be in order. Here’s he source code:
Scroll Down for screen shots.

InventoryData.gd

extends Resource
class_name InventoryData
 
 
signal inventory_updated(inventory_data: InventoryData) # Sygnał emitowany po zmianie danych ekwipunku
signal inventory_interact(inventory_data: InventoryData, index: int, button: int) # Sygnał dla interakcji ze slotami
 
@export var slot_datas: Array[SlotData] # Tablica do przechowywania danych slotu
 
 
func load_inventory():
    var inventory_data = load("res://test_inventory.tres") as InventoryData
    
    if inventory_data != null:
        slot_datas = inventory_data.slot_datas
        
        
func save_inventory():
    var inventory_data := InventoryData.new()
    inventory_data.slot_datas = slot_datas
    
 
        
# Pobranie danych z okreslonego slotu
func grab_slot_data(index: int) -> SlotData:
    var slot_data = slot_datas[index] # Pobierz dane ze wskazanego indeksu
 
    if slot_data: # Jeśli są dane
        slot_datas[index] = null # Usuń dane ze slotu
        inventory_updated.emit(self) # Wyemituj sygnał inventory_updated
        return slot_data # Zwróć dane slotu
    else:
        return null #Jesli brak danych, zwroc null
 
 
 
 
# Funkja upuszczenia danych slotu
func drop_slot_data(grabbed_slot_data: SlotData, index: int) -> SlotData:
    var slot_data = slot_datas[index]
    var return_slot_data: SlotData
    
    # Jeśli istnieją dane i mogą być całkowicie scalone
    if slot_data and slot_data.can_fully_merge_with(grabbed_slot_data):
        slot_data.fully_merge_with(grabbed_slot_data)
    else:
        # Jeśli brak istniejących danych lub nie można scalić całkowicie, zastąp dane slotu
        slot_datas[index] = grabbed_slot_data
        return_slot_data = slot_data
 
    inventory_updated.emit(self) # Wyemituj sygnał inventory_updated
    return return_slot_data # Zwróć potencjalnie zastąpione dane
 
 
 
# Funkja upuszczenia jednego przedmiotu ze slotu
func drop_single_slot_data(grabbed_slot_data: SlotData, index: int) -> SlotData:
    var slot_data = slot_datas[index]
 
    if not slot_data:
        slot_datas[index] = grabbed_slot_data.create_single_slot_data()
    elif slot_data.can_merge_with(grabbed_slot_data):
        slot_data.fully_merge_with(grabbed_slot_data.create_single_slot_data())
 
    inventory_updated.emit(self)
 
    if grabbed_slot_data.quantity > 0:
        return grabbed_slot_data
    else:
        return null
 
 
 
 
# Obsługa kliknięcia na slot
func on_slot_clicked(index: int, button: int) -> void:
    inventory_interact.emit(self, index, button)
 
func is_inventory_full() -> bool:
    for slot_data in slot_datas:
        if not slot_data:
            return false # Znaleziono pusty slot, więc ekwipunek nie jest pełny
    return true # W ekwipunku nie ma pustych slotów, więc jest pełny

Meteor.gd

extends Node3D
 
# Licznik trafień
var hit_count:int = 0
@onready var particles1 = $GPUParticles3D2
@onready var particles_red = $CrystalRedParticle
 
# Funkcja do aktualizacji licznika trafień
func increment_hit_count():
    hit_count += 1
    if hit_count >= 2:
        particles1.emitting = true
        particles_red.emitting = true
        await get_tree().create_timer(0.5).timeout
        queue_free()  # Usuń meteoryt z gry
        add_crystal() # Dodaje Red Crystal do ekwipunku
 
 
func _on_area_3d_area_entered(_area):
    particles1.amount_ratio = 8
    particles1.emitting = true
    particles1.amount_ratio = 60
    increment_hit_count()
    
func add_crystal():
    var inventory_data = load("res://test_inventory.tres")
    # Utwórz SlotData dla Czerwonego Kryształu
    var Red_Crystal = SlotData.new()
    Red_Crystal.item_data = load("res://Items/Red_Crystla.tres")
    Red_Crystal.quantity = randi_range(5, 15)
    # Dodaj Czerwony Kryształ do slotu 0
    inventory_data.drop_slot_data(Red_Crystal, 0)
    inventory_data.save_inventory()
    #inventory_data.save_slots("res://Manager/save.json")

Has anyone encountered a similar problem or knows of a solution? Any help would be greatly appreciated!

Thank you!


I think you forgot to save the inventory data properly

How can i do this?

You can find your answer in this page

1 Like

Thanks

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.