How to write/read parameter persistent data

Hi everyone,

I have created a custom GH_PersistentParam with its own GH_Attributes. It cannot receive any input and I have put a couple of “buttons” on it for the user to select from dropdown menus. The text in the “buttons” is updated after the user choice, it is stored in string variables which I manage to save through the Read() and Write() overrides of the parameter.

I have also implemented the mouse button event handler to call something like:

Owner.PersistentData.Append(new MyCustomGoo(MyCustomClassConstructor(arguments)));

The arguments that I pass are based on the above mentioned strings and create the object as required. I thought I could use a similar approach and call:

SetPersistentData(new MyCustomGoo(MyCustomClassConstructor(arguments)));

and I thought I could do this in the Read() method. But this doesn’t seem to work. I notice that the persistent data in the parameter seems to be actually created from the custom class constructor but without arguments, so I assume this is happening somewhere else?

Any suggestions/help would be very appreciated.

Many thanks

Are you populating the persistent data in Read() before or after you call the base class Read() method?

I tried to do it like this, overriding the Read() method of the GH_PersistentParam class.

public override bool Read(GH_IReader reader)
        {
            string1 = reader.GetString("string1");
            string2 = reader.GetString("string2");
            SetPersistentData(new MyCustomGoo(MyCustomClassConstructor(strings)));
            return base.Read(reader);
        }

In an old thread I read that the method Read() is called every time a file is opened or the component pasted on the canvas, so I thought it could be the right place to do it.

I am not sure what you mean by “before or after I call the base class Read() method” though. As said above, I am not really calling it myself, I understood it gets called automatically.

EDIT: I just realised you may be refering to the base.Read().

So, in the meantime I tried this:

public override bool Read(GH_IReader reader)
        {
            string1 = reader.GetString("string1");
            string2 = reader.GetString("string2");
            bool rdstr = base.Read(reader);
            PersistentData.Clear();
            SetPersistentData(new MyCustomGoo(MyCustomClassConstructor(strings)));
            return rdstr;
        }

And it seems to work. Could you please explain me what happens when the base.Read() is called and why the previous code wasn’t working?

Thanks in advance.

base.Read() will wipe the persistent data and try to read it from the archive. So anything you do to it prior to that will be undone.