Scriptable object reference not saved

Discussion and help for Easy Save 3
funtomata
Posts: 26
Joined: Tue Feb 23, 2021 4:25 pm

Scriptable object reference not saved

Post by funtomata »

Hi,
I just got started with Easy Save so I'm probably doing everything super wrong.

So on my scene, some objects have an "interactable" component, witch references an "InteractionState" scriptable object. That InteractionState is updated at runtime when some stuff happens in the game. The new reference is not a runtime SO, it's an asset SO.

I configured the AutoSave to save the InteractionState reference, then played the game to trigger the interaction state change (let's call them state1 and state2), stopped the game and restarted.

My expectation was that the interaction state should be set to state2 when restarting, but it was set to state1. Note that other things, like the transform position of my character, were properly saved and loaded.

I checked in the Reference Manager to see if state2 was in the list of managed references, and it is there.

I also can't think of a reason why some other part of my code would reset the reference after easy save would have loaded it.

Am I missing something in the bigger picture?
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Scriptable object reference not saved

Post by Joel »

Hi there,

When you reference an SO in Assets from a scene, Unity creates an instance of that SO at runtime rather than referencing the one in Assets. This means that it will create a new instance each time, which won't exist between sessions.

Instead you would need to create some way of identifying each SO and store that (for example, a unique string). Then when loading, use that unique string to get the appropriate SO.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
funtomata
Posts: 26
Joined: Tue Feb 23, 2021 4:25 pm

Re: Scriptable object reference not saved

Post by funtomata »

I also have problems with saving another scriptable object which contains a list of items (my inventory SO)

My character has an inventoryManager monobehaviour which references that inventory.

Here's the code for both:

Code: Select all

    public class Inventory : ScriptableObject
    {
        [SerializeField] private string guid = System.Guid.NewGuid().ToString();
        [SerializeField] private List<Item> startingItems;
        public List<Item> items;

        public void Load()
        {
            items = ES3.KeyExists("test") ? ES3.Load<List<Item>>("test") : new List<Item>(startingItems);
        }

        public void Save()
        {
            ES3.Save("test", items);
        }
    }

Code: Select all

    public class InventoryManager : MonoBehaviour
    {
        public Inventory inventory;

        private void Start()
        {
            inventory.Load();
        }
        public void Add(Item item)
        {
            inventory.items.Add(item);
            inventory.Save();
        }

        public void Remove(Item item)
        {
            inventory.items.Remove(item);
            inventory.Save();
        }
    }
Add and Remove methods are properly called when I pick up an object, but when I call Load, it never finds the key and loads the starting list of items instead. I tried to replace the "guid" key by a hardcoded string just to make sure, and it doesn't work.
Last edited by funtomata on Tue Feb 23, 2021 6:43 pm, edited 1 time in total.
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Scriptable object reference not saved

Post by Joel »

Hi there,

You appear to be saving the data with the key "test", but trying to load it with the guid as the key. You need to use the same key to load as you used to save.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
funtomata
Posts: 26
Joined: Tue Feb 23, 2021 4:25 pm

Re: Scriptable object reference not saved

Post by funtomata »

Joel wrote: Tue Feb 23, 2021 6:30 pm Hi there,

You appear to be saving the data with the key "test", but trying to load it with the guid as the key. You need to use the same key to load as you used to save.

All the best,
Joel
It's a copy paste error in the forum post, I fixed it now, but my code was correct. Still doesn't work.

Could the problem be due to the fact that Item is a scriptable object?

Code: Select all

   [CreateAssetMenu (menuName = "Itemisation/Item")]
    public class Item : ScriptableObject
    {
        public string itemName;
        public Sprite sprite;
        public Sprite hiddenSprite;

        public void OnValidate()
        {
            if (itemName != "")
                itemName = name;
        }
    }
Edit:
I tried to save a simple int and it doesn't work either so it has nothing to do with Item. The "test" key is just never created.

There's this error in the console whenever I stop my game, could it be related?
FormatException: Expected '}', found ']'.
ES3Internal.ES3JSONReader.ReadCharIgnoreWhitespace (System.Char expectedChar) (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3JSONReader.cs:374)
ES3Internal.ES3JSONReader.EndReadObject () (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3JSONReader.cs:116)
ES3Reader.ReadObject[T] (ES3Types.ES3Type type) (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:251)
ES3Reader.Read[T] (ES3Types.ES3Type type) (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:278)
ES3Types.ES3CollectionType.ReadICollection[T] (ES3Reader reader, System.Collections.Generic.ICollection`1[T] collection, ES3Types.ES3Type elementType) (at Assets/Plugins/Easy Save 3/Scripts/Types/Collection Types/ES3CollectionType.cs:52)
ES3Types.ES3ArrayType.Read (ES3Reader reader) (at Assets/Plugins/Easy Save 3/Scripts/Types/Collection Types/ES3ArrayType.cs:36)
ES3Reader.Read[T] (ES3Types.ES3Type type) (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:274)
ES3.Deserialize (ES3Types.ES3Type type, System.Byte[] bytes, ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:775)
ES3.Deserialize[T] (System.Byte[] bytes, ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:764)
ES3File.Load[T] (System.String key, T defaultValue) (at Assets/Plugins/Easy Save 3/Scripts/ES3File.cs:243)
ES3.Load[T] (System.String key, T defaultValue, ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:413)
ES3AutoSaveMgr.Load () (at Assets/Plugins/Easy Save 3/Scripts/Auto Save/ES3AutoSaveMgr.cs:81)
ES3AutoSaveMgr.Awake () (at Assets/Plugins/Easy Save 3/Scripts/Auto Save/ES3AutoSaveMgr.cs:100)
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Scriptable object reference not saved

Post by Joel »

Hi there,

Please could you create a new, basic project with a simple scene which replicates this and private message it to me with instructions?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
funtomata
Posts: 26
Joined: Tue Feb 23, 2021 4:25 pm

Re: Scriptable object reference not saved

Post by funtomata »

I quickly put together a project mimicking what I'm doing (exact same code) but it works properly there :-(

I also don't get the bracket error. So either there's something corrupt in my ES3 on my main project, or there's some ES3 components interfering with the save somehow.

I'm going to roll back my project to before I installed ES3 and do things one at a time to see if I can reproduce the problem there.
funtomata
Posts: 26
Joined: Tue Feb 23, 2021 4:25 pm

Re: Scriptable object reference not saved

Post by funtomata »

fyi, after rolling back my project and strictly adding the inventory save code without messing with any auto save, I don't have any more problems.

I'll keep you informed if the problem comes back after I added auto saves
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Scriptable object reference not saved

Post by Joel »

Glad you managed to get it working. Let me know if you run into any other problems.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
funtomata
Posts: 26
Joined: Tue Feb 23, 2021 4:25 pm

Re: Scriptable object reference not saved

Post by funtomata »

Joel wrote: Tue Feb 23, 2021 8:20 pm Glad you managed to get it working. Let me know if you run into any other problems.

All the best,
Joel
Hey Joel,
so I found how to reproduce the bug. It seems that adding autosave causes problems. I don't see anything in the docs saying that you shouldn't use AutoSave in conjunction with AS3.Save is there? I'll send you a PM with a project and steps to reproduce
Post Reply