Saving/Loading, Serializing SortedDictionaries...

Hey there, I’ve just started getting into saving and loading data, and I understand the basics of it, but what’s kind of worrying me down the line is that in my scenes/maps I’m storing my objects in this fashion…

Firstly, through a class:

public class MG_O_Object {
    public GameObject O_Object_gameObject { set; get; }
    public O_Lists.TriggerType triggerType { set; get; }
    public List<string> O_List { set; get; }
    public Vector3 storedPosition;
    public Vector3 storedRotation;
}

And then writing it into a SortedDictionary:

foreach (GameObject gameObject in GameObject.FindObjectsOfType(typeof(GameObject))) {
                    MG_O_Object MG_O_object = new MG_O_Object () {
                        O_Object_gameObject = gameObject,
                        triggerType = gameObject.GetComponent<O_Lists> ().triggerType,
                        O_List = gameObject.GetComponent<O_Lists> ().O_List,
                    };
                    MG_O_Objects.Add(objectNumber+gameObject.GetComponent<O_Lists>().executionOrder,MG_O_object);
        objectNumber += 1;
}

I was using this to work with as I naively thought that when I got to save/load dictionaries would be easily to serialize.

Now I’m concerned because I’m reading and hearing all over the internet that you can’t serialize dictionaries, let alone sorted dictionaries (which are hope aren’t even more difficult to deal with).

What kinds of options do I have available to me to get the data to and from a file then safely? Do I have to take the SortedDictionary, convert it to a List and vice versa?

why do you need a sorted dictionary?

if necessary, you could just also store it’s index as a property in MG_O_Object, and rebuild your dictionary based on that index.

My sentiments exactly, I should have just used sorted lists instead or something. The reason I was using it, is I had been reading at the time that it was faster, but I’m now reading the difference is negligible. I thought that down the line for some reason they’d be easier, but now I’m seeing I should have just used lists. A tip to noobs like me: use lists and sorted lists instead, stay away from dictionaries for games.

dictionaries are fine, just not for serialization. if you ever need to look something up by key, a dictionary is way better than ripping through a list. often times with serialization, i’ll serialize as a list, but on awake or start, i’ll build a dictionary from my list data if i need it at my fingertips.

When I need to use a dictionary I generally just use key, value pairs as a custom data structure, then on awake build my dictionary again (ofc you need to handle possible duplicates)

Before using “exotic” data structures read what they actually do for you as you seem to be implying that sortedDictionaries are better than regular dictionaries (which they are not for general use)

Have fun

Scrap what I wrote before, you should do it this way.

Is it possible to serialize SortedLists, then? I can easily just re-write my sorted dictionaries as sorted lists instead.

Why would you want to serialize sorted lists?

So he can see it in inspector most likely.