I'm trying to create a savable tree data structure.

I derived my tree nodes from Resource so I can easily persist and reload the tree. Each node needs to be aware of its parent, so they have a var parent: MyTreeNode property.

This structure works in code, but I can't save it because of the resource recursion (node has a child, child has the parent: Recursion detected, unable to assign resource to property.). I tried assigning the parent as a weakref but it just gets set to null when assigned to the resource.

What is the best way to represent this so that I can save the tree as a resource? If it's not possible to save it in this form, is there a suitable place on my custom resource where I can walk the tree and initialize all the parent properties? Maybe a setter on the children array?