Page 1 of 1

Saving "Aot Dictionary" type

Posted: Thu Feb 10, 2022 7:20 pm
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!

Re: Saving "Aot Dictionary" type

Posted: Thu Feb 10, 2022 8:19 pm
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

Re: Saving "Aot Dictionary" type

Posted: Thu Feb 10, 2022 10:03 pm
by unity_user5192182
Thanks Joel, for the quick reply! Will try it and update you.

Re: Saving "Aot Dictionary" type

Posted: Thu Feb 10, 2022 11:09 pm
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!

Re: Saving "Aot Dictionary" type

Posted: Thu Feb 10, 2022 11:37 pm
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