Serialization of individual nodes

Hi,
I am probably doing something the way it wasn’t designed for, but bear with me :slight_smile:

Considering a case in which:

  1. I have a MonoBehaviour that has a reference to a single GraphNode (custom subclass of LevelGridNode marked with a Serializable attribute).
  2. The field is marked with the SerializeField attribute.
  3. I Instantiate (clone) the original object and after accessing the Node’s position of this cloned object it returns 0,0,0 as the Int3 isn’t marked as Serializable.

It would be awesome if I could do something like the above. Is there a reason why I cannot? Are there any workarounds?

EDIT:
I have now worked around this by implementing ISerializationCallbackReceiver and storing/restoring the position manually myself, but it seems like a terrible hack. I still would like to know what is the recommended way.

Hi

Nodes are not intended to be serialized that way. Even node indices are not guaranteed to remain the same.

If you are using a grid graph, I would instead recommend that you serialize the node.XCoordinateInGrid and node.ZCoordinateInGrid and then load the node using gridGraph.GetNode(x, z). Alternatively, store node.NodeInGridIndex and then load it using gridGraph.nodes[index].

1 Like

Yeah, I ended up doing just that. Thanks for the confirmation.