Packing and Unpacking Object References

From C4 Engine Wiki

Jump to: navigation, search

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:

  1. Any object that is part of the graph needs to implement methods of the Packable base class.
  2. Any object keeping references must override the Prepack() function to add objects with references.
  3. Engine will call the Prepack() function before calling the Pack() function.
  4. The engine will compress duplicates out of the list, and assign each Packable object an "index".
  5. Inside the Pack() function, each object reads the object index and writes that as a reference.
  6. 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.
  7. No object can participate in more than one Pack() operation at the same time.
  8. The object index (on packable) is no longer meaningful after the callback to the unpack hook.
Personal tools