Deserialize instance references

I have a component that allows you to select a set of components and stores the reference (IGH_DocumentObject) internally.
To keep this data stored internally (after closing and opening the file) I am serializing them using the InstanceGuid, is there a better way? The problem comes when deserializing, I retrieve the Guids but at that time have not yet created instances or loaded the entire document, I guess. Then I get the object reference error when I try to retrieve instances from the guids.

So, what’s the right way to do this? I have to pick up the guids and wait for the document to finish loading and then retrieve the IGH_DocumentObject from the InstanceGuids? How would I do this? With the Grasshopper.Instances.DocumentServer.DocumentAdded or Grasshopper.Instances.ActiveCanvas.Document.ObjectsAdded or other event?


Yep, that’s the right way. Incidentally if you keep InstanceIds locally, you must implement the IGH_InstanceGuidDependent interface in your component and handle the InstanceGuidsChanged() method, because sometimes a document will modify the instance ids of all objects to prevent collisions with objects in another document. Although if you only get the IDs when you serialise and then forget them again soon after deserialisation, you don’t have to keep taps on changing ids.

A good time to try and find the actual object instances based on their ids would be the SolutionStart event. At this point all objects and their connections have been deserialized into runtime instances.

Thank you David :slight_smile: