From the course: Grasshopper and Rhino: C# Scripting

Script editor structure

From the course: Grasshopper and Rhino: C# Scripting

Start my 1-month free trial

Script editor structure

- [Narrator] Now let's take a look at the basic structure of the C# editor. Go ahead and place a new C# component if you don't already have one on the canvas. Now, there are two ways that we can open up the editor. We can right-click the component and select edit source or we can simply double left-click the component which you can see opens up the editor window. As you can see, the actual scripting part of the editor is within the middle. On the border, we have a series of different buttons. So let's quickly go through those first. The run script button allows us to execute our code without closing down the window. So normally we would hit OK, our component would execute and it would run any script that's inside. We can use the run script button to simply execute the script. The solve instance overrides allows us to insert methods before and after our script runs. The preview overrides buttons allows us to change the way that geometries previewed within Rhino. Both of these buttons are outside the scope of this course, but they're worth looking into once you're a little bit more familiar with C#. Next, we have the font editor. This allows us to change the way that font is previewed within the script editor. And then we have the shrink script editor. This allows us to change the way the window is focused on when we click outside. So at the moment it's selected. If I was to click the canvas, you can see that it's minimized and we can use the Grasshopper canvas. If this was not selected, you'll see that it doesn't minimize although we can still use the Grasshopper canvas. Down the bottom, we had the cache and recover from cache buttons. This is very useful for temporarily saving state of the script. For example, if we hit cache or save the state, and then we make some changes, we can revert to that previous state by hitting the recover from cache and then selecting that state. Lastly, we have the OK button at the bottom-right. As I mentioned a minute ago, this simply closes the window and executes the script. Now let's take a look at the actual text editor part of the window. You'll notice this is made up by a series of sections, which are grouped by these plus and minus symbols. These are called regions in which like code is grouped together. At the top is the using region. This contains a lot of using statements, which are essentially bringing in libraries of code, which we can use within our script. For example, there's the Rhino library and the Grasshopper library, which allows us to interact with Rhino and Grasshopper. We'll learn about accessing these libraries later on. The next section of the code includes all the code that will execute when we run our script, which is contained within this script instance class. Within here, we firstly have the utility function section. This contains a series of pre-made functions that we can utilize in our script, including print and reflect. Don't worry if these functions don't make much sense yet, we'll be creating our own functions later on. The next region is the members region. This contains some variables or some objects that we can also utilize in our script, such as the Rhino document or the Grasshopper document. Next is the run script section which is probably the most important. This is where we write our actual code which will execute every time our component runs. As you can guess, we'll be spending a lot of time in this part of the editor. Lastly, we have a section called custom additional code. As you can probably guess, this is where we add any additional code that we want to use in our script. We can create custom functions that can also be used in our script which we'll be doing later on in the course.

Contents