StackOverflowException - how to save circular references?

Discussion and help for Easy Save 3
Post Reply
bzwill
Posts: 7
Joined: Mon May 14, 2018 8:58 pm

StackOverflowException - how to save circular references?

Post by bzwill »

I have a Cell class with a field of type CellNeighbours (neither are MonoBehaviours). This class holds a list of cells.

If I add both of these types and have Cell save neighbours and neighbours save cells, I get a stack overflow - It doesn't seem that ES3 saves these by referenceID in the graph?

I have tried to see how to override this behavior, but I cant work out where to change this.

I have tried using ES3.ReferenceMode.ByRef in the writer.WriteProperty() call, but the problem persists.

Any help? :)
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: StackOverflowException - how to save circular references

Post by Joel »

Hi there,

The Stack Overflow occurs because there is a cyclic reference (i.e. A Cell class references itself). Only objects which derive from UnityEngine.Object can be stored by reference, so it will be stored by value.

This issue isn't just limited to Easy Save but also occurs in Unity's serialisation. However, Unity suggest that we can store our own references to objects. One way of doing this would be to add all of your cells to an array, and then store the index of the Cell in this array, rather than the Cell itself.

The easiest way to get Easy Save to save this index would be to go to Window > Easy Save 3 > Types, create an ES3Type for your Cell class. This will create an ES3Type_Cell.cs file in Assets/Easy Save 3/Types which you can modify to save your index instead of the object itself.

Hope this helps!

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