Ho to detect a new compile of a c# script?

Hi all.

Give a simple class like:

private void RunScript(object x, object y, ref object A)
  {
    obj = new MyClass(this.Component);
  }

  // <Custom additional code> 
  public MyClass obj;

  public class MyClass{
    IGH_Component comp;
    public MyClass(IGH_Component Comp){
      comp = Comp;
    }
  }

That “obj” object can run methods internally autonomously. Load on the CPU.
I can manage that class easily from the main void RunScript.

But if, before stopping that object, I open the script editor, make some edit, and close (triggering a new compile of the c# component) … I’ll lose any connection to that object!
“comp” always return valid, even after re-compile.
My new “obj” object will be a new, different object (after recompile), and now I have no control over the previous one.

How to solve this?
Any suggestions? Workarounds?

What if the object is constructed upstream and passed to the script that is working on it. Like this:

test.gh (6.2 KB)

1 Like

Interesting!
Thanks!

I’ll be honest, I don’t remember the specific case for what was I needing this 1 year ago… but similar problems still occurs to me.

In the first c# script you removed the “public class Script_Instance : GH_ScriptInstance” and “private void RunScript()” sections, and built directly a class.
Then, you even used the output “classInstance” without being inside the usual “private void RunScript()” … How does it work? What’s going on?

And… a custom class/object can now exit a c# script component and go to another?
I think 7 couldn’t do this. Cool!

I’ll make some tests.

In the new C# scripting component, you don’t have to implement a GH_ScriptInstance. This was called SDK-mode on the legacy IronPython component. I made this consistent across languages. Both python and C# now support Script-mode (without GH_ScriptInstance class) and SDK-mode (with GH_ScriptInstance class).

As you know the GH_ScriptInstance is really powerful to implement before/after solve and preview handlers in grasshopper. The script-mode makes it easy to write C# code when these overrides are not necessary and you just want to put some logic together.

To match legacy behaviour, the C# component defaults to SDK-mode, and Python components default to Script-mode.

1 Like