Detect KeyDown and MouseDown Events from ScriptComponent

BeforeCompile & AfterCompile

Yes @TomTom, you’re 100% right. Although the GC would eventually get rid of even an interface guard the risk is that in the meantime, before being destructed, events would fire who knows where and when. Guarding passive data is one thing, but not unregistering delegates before they become inaccessible can end up in disaster.

I think there’s only one way to deal with this in a safe way in the Script component, and that is if @DavidRutten intoduces a BeforeCompile method (or event) and of course, an AfterCompile method (or event) on the Script_Instance class (the GH_Component can’t be overridden in this context anyway).

And as we have seen, the existing methods BeforeRunScript and AfterRunScript doesn’t help becaue it’s not related to neither mouse events nor to the compile command and so any delegates becomes inaccessible before having a chance to clean up any eventhandlers.

Being able to set up delegates/event subscribers is a crucial step in modern development. Prototyping with the script components is also extremely useful an efficient, so I can’t think of a good reason not to introduce those two methods (or hooks). Even if adding event hooks also on the GH_Component for this end, one would still need the extra Pre-Post-methods on the ScriptComponent in order to capture the case in which compiling renders any old event subscribers inaccessible.

I really think that the script component should become a first class member in the development tool box of grasshopper. Only the editor can remain simpler for simplicity’s sake.

// Rolf

This should be solved by creating a destructor for Script_Instance, however it seems that .Net calls it whenever it wants, sometimes before creating a new instance and sometimes much later.
I suppose this could be corrected by simply forcing the finalization of the instance within the Scripting component before reassigning it.

  private void RunScript(object x, object y, ref object A){
    RhinoApp.WriteLine("RunScript");
  }

  ~Script_Instance(){
    RhinoApp.WriteLine("***Destructor***"); 
  }

Rhino ouput:

RunScript
Destructor
RunScript
RunScript
RunScript
RunScript
Destructor
RunScript
Destructor
RunScript
Destructor
RunScript
Destructor
RunScript
Destructor
Destructor
Destructor
RunScript