Solution exception:Input parameter index [-1] too low for Component

Repost from Stack Overflow: Stack Overflow Question

I am not sure why I am getting this error for the input parameter index being too low. I usually get this error when the DA.GetData call can’t find an input. I had it working just a second ago. This time the names of the inputs match the DA.GetData calls.

I am using Visual Studio to write this grasshopper component.

It would help if I could get a line number in Grasshopper where this error was. I guess that would be a separate question though.

namespace MyProject1
{
    public class NormalDist : GH_Component
    {
        public NormalDist()
          : base("Normal Distribution", "NormalDist",
              "Generates a normal distribution of points.",
              "Extras", "Normal Distribution")
       {

        }


        protected override void 

 RegisterInputParams(GH_Component.GH_InputParamManager 
pManager)
        {
        pManager.AddIntegerParameter("Seed", "Seed", "integer seed for random number generator", 
GH_ParamAccess.item);
            pManager.AddNumberParameter("StdDev", "StdDev", "Standard Deviation", 
GH_ParamAccess.item);
        pManager.AddNumberParameter("Mean", "Mean", "Mean of the normal distribution", 
GH_ParamAccess.item);
    }

 /// Registers all the output parameters for this component.

    protected override void 
RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
    {
        pManager.AddNumberParameter("Output", "output", "A collection of 
normally distributed points", GH_ParamAccess.item);

    }

  /// This is the method that actually does the work.

    protected override void SolveInstance(IGH_DataAccess DA)
    {

        int seed = 0;
        double std = 0;
        double mean = 0;
        DA.GetData<int>("Seed", ref seed);
        DA.GetData<double>("StdDev", ref std);
        DA.GetData<double>("Mean", ref mean);
        Random rand = new Random(seed);
        double u1 = 1.0 - rand.NextDouble();
        double u2 = 1.0 - rand.NextDouble();
        double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2);
        double randNormal = mean + std * randStdNormal;
        DA.SetData("Output", randNormal);
     }

You need to specify the index number of the inports (where you have text now is where an integer should be placed), like so:

        DA.GetData(0, ref seed);
        DA.GetData(1, ref std);
        DA.GetData(2, ref mean);
        ...
        ...
        // Start counting from 0 again on the out-ports:
        DA.SetData(0, randNormal);

My approach to this is to use variables for the indexes so that I don’t mess them up if they are many, or if I change the order of the ports.

    int IN_Seed;
    int IN_Std;
    int IN_Mean;
    int OUT_Output;

    RegisterInputParams(GH_Component.GH_InputParamManager pManager)
    {
        IN_Seed = pManager.AddIntegerParameter("Seed", "Seed", "... );
        IN_Std = pManager.AddNumberParameter("StdDev", "StdDev", ".... );
        IN_Mean = pManager.AddNumberParameter("Mean", "Mean", "... );
    }

    RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
    {
       OUT_Outport = pManager.AddNumberParameter("Output", "output", "A collection... "... );
    }

and then use these indexes when reading from and writing to the ports, like so:

    DA.GetData(IN_Seed, ref seed);
    DA.GetData(IN_Std, ref std);
    DA.GetData(IN_Mean, ref mean);
    ...
    DA.SetData(OUT_Outport, randNormal);

// Rolf

3 Likes

Extremely helpful. Thanks so much.

1 Like

This is a nice approach! I always fight with the indexes, I remember there was a way of using string names for this but I just got used to the pManager[idx].Whatever syntax