Referencing the instance GUID in Read method C#

Hi all,

I’m having some issues reading/writing a custom component and its properties on saving the GH document.
I’m trying to create a chunk like this:

> public override bool Write(GH_IWriter writer)
> {
>     var component_chunk = writer.CreateChunk(this.InstanceGuid.ToString());
>        
>     *do some stuff*
>
>     return base.Write(writer);
> {

Set some data to the chunk to read it on opening as follows:

> public override bool Read(GH_IReader reader)
> {
>     var component_chunk = reader.FindChunk(this.InstanceGuid.ToString());
>    
>     *do some stuff*
>
>     return base.Read(reader);
> {

However, the issue I’m facing is that this.InstanceGuid returns a different GUID in the Read method somehow, so it cannot find the chunk.

When I just save a C# script component that returns its InstanceGUID in a document, the GUID is the same upon opening.

Can anyone explain this to me?
And is there another unique identifier that I can use to create the chunk for the specific component instance?

Otherwise I get issues when my custom component is in the definition multiple times.

Any help is appreciated!

Not sure why the id would be mutating, if that’s what’s happening, but why not just store the guid under a name you can find in Write so you can retrieve it in Read?
in Write:

writer.SetGuid('InstanceGuid', this.instanceGuid);

in Read:

reader.TryGetGuid('InstanceGuid' out Guid storedInstanceGuid);

Thanks for your reply!

Wouldn’t this still give me a problem when multiple of my custom components are in the definition? Since they would all have the writer.SetGuid('InstanceGuid', this.instanceGuid); line, overwriting the last InstanceGuid?

BTW, running the component in debug mode shows me that the GUID is different on read than it was on writing, so I’m pretty sure that happens.

The values set in reader and writer are not stored at the document level, they are nested under your component instance chunk. You won’t accidentally read a value from a different instance with those methods.

1 Like

Awesome, that works indeed. Somehow I had the understanding that you would always need to make chunks for your component…

Thanks again!

1 Like