Packing and Unpacking Object References
From C4 Engine Wiki
If you implement a DAG (Directed Acyclic Graph) of objects, where some objects reference other objects, and you want to serialize all the objects as part of packing/unpacking, here's what you do:
- Any object that is part of the graph needs to implement methods of the Packable base class.
- Any object keeping references must override the Prepack() function to add objects with references.
- Engine will call the Prepack() function before calling the Pack() function.
- The engine will compress duplicates out of the list, and assign each Packable object an "index".
- Inside the Pack() function, each object reads the object index and writes that as a reference.
- Inside the Unpack() function, each object reads the object index, and then registers a callback per object index to set the different members, or add them to a collection etc.
- No object can participate in more than one Pack() operation at the same time.
- The object index (on packable) is no longer meaningful after the callback to the unpack hook.
