algorithmic modeling for Rhino
I am trying to implement a custom message using the GH_Component.Message Property (which should appear similar to the one seen in the attached image) in visual studio in c# but I am having difficulty figuring out where the code should be executed.
The issue is that when I first drop the component on the canvas, the message does not show up until I fiddle with some inputs, but I want a default message to appear as soon as the component is placed on the canvas.
Tags:
My typical approach is to call the message updater in the class constructor, like so:
private bool some_property;
//my class constructor
My_ComponentClass() : base(blah blah blah){
some_property = true; //the default value for the component;
updateMessage();
}
//message updater
private void updateMessage()
{
if (some_property)
{
Message = "Property is True!";
}
else
{
Message = "Property is not true.";
}
}
also note that if you want the variable message property to persist through copy-paste / save / reopen, you'll need to override Read and Write functions:
public override bool Write(GH_IWriter writer)
{
writer.SetBoolean("SomeProperty", someProperty);
return base.Write(writer);
}
public override bool Read(GH_IReader reader)
{
someProperty = false;
reader.TryGetBoolean("SomeProperty", ref someProperty);
updateMessage();
return base.Read(reader);
}
Thanks, this worked as advertised. What I was missing was setting the message in the class constructor and then re-setting it in the read function. Examples like this are really useful for understanding how this all works.
The only small thing missing in your example is declaring the some_property variable outside of the class constructor (for anyone looking to copy this example code in the future).
This is great!
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by