Saving "Aot Dictionary" type

Discussion and help for Easy Save 3
Post Reply
unity_user5192182
Posts: 4
Joined: Sat Jan 29, 2022 8:03 am

Saving "Aot Dictionary" type

Post by unity_user5192182 »

Hi,

I'm using Unity Visual Scripting (VS) in Unity 2021.2. I have a variable of type "Aot Dictionary".

When I try to save that variable using ES3, it's saved as NULL even though the variable has a non-NULL value. Can you please tell me how I can save such a variable? (Btw, I'm able to save and load a "Float" variable without any issues)

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

Re: Saving "Aot Dictionary" type

Post by Joel »

Hi there,

AotDictionary isn't in our Supported Types list and Unity doesn't expose the required fields or interfaces for it to be automatically serialisable. However, in theory adding an ES3Type like this to your Plugins/Easy Save 3/Scripts/Types/Unity Types/ folder should make it supportable:

Code: Select all

#if UNITY_2021_1_OR_NEWER
using System;
using UnityEngine;
using Unity.VisualScripting;
Using System.Linq;

namespace ES3Types
{
	[UnityEngine.Scripting.Preserve]
	public class ES3Type_AotDictionary : ES3Type
	{
		public static ES3Type Instance = null;

		public ES3Type_AotDictionary() : base(typeof(AotDictionary))
		{
			Instance = this;
		}

		public override void Write(object obj, ES3Writer writer)
		{
			AotDictionary casted = (AotDictionary)obj;
			writer.WriteProperty("Keys", casted.Keys.Cast<object>.ToList());
			writer.WriteProperty("Values", casted.Values.Cast<object>.ToList());
		}

		public override object Read<T>(ES3Reader reader)
		{
			var dict = new AotDictionary();
			var keys = reader.ReadProperty<List<object>>();
			var values = reader.ReadProperty<List<object>>();

			for(int i=0; i<keys.Count; i++)
				dict.Add(keys[i], values[i]);

			return dict;
		}
	}
}
#endif
I'm not at a computer with Unity installed at the moment as it's outside of office hours, but let me know how you get on and if it doesn't work for you I'll look into it further in the morning.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
unity_user5192182
Posts: 4
Joined: Sat Jan 29, 2022 8:03 am

Re: Saving "Aot Dictionary" type

Post by unity_user5192182 »

Thanks Joel, for the quick reply! Will try it and update you.
unity_user5192182
Posts: 4
Joined: Sat Jan 29, 2022 8:03 am

Re: Saving "Aot Dictionary" type

Post by unity_user5192182 »

Hi Joel,

It's working well :)

Just FYI - a couple of errors were thrown due to typos and such, I was able to clean it up.

Thanks again for your quick help, I really appreciate it very much!
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving "Aot Dictionary" type

Post by Joel »

Glad that worked, despite the typos :)

I’ll fix them when I’m back in the office in the morning and push the support for AotDictionary to the next update.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply