C# script not working

Hi, I am very new to grasshopper and c# script components.
When I double click c# script components in grasshopper, it shows an blank page - no default imports or structure to write a script.

Can anyone help me how to get the default setting?
Thanks a lot!!

on mac grasshopper, the C# editor is not quite as fully featured as on windows. It lacks intellisense, etc. However, the “script” portion is simply the body of the “RunScript” method — it works just fine. The one thing you can’t do is customize your usings — if you add a custom reference via manage assemblies, it will simply import all namespaces.

1 Like

And if you’re not familiar with the way it works on mac, effectively it creates a RunScript method that looks like this:

private void RunScript(object x, object y, ref object A) {
// your script here
}

where the signature of the method reflects the inputs and outputs of your script. (zoom in to the component and click the small “+ / -” buttons to add / remove inputs + outputs.). You can specify the name of each input by right clicking it on the component and renaming it, like any other grasshopper input, and you can specify its type by choosing between item / list / tree and the type hint. So, for example, if I set up my scripting component like this:


Then the method I’d be filling in would look like this:

private void RunScript(List<Brep> b, ref object output) {
// everything in the "script" portion of the c sharp editor
}
1 Like

Hi Andrew, thanks a lot for your reply!

I simply copy paste your RunScript on the mac c# script that is;

private void RunScript(object x, object y, ref object A) {
// your script here
}

however, it gives me a bunch of error list…


it seems like a default setting is missing. Or do I understand your suggestion incorrectly?

I would really appreciate your help!!:slight_smile:

There’s a lot of boilerplate code around the runscript function that you have erased. On windows that code is not user-modifiable, but on Mac I think we use an editor which doesn’t allow us to lock certain lines in the script. So you must take care to not erase anything from the standard script template which is required.

1 Like

Hi! Just to make it clear, I never erased anything in the script.
It was the first time that I opened c# script in Rhino 6 and it was totally empty/blank space.

I copied and paste the default setting below;
sing System;

using System.Collections;

using System.Collections.Generic;

using Rhino;

using Rhino.Geometry;

using Grasshopper;

using Grasshopper.Kernel;

using Grasshopper.Kernel.Data;

using Grasshopper.Kernel.Types;

///

/// This class will be instantiated on demand by the Script component.

///

public class Script_Instance : GH_ScriptInstance

{

#region Utility functions

///

Print a String to the [Out] Parameter of the Script component.

/// String to print.

private void Print(string text) { /* Implementation hidden. */ }

///

Print a formatted String to the [Out] Parameter of the Script component.

/// String format.

/// Formatting parameters.

private void Print(string format, params object args) { /* Implementation hidden. */ }

///

Print useful information about an object instance to the [Out] Parameter of the Script component.

/// Object instance to parse.

private void Reflect(object obj) { /* Implementation hidden. */ }

///

Print the signatures of all the overloads of a specific method to the [Out] Parameter of the Script component.

/// Object instance to parse.

private void Reflect(object obj, string method_name) { /* Implementation hidden. */ }

#endregion

#region Members

///

Gets the current Rhino document.

private readonly RhinoDoc RhinoDocument;

///

Gets the Grasshopper document that owns this script.

private readonly GH_Document GrasshopperDocument;

///

Gets the Grasshopper script component that owns this script.

private readonly IGH_Component Component;

///

/// Gets the current iteration count. The first call to RunScript() is associated with Iteration==0.

/// Any subsequent call within the same solution will increment the Iteration count.

///

private readonly int Iteration;

#endregion

///

/// This procedure contains the user code. Input parameters are provided as regular arguments,

/// Output parameters as ref arguments. You don’t have to assign output parameters,

/// they will have a default value.

///

private void RunScript(object x, object y, ref object A)

{

}

//

// </Custom additional code>

}

but it returns me a bunch of error message.

Perhaps I need to buy a new pc…

Hi @Jane.Cho,

You’re right, there is no boilerplate code visible in Mac, nor is it needed. The “Script” section in the top is in the RunScript() method, so you don’t need to define the method signature. The “Overrides” section at the bottom is where you’d put overrides for other methods or define types/classes. These are the same two editable sections that you see in the Windows script editor.

So, in the Script section you can write something like A = "Hello World";, for example.

Hope this helps!
Curtis.

3 Likes

Hi @curtisw,

thanks a lot!!! now ‘Hello world’ works! I don’t need to buy a new pc :wink:

1 Like