I moved your code managing the ValueList inside a method called by the event handler ParamSourcesChanged. This event fires when a new source is added or removes to an input of the component, which allows to distinguish your two cases.
This seems to have the desired behaviour, the list updates when it’s connected or reconnected, but not when the keys and values are modified. Although what you currently have seems best in my opinion as it ensures the value list is constantly updated.
Thanks so much for spending the time on my question!!
I didn’t know about ParamSourcesChanged!
It almost works fine, but I have an additional question.
It is about the timing of when the inputs are assigned to keys and items.
If even one input is missing, I get the error message 1. Input parameter K failed to collect data.
At this point, I think that SolveInstance is not executed.
Therefore, keys and items are still empty, and ParamSourcesChanged is running, but I don’t think it is getting the values to assign to the ValueList.
To make it work anyway, I made the input optional so that it would work on the first input too.
I added pManager.Optional = true;.
But I am wondering if there is a way to do this without making it optional.
Do you think there is such a way?
Also, this code checks if the keys in the ValueList are the same before updating the ValueList.
Do you think it is possible to check and update items as well as keys?
My understanding of vl.ListItems is unclear.
Indeed SolveInstance seems to only be called if all mandatory inputs recieve something, I definitely thought it was not the case. However in this situation this is a good thing, because you wouldn’t want to fill the valueList unless a list of keys AND an list of values are provided ? As a fun component behaviour, I added a test to check if Keys and Values are provided something, and if not, it removes the wires from the value list, which prevents the update.
I don’t really see what you are looking for exactly with this component.
My understanding of vl.ListItems is a List of GH_ValueListItem which is an object storing both the key and a value of the ValueList.
Right now, when a wire is updated, the code is checking if the new list of keys entering the K input is identical to the keys stored in ValueList, and only if it’s not the case, it updates the entire list. Which means that if you change the values and then unplug/plug the value list, the values won’t be updated, is that what you want ? If not you could basically remove half the code and only keep what’s in the else branch.
Thank you so much for your more than perfect answer!!
You solved my problem in a very intelligent way!
I couldn’t have solved it without your help! Thank you!
Incidentally, your screen capture gif is so beautiful!
The Grasshopper canvas is white or transparent and very easy to see.
The mouse click animation is cool too.
Could you please tell me how you captured it?