Custom component with toggle button loses the value when file closes

Hello there,

I have developed a component that integrates a toggle button. Given two numerical buttons, the toggle button adds/subtracts the values. Everything works well but when saving the file and opening it again the toggle button returns to a default value (e.g. “subtract”), even though when the file was saved according to the button the values were added. How do I make sure that the toggle value is saved when I close the file?

Thank you,

Hi,

You need to write your value and read it using those overrides.

https://developer.rhino3d.com/api/grasshopper/html/Methods_T_GH_IO_GH_ISerializable.htm

Example with a boolean below.

private bool m_fa = false;

public bool fa
{
    get
    {
        return this.m_fa;
    }
    set
    {
        this.m_fa = value;
    }
}

public override bool Write(GH_IWriter writer)
{
    writer.SetBoolean("fa", this.fa);
    return base.Write(writer);
}
public override bool Read(GH_IReader reader)
{
    reader.TryGetBoolean("fa", ref this.m_fa);
    return base.Read(reader);
}

1 Like

indeed… magnifique, merci!