Output.AddVolatileData in C# Script component overridden with null element

Hi there!

TL;DR: how can I avoid C# script components automatically assigning the ref values to the outputs?

I am writing a C# script component where I want to be able to account for changes in the amount of outputs (plus and minus icons on components), so I need a way to assign outputs programmatically. I am using Output.AddVolatileData(), such as:

  private void RunScript(object x, object y, ref object A, ref object B)
    for (int i = 0; i < Component.Params.Output.Count; i++) {
      Component.Params.Output[i].AddVolatileData(new GH_Path(0), 0, "This is output #" + i);
      Component.Params.Output[i].AddVolatileData(new GH_Path(1), 0, "This is output #" + i);

But it turns out that the first element of path 0 always get overriden with a null element:

I am assuming this is because the component takes the unassigned A and B refs and places them automatically in the output datatrees. This is easy to see because it doesn’t happen in the out output, if I AddVolatileData to other branches, or if I manually assign values to the ref outputs before the AddVolatileData call.

So, my question is, how can this behavior be overridden, so that I can assign values to all outputs programmatically, without having the first element always be null?


After GH solve/runs the SolveInstance or RunScript, it assigns the output parameters, so you must do it after that. Try doing this in AfterSolveInstance(). In the C# editor, click at the second button (after the green play icon) and see at the end of the script.

1 Like

Oh nice! So many years of GH and I had never even seen those buttons! This is perfect, thanks @Dani_Abalde!

Even inside the AfterSolverInstance you will end up having a null element at path zero if you don’t clean the data before adding the new one.
this.Component.Params.Output[i].AddVolatileDataList(path, list);