Deserialization Callback

Discussion and help for Easy Save 3
Post Reply
alchemist_wurzel
Posts: 5
Joined: Fri Nov 26, 2021 11:35 am

Deserialization Callback

Post by alchemist_wurzel »

Hi there!

Using Unity 2020.3.17 and EasySave 3.3.2f7.

We would like to avoid storing direct ScriptableObject references in our save file so we store their keys (string) and after deserialization we want to access our internal database to get the actual ScriptableObject reference.
For this, objects that are deserialized need to know when they are done being deserialized and the keys are present, much like the ISerializationCallbackReceiver would do for us in the Unity Editor. After deserialization they would then access the Database to get their needed ScriptableObject references.

Does EasySave provide an 'easy' way to do this or should we try to create a custom interface and modify the Load functions of ES3.cs to invoke our own OnAfterDeserialize method if said interface is implemented?

Cheers!
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Deserialization Callback

Post by Joel »

Hi there,

We have a slightly different method of doing this in Easy Save as we needed a solution which allows this to be achieved without the need to modify the class itself (as this isn't possible in most cases).

You can achieve this using an ES3Type. For example, by adding a script like this to your project you can manually control what happens before and after. Easy Save automatically finds this script and uses it when serializing and deserializing your ScriptableObject:

Code: Select all

using UnityEngine;

namespace ES3Types
{
	public class ES3UserType_MyScriptableObject : ES3Type
	{
		public static ES3Type Instance = null;

		public ES3UserType_MyScriptableObject() : base(typeof(MyScriptableObject))
		{
			Instance = this;
		}

		public override void Write(object obj, ES3Writer writer)
		{
			var so = (MyScriptableObject)obj;
			writer.WriteProperty("key", so.key);
		}

		public override object Read<T>(ES3Reader reader)
		{
			var key = reader.ReadProperty<string>();
			return GetYourScriptableObject(key);
		}
	}
}
(Of course, you'd need to modify this for your specific ScriptableObject)

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
alchemist_wurzel
Posts: 5
Joined: Fri Nov 26, 2021 11:35 am

Re: Deserialization Callback

Post by alchemist_wurzel »

Hi Joel, I will keep this in mind. Thank you for the quick response!
Post Reply