[Bug] InventoryGridIndexDataSaver does not save

avant

New member
I am trying to get the exact order of the item slots saved for a chest. I've added the InventoryGridIndexData and InventoryGridIndexDataSaver components. This keeps the order correct during gameplay but throws a nullexception while saving.

Steps to reproduce:
- In the UltimateInventorySystem demo scene, add the InventoryGridIndexData and InventoryGridIndexDataSaver components to a chest
- Add the reference to the Inventory in the InventoryGridIndexData component
- Add the reference to the InventoryGridIndexData component in the InventoryGridIndexDataSaver component
- Try to save the game

Notes:
The main issue is coming from m_InventoryGridIndexData.GridIDTabIndexedItems being null during the SerializeSaveData. That is a dictionary that does start as null, but gets initialized in the SetGridIndexData function on the InventoryGridIndexData. This can also cause issues because SetGridIndexData would need to be called before saving, so a player would have to open a chest to try and initialize it for example.
However, even if a player does open the chest to initilize the GridIndexData, it will still return null and the same saving issue happens


Discussion on Discord with some images:
 
Than you for reporting this bug.

Here are the changes I made to fix it

In InventoryGridIndexDataSaver



Code:
/// <summary>
/// Serialize the save data.
/// </summary>
/// <returns>The serialized data.</returns>
public override Serialization SerializeSaveData()
{
    if (m_InventoryGridIndexData == null) { return null; }
    m_InventoryGridIndexData.Initialize(false);

Code:
/// <summary>
/// Deserialize and load the save data.
/// </summary>
/// <param name="serializedSaveData">The serialized save data.</param>
public override void DeserializeAndLoadSaveData(Serialization serializedSaveData)
{
    if (m_InventoryGridIndexData == null) { return; }
    m_InventoryGridIndexData.Initialize(false);

Initialize(false) means it will initialize the component if it is not yet initialized. The "false" referes to "forcing initialization"

Inside the InventoryGridIndexData you'll need to change the Initialize function to :
Code:
/// <summary>
/// Initialize the component.
/// </summary>
/// <param name="force">Force the component to reinitialize.</param>
public virtual void Initialize(bool force)
{
    if(m_IsInitialized && !force){ return;}
    
    if (m_Inventory == null) { m_Inventory = GetComponent<Inventory>(); }
    if (m_GridIDTabIndexedItems == null) {
        m_GridIDTabIndexedItems = new Dictionary<(int, int), InventoryGridIndexer>();
    }
    m_IsInitialized = true;
}

That seems to have solved it for me but if you have other issues let me know
 
I am still not able to save the inventory grid and load it properly. These changes allow the code to run without trowing exceptions, but my end goal is to save the inventory grid index data so if I then re-launch the project and load that save, the items are in the same place. I can recreate this in the demo scene.

Steps to reproduce:
- In the UltimateInventorySystem demo scene, add the InventoryGridIndexData and InventoryGridIndexDataSaver components to a chest
- Add the reference to the Inventory in the InventoryGridIndexData component
- Add the reference to the InventoryGridIndexData component in the InventoryGridIndexDataSaver component
- Add an InventorySaver component to the chest
- Play the game, and go to the chest you added the components to and take something out
- Open the menu and save the game
- Stop the game
- Replay the game, and then open the menu and load the game
- The chest will show the correct items, but the slots are not saved so items will be re-arranged
 
I'm sorry I should have tested more thouroughly.
I made a silly mistake in the code. The loaded data was never applied to the component...

You'll need to add this line of code:
Code:
m_InventoryGridIndexData.GridIDTabIndexedItems = gridIDTabIndexedItems;

at the end of the DeserializeAndLoadSaveData function in the Saver right before calling UpdateInventory.


Also one other thing, you will need to make sure you ItemInfoGrid has an ID that is NOT -1:

1724148337018.png

Otherwise the data will not be saved. It's specified here:
1724148391864.png

I hope that fixes the issues!
 
Back
Top