Saving and loading inventory

kendogar

New member
Hello again,

sorry, but could you help me get saving and loading my inventory running?

I have added the Save System Manager and the Inventory System Manager Item Saver to the scene and set up like this:

1629124776025.png

Aditionally my character has the Inventory Bridge Saver set up as follows:

1629124817385.png

Now I'm fairly sure the inventory gets saved, as there are references to my items in the save.json (attached to this post as .zip with save and debug file).

But upon restarting the game I'm getting the following NullReferenceException: NullReferenceException: Object reference not set to an instance of an object

This happens in the for-loop of this function:

Code:
public void Equip(int categoryIndex, int itemSetIndex, bool forceEquipUnequip = false, bool immediateEquipUnequip = false)
{
    // The correct ItemSet has been found. Equip the item using the correct EquipUnequip ability.
    for (int i = 0; i < m_EquipUnequipAbilities.Length; ++i) {
        var abilityCategoryIndex = m_EquipUnequipAbilities[i].ItemSetCategoryIndex;
        if (abilityCategoryIndex >= 0 && abilityCategoryIndex != categoryIndex) {
            continue;
        }
        m_EquipUnequipAbilities[i].StartEquipUnequip(itemSetIndex, forceEquipUnequip, immediateEquipUnequip);
    }
}

Can you help me get saving and loading up and running, please?
 

Attachments

  • save.zip
    2.5 KB · Views: 0
When you have an error, please show the full error message, this will make it a lot easier for me to know where the error started and on what line it broke.

By any chance, have your tried loading the save data manually, after one frame, instead of using the "Load On Start" options?
I'm wondering if the bridge might not be initialized yet when you start loading the data.

Having the full error message might help me be sure whether that's the case or not
 
Sorry, I should have been more specific with that error.

However given your heads up I managed to resolve the issue, as it was indeed the bridge not yet being intitialized! To be more specific, the bridges "m_EquipUnequipAbilities" was still null and when the Loader tried to unequip every item using that action it crashed of course. I am now waiting one frame before loading the inventory to bypass this.

But should it not work without that workaround by code? Did I setup my scene the wrong way so that the built in save/load functionality crashes in what I guess is a default usecase?

Either way: While loading the inventory and re-equipping the last equipped item does work now, the position of the item on the inventory grid does not get restored. Can I somehow achieve the position being restored or would I have to extend the saver/loader to make the magic happen?

Kind regards
 
But should it not work without that workaround by code? Did I setup my scene the wrong way so that the built in save/load functionality crashes in what I guess is a default usecase?
Making a flexible save system is complicated. the order in which things are initialized, saved, loaded, etc... is important. So Having the Load On Start can sometimes work if everything i initialized previously, but that's not always the case.
Sometimes you need to load the data for the items before you can initialize the character for example.

Our save system is meant to be either built upon or used within another "main" save system. It's a lot better to have a GameManager that defines when things are loaded/saved, especially if you use multiple scenes additive or not. It gives you a lot more control.

Either way: While loading the inventory and re-equipping the last equipped item does work now, the position of the item on the inventory grid does not get restored. Can I somehow achieve the position being restored or would I have to extend the saver/loader to make the magic happen?
I recently added an InventoryGridSaver, I believe it is available in the lastest update v1.1.8. You can add that component next to your InventoryGrid and it should get saved automatically.
If you are using an ItemShapeGrid instead, I believe I have a different Saver component for that.

You can check out the built-in saver components by checking what scripts are inheritting the "SaverBase" abstract class.

I hope that helps
 
Alright thanks for clarification!

I did now integrate the saving part into my own save logic and it works flawlessly. Appreciate your ongoing support when having issues.
 
Top