Fusion of "Addition" and "Merge" native components

Hello,
I’m trying to replicate the behavior of the Merge and Addition components in one component. The goal is to start with two input variables, which can never be deleted. Once both variables are fed with a value, a new (third) input slot should be created. Once a value is provided to the third slot a new (forth) input slot should be created and so on. If the user does not want to provide a fourth value, he/she should be able to delete this input slot. I think I have understood most of the concepts related to the VariableParameterComponent interface but there are certain behaviors I have not managed to implement:

  1. When the first and second variables are fed with a value, the third slot is NOT generated, even though I have implemented the RegisterInputParam function in VariableParameterMaintenance. Obviously the same applies when three variables are fed with a value and so on. Rather, the user has to manually click on the “+” sign.
  2. When the new input slot (e.g. third one) is manually introduced, the summation of the previous varaibles (e.g. first and second) fails.
  3. I cannot manage to delete the 3rd input slot if the first and second variables have been fed with a value.

I am attaching the code here:

MyComponent1.cs (4.5 KB)

Some references I have already looked into:

Any help? Thanks

Hi,

For the inputs to be added automatically you need to implement the event handler ParameterSourcesChangedEventHandler

There is an example here :

This would look like this :

 public MyComponent1()
          : base("MyComponent1",
                 "Nickname",
                 "Description",
                 "Category",
                 "Subcategory")
        {
            this.Params.ParameterSourcesChanged += new GH_ComponentParamServer.ParameterSourcesChangedEventHandler(this.ParamSourcesChanged);
        
    }

There is a method ParamSourcesChanged called there, this is where you add the parameters.

private void ParamSourcesChanged(object sender, GH_ParamServerEventArgs e)
        {      

            if (e.ParameterSide == GH_ParameterSide.Input && e.ParameterIndex == this.Params.Input.Count - 1)
            {

                // add another input
                IGH_Param iGH_Param = CreateParameter(GH_ParameterSide.Input, this.Params.Input.Count);
                this.Params.RegisterInputParam(iGH_Param);
                this.VariableParameterMaintenance();
                this.Params.OnParametersChanged();

                // }

            }


        }

The VariableParameterMaintenance is only used to renumber everything.

For the second issue I guess the problem comes from double.NaN that makes the addition fail. I’ve changed it to 0, as well as looping through all inputs.

I’ve added the Optional = true option so inputs can stay empty.

If you really want the user to use the first two inputs before adding a third input, you may want to check the SourceCount property of inputs.

MyComponent1.cs (4.9 KB)

Amazing! Thanks a lot. Exactly what I was looking for.