Save System Pickup Problem

Before I get too far into this I just want to say that I watched the Inventory course fully before trying any integration into my own project, and now that I’m having some hiccups with the save system I’m going to be visiting the RPG core course where it was mentioned there would be more information on that subject.

I’m still working my way through my second watch of the Inventory course while I integrate it, but I’ve noticed something that didn’t happen when I followed along with the project files provided in the class and I want to try to figure out what I’m doing wrong.

In this first video there are 13 pickups. I pick them up, save…

…and reload in the second video. The pickups do not stay saved in my inventory, but more interestingly; the same two items disappear every time I reload and I can’t think of why that would be.

Pickup Spawner Scripts
pickupspawner
Pickup Scripts
Pickup
Scriptable Object Example
scriptableobjectexample
Scripts Attached To Player
PlayerScripts
Save System on Core
Core
Save System Prefab Scripts

If I need to provide screenshots of anything else I’m happy to.

The thing that keeps coming to my mind, I’m not sure where it is in the course, was something mentioned about making sure the save system knew what item pickups had been picked up. I’m going to continue watching the course to try to figure it out.

Update: I added all of the pickup spawners as prefabs, and now nothing disappears when I reload- They still do not stay saved in the inventory however.

Hmmm… so far, everything looks right (in setup, obviously not in what’s going on.

Let’s start with the obvious, are there any errors in the console window, especially when saving or loading a scene?

Are there any changes you’ve made to the underlying scripts? (Inventory, SaveableEntity, SavingWrapper, SavingSystem, adn PickupSpawner?

1 Like

These are the errors I am currently getting when I hit play, and when I save and reload.


The ones regarding the mana and health colors being invalid have to do with the health minimum not being set yet and I haven’t worried about that.
The first error though sends me to my movement script where I added in the capture and restore state methods, as I mention further below.
ErrorRefer

Which I believe has something to do with the Action Scheduler interfacing with the animation controller used for the course, but I might be wrong. (I haven’t fully taken that course yet, so I’m not 100% certain so I haven’t modified anything yet)

The only thing I did was modify one of my scripts to allow it to save the location, as follows:
MovingCode
MovementCodeAddon

When there is an error in the coroutine that loads and saves the game, it halts progression of the saving or loading of the game. It’s just a quirk in Unity. In this case, since you’re not currently using the ActionScheduler, just go ahead and comment that line out and see what happens. You might need to delete the save file.

1 Like

Alright, I tried that out and I think I’m seeing just what you’re talking about in action.
Here is what I see when I reload Unity after commenting out the code for the action scheduler as well as remove the action scheduler component from my player gameobject.


This is what I see after deleting the old save, reloading, picking up the pickups in the scene, saving and reloading once again.

It seems like the save system stopped just as you described due to the error being thrown, as now my inventory looks like this and there are no pickups on the ground.

Here is the code reference the errors bring me to, on the Action Slot UI script.

You actually didn’t have an Action Scheduler attached to the player in the first place. My best guess is that you deleted the ActionStore instead.

You are 100% right about that, I just put it back on my player gameobject and tested things out, its still behaving the way it previously was.

I have all of the action slots prefabbed and set up with their child inventory item to be their default icon.
setup
actionslotui
hilight

I mention that because of the error that was thrown previously. I’m going to take a break and get my son to bed, but I really appreciate you taking the time to help me out.

by previously was… do you mean it’s still throwing null reference errors accessing the Action Store or the initial behavior at the top of the post?

My apologies for it being unclear, I was being too hasty in responding. It is no longer throwing errors referring to the action slot after re-attaching the action store to the player, but the inventory is still behaving like this after deleting the save, re-launching, picking up items, saving and reloading.

So it looks like Inventory either isn’t saving, restoring, or displaying the restored state correctly.
Paste in your Inventory.cs CaptureState and RestoreState methods. Bear with me, we’re going to be making some modifications to help us visualize what may be happening.

1 Like
 object ISaveable.CaptureState()
        {
            var slotStrings = new InventorySlotRecord[inventorySize];
            for (int i = 0; i < inventorySize; i++)
            {
                if (slots[i].item != null)
                {
                    slotStrings[i].itemID = slots[i].item.GetItemID();
                    slotStrings[i].number = slots[i].number;
                }
            }
            return slotStrings;
        }

        void ISaveable.RestoreState(object state)
        {
            var slotStrings = (InventorySlotRecord[])state;
            for (int i = 0; i < inventorySize; i++)
            {
                slots[i].item = InventoryItem.GetFromID(slotStrings[i].itemID);
                slots[i].number = slotStrings[i].number;
            }
            if (inventoryUpdated != null)
            {
                inventoryUpdated();
            }
        }

Ok, let’s add some debugs to see if the system itself is saving/restoring correctly (I suspect this will pass, and move us to the next place to check)

In CaptureState, in ach of the if(slots[i]!=null) block:

Debug.Log($"Capturing slot {i} {slots[i].item.GetDisplayName}/{slots[i].item.GetItemID()}";

In Restorestate, in the loop after slots[i].number = slotstrings[i].number;

if(slots[i].item!=null)
{
    Debug.Log($"Restoring slot {i} {slots[i].item.GetDisplayName}/{slots[i].item.GetItemID()}";
}

We’re looking to see that you get the same things capture as restored.


I’m assuming this is the way you wanted me to set up the debug statements you provided, however the GetDisplayName doesn’t seem to work on my end.

This is what happens when I type these things right into the window instead of a code edtor. Add () to the GetDisplayName so it read Item.GetDisplayName()

1 Like

No worries, I figured it was something like that; thank you for that.


So it seems like it isn’t restoring properly, as when I hit load it doesn’t pop up a debug log, it only complains about my orbs not initializing their value.

I commented out the line on my orbs trying to set the scale of their color just to make sure that it wasn’t causing the save / load system a problem somehow. Still doesn’t display the debug when I load.

Hmmm put this at the top of Inventory.RestoreState()

Debug.Log($"RestoreState!");


That message plays whenever I hit reload now, just to make sure I put everything in the right place:

Something I hadn’t thought of. Are all of your inventory items in a Resources folder?

1 Like

Privacy & Terms