How to save new inventory size if it changes during play time

Hey,

I made a button which will increase the players inventorySize by a certain amount (1). While playing, you can see the size change in the inventory, but if I save and then play again, the inventory size is not saved. It’s the same size that I’ve set in the Inventory.cs inspector.

I am using the new JSon saving system that Brian made a tutorial for (and I am very grateful for that) and I thought that the inventory size and slot length would also get saved. Or at least, the new slots length would be set to the new inventory size.

I remember that we are initializing the slots in Awake, to the value of the inventorySize so I don’t know if this has an impact.

I really don’t know what else to try to make it work as I really want to enable the player to increase its inventory space at certain points, so I was hoping someone might be able to provide a solution.

Thank you.

The inventory slot size isn’t saved by default in my implementation of the Json saving system, but it easily can be. I’m at work, but when I get home, I’ll cobble a strategy for this.

Ok, thank you so much Brian!

Ok, this one isn’t as bad as I thought it would be:

We’re already using an IDictionary/JObject to represent the state of the inventory, so we’re just going to add a key/pair to represent the current inventory size:

Here’s our modified CaptureAsJToken

    public JToken CaptureAsJToken()
    {
        JObject state = new JObject();
        IDictionary<string, JToken> stateDict = state;
        stateDict["inventorySize"] = new JToken.FromObject(inventorySize); //Adding our inventory size
        for (int i = 0; i < inventorySize; i++)
        {
            if (slots[i].item != null)
            {
                JObject itemState = new JObject();
                IDictionary<string, JToken> itemStateDict = itemState;
                itemState["item"] = JToken.FromObject(slots[i].item.GetItemID());
                itemState["number"] = JToken.FromObject(slots[i].number);
                stateDict[i.ToString()] = itemState;
            }
        }
        return state;
    }

Then, it’s just a matter of fixing RestoreFromJToken to accomodate. We’ll swap a couple of lines to set the dictionary size after we map the dictionary:

    public void RestoreFromJToken(JToken state)
    {
        if (state is JObject stateObject)
        {
            IDictionary<string, JToken> stateDict = stateObject;
            //Only change the inventory size if a key is present, making this backwards compatable.
            if(stateDict.ContainsKey("inventorySize"))
           {
                  inventorySize = stateDict["inventorySize"].ToObject<int>(); 
           }
            slots = new InventorySlot[inventorySize];
            for (int i = 0; i < inventorySize; i++)
            {
                if (stateDict.ContainsKey(i.ToString()) && stateDict[i.ToString()] is JObject itemState)
                {
                    IDictionary<string, JToken> itemStateDict = itemState;
                    slots[i].item = InventoryItem.GetFromID(itemStateDict["item"].ToObject<string>());
                    slots[i].number = itemStateDict["number"].ToObject<int>();
                }
            }
            inventoryUpdated?.Invoke();
        }
    }

That’s the whole fix.

1 Like

Thank you Brian! It works like a charm! I can now safely add an idea that I had, but was blocked by the fact that the size was not saved.

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

Privacy & Terms