I had this in the old version of Peacock and and for the next version I’ve decided to change the focus. The problem with it being automatic is that it gives you automatic values, and it is possible that you already have a ValueList with certain values and if you plug it in it will change it. To avoid that I put a parameter between the value list and the component. Instead, what I’ll do now is to have a menu option in the parameter (by double clicking) to externalise a value list with default values. If you want the functionality for many cases I would recommend this other approach because it can be annoying to change the values once you have done it. But if you only want it for a particular case where it’s always good to have the same possible values, then that’s fine.
I can’t give you the whole code because it’s intertwined with several sides. I have a class to create default parameters that are created in each parameter as defined, one of them creates a ValueList, here source is the parameter in question. The base class is in charge of adding it to the document, plugging it to an input, saving an undo event and aligning it to the input. My base class for parameters has a menu option that does all this when the user claims it and then it expires the valuelist.
public class DefaultValueList : DefaultSource<Grasshopper.Kernel.Special.GH_ValueList>
public DefaultValueList(string Nickname, List<string> Keys, List<string> Values, int DefaultIndexValue, Grasshopper.Kernel.Special.GH_ValueListMode Mode)
source = new Grasshopper.Kernel.Special.GH_ValueList();
source.NickName = Nickname;
for (int i = 0; i < Keys.Count; i++)
source.ListItems.Add(new Grasshopper.Kernel.Special.GH_ValueListItem(Keys[i], Values[i]));
source.ListMode = Mode;
If you want the automatic approach, in your component you have to check if your input contains a value list before been computed, maybe in BeforeSolveInstance(), and in that case clear the value list, add your values (with a code similar to the above one) and not sure if it should be expired, I guess yes but maybe this will do recompute your component two times. Not sure, I did that inside clusters an that make it different.