C# script does not function anymore

grasshopper
unhandled
metahopper

#1

I have received this C# script from Andrew Heumann if I remember correctly…

The script can be used to manipulate a slider with F7/F8 on the keyboard or by setting F7/F8 to buttons on a 3D Connexion Space Navigator or even on a Wacom Intuos tablet with buttons or the touch wheel. The slider is linked to the script with Metahopper.

The script worked fine in the original grasshopper file.

It also worked fine in any other file I copied it into, until two days ago when I updated Rhino.

@andheum or anyone else at Rhino, any ideas?

KeyListener_02.gh (9.7 KB)

thanks


(Andrew Heumann) #2

It looks like some of the default using statements have been removed from the c# script editor. (@DavidRutten is this the case? I imagine this could break a lot of scripts)

In any case, to fix, just add these two lines where indicated:

image

Also, your definition was configured slightly wrong - should be like this:
image


(Andrew Heumann) #3

ahhh nvm @DavidRutten I see that saved old scripts automatically migrate their old usings (you think of everything!). @martinsiegrist - did you copy-paste this script from somewhere, rather than opening from an old file?


#4

I only saved this script as a user object today…

It is my only C# script and if I needed it, I would just copy it together with the metahopper component, toggle and text field.

It works again with the two lines of added code. Thanks for the quick help!!!


(David Rutten) #5

Yep, a severe error on my part. If a script component is loaded from an old file is runs fine, but if it is saved without being modified (even trivial modifications will do) it will not load fine again. This was missed during testing because it requires a file to be opened twice before it breaks, still … very amateurish.

This file contains a script component which will repair the missing references in a GH file (it will save another file next to it with the fixes). However this file too will fail to load a second time because of the exact same reason.

To fix a file permanently, run the attached script, then open up each script component and add a blank line somewhere. Apologies for this mess, it has been fixed, but that fix will take some time to get out to everyone.

FixScriptComponent.gh (6.1 KB)


#6

While at it, why not add the methods
private virtual void PreCompile() and
private virtual void PostCompile() to the Script_Instance as well, so we have a place to manage eventhandlers before they become inaccessible due to compiling

// Rolf


(David Rutten) #7

How exactly am I supposed to call PreCompile() before compiling the script?


#8

When pressing the green button/arrow, or when closing the script editor, after code modification (?).

// Rolf


#9

… OK I see what you mean. Any such methods in the Script_Instance would still always be the previously compiled currently running version.

For the purposes I was thinking of, namely a chance to unsubscribe event handlers if any, a solution that could work could be to have an extra eventhandler “OnJustBeforeImDying()” fireing just before the destructor is done with the object that is about to be recompiled. Perfect would be if such an event could be fired by GH_Component’s destructor, and of course, also unregistering any hooks to itself before entering eternity.

There’s that problem with eventhandlers (Mouse & Kbd events) not being unhooked before the script component is recompiled, and so they’re still firing in their “after life” so to speak. (Yes, there obviously is an afterlife). Any solution should compile also in VS.

Sorry for highjacking the thread, but this problem really should be solved. We need event handlers for Mouse & Kbd events but they’re currently causing problems in script components.

// Rolf