# GH: Compiling ScriptComponent doesn't call "BeforeRunScript()"

**URL:** <https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904>\
**Category:** Grasshopper Developer\
**Created:** [June 12, 2018, 4:43pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904 "2018-06-12T16:43:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![RIL](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ril/32/771758_2.png) [@RIL](https://discourse.mcneel.com/u/RIL)\
**Post date:** [June 12, 2018, 4:43pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/1 "2018-06-12T16:43:16Z")

</div>

When compiling a ScriptComponent it doesn’t call “ **BeforeRunScript** ()” before calling RunScript. This keeps **causing runtime errors in RunScript()** after each code modification in cases where BeforeRunScript() is required to run before executing the main method **RunScript** (…).

This is really bad if the required settings done in “BeforeRunScript()” is disovered very late in the RunScript method, meaning, it takes long before it crashes, and only after first crash the component runs as expected (since it will then actually call the method “BeforeRunScript()”)

In order to get components to behave which has this requirement, I will always have to wait for the first crash, and then press F5 or implement a “Reset” method to get to the first “clean” run of the component.

```auto
private LetsSayANullableTypeOfSorts m_criticalSetting = null;

public override void BeforeRunScript()
{
    // This code will NOT be executed on compile, 
    // which leads to a runtime error. :( 
    m_criticalSetting = GetCriticalSetting();
}

private void RunScript(Inputs..., Outputs...)
{
    // lotsa code here
    // ...
    if (m_criticalSetting.IsOK) <--- # KABOOM! #
    {
       // Bah! Never mind
    }
}

```

**Q** : Fixed in next service release? 😌

// Rolf

---

<div class="post-metadata">

**Author:** ![DavidRutten](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/davidrutten/32/755688_2.png) [@DavidRutten](https://discourse.mcneel.com/u/DavidRutten)\
**Post date:** [June 12, 2018, 5:25pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/2 "2018-06-12T17:25:25Z")

</div>

There’s a `BeforeSolveInstance()` method you can override in regular components, and call the `BeforeRunScript()` method there.

---

<div class="post-metadata">

**Author:** ![RIL](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ril/32/771758_2.png) [@RIL](https://discourse.mcneel.com/u/RIL)\
**Post date:** [June 12, 2018, 5:32pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/3 "2018-06-12T17:32:13Z")

</div>

> [@DavidRutten](#):
>
> override in regular components

Huh? Now (again) I didn’t get it.

This problem is with the ScriptComponent, how can I solve it in a “regular component”?

OK, I’m slow today, but I really didn’t get it.

// Rolf

---

<div class="post-metadata">

**Author:** ![DavidRutten](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/davidrutten/32/755688_2.png) [@DavidRutten](https://discourse.mcneel.com/u/DavidRutten)\
**Post date:** [June 12, 2018, 5:33pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/4 "2018-06-12T17:33:08Z")

</div>

Maybe I’m confused then. You’re doing something where you want to copy paste from a script component into VS and have it work right?

---

<div class="post-metadata">

**Author:** ![RIL](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ril/32/771758_2.png) [@RIL](https://discourse.mcneel.com/u/RIL)\
**Post date:** [June 12, 2018, 5:37pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/5 "2018-06-12T17:37:49Z")

</div>

Nah. Although I’m _also_ dealing with the sync to VS (yes i am), but in this particular case I just want the ScriptComponent to not crash when I press the green compile arrow.

In VS the `BeforeSolveInstance` is doing its job, but OTOH, VS is not trying to run the Component after each Build… 😉

// Rolf

---

<div class="post-metadata">

**Author:** ![DavidRutten](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/davidrutten/32/755688_2.png) [@DavidRutten](https://discourse.mcneel.com/u/DavidRutten)\
**Post date:** [June 12, 2018, 5:40pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/6 "2018-06-12T17:40:34Z")

</div>

Nope, even more confused now. Do you have a regular script component with an overridden `BeforeRunScript()` method and that method _isn’t_ called during every solution before any calls to `RunScript()`?

---

<div class="post-metadata">

**Author:** ![RIL](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ril/32/771758_2.png) [@RIL](https://discourse.mcneel.com/u/RIL)\
**Post date:** [June 12, 2018, 5:43pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/7 "2018-06-12T17:43:12Z")

</div>

> [@DavidRutten](#):
>
> Do you have a regular script component with an overridden `BeforeRunScript()`

Yes.

> [@DavidRutten](#):
>
> and that method _isn’t_ called during every solution before any calls to `RunScript()` ?

It is not called when I press compile (green arrow) although the RunScript method is called. This is why it crashes once. After the compile-press, then it always calls the BeforeRunScript as expected. Only not when pressing the compile button, then it seems to rush to call RunScript directly after compilation finishes (bypassing BeforeRunScript).

// Rolf

---

<div class="post-metadata">

**Author:** ![DavidRutten](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/davidrutten/32/755688_2.png) [@DavidRutten](https://discourse.mcneel.com/u/DavidRutten)\
**Post date:** [June 12, 2018, 5:50pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/8 "2018-06-12T17:50:02Z")

</div>

I cannot repeat that here, so I’ll need a script that does this.

This test _always_ prints the Before and After messages to the Rhino command line. Do note that the code has to change before the Green Arrow button makes sense.

[BeforeDuringAfter.gh](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/0/8/08697b2986cfd698dcb837fd7bbb78e932a06c39.gh) (5.6 KB)

---

<div class="post-metadata">

**Author:** ![RIL](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ril/32/771758_2.png) [@RIL](https://discourse.mcneel.com/u/RIL)\
**Post date:** [June 12, 2018, 6:00pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/9 "2018-06-12T18:00:59Z")

</div>

> [@DavidRutten](#):
>
> I’ll need a script that does this.

Yup, I just prepared a debug case for you.

To reproduce, just add an Output, which will cause the component to recompile (it goes bananas, due to not calling BeforeRunScript()). Then press F5 or Reset to let it have a second chance to behave. Poke on the R-slider to make it do its thing.

 ![bild](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/6/c/6cf8d21a18c856613daae1c39a905494e64c9362.png)

[Point\_Inclusion\_Debug.gh](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/9/7/9701e360baf9529566588c86e5737033c2b8c358.gh) (2.6 MB)

// Rolf

---

<div class="post-metadata">

**Author:** ![RIL](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ril/32/771758_2.png) [@RIL](https://discourse.mcneel.com/u/RIL)\
**Post date:** [June 12, 2018, 6:19pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/10 "2018-06-12T18:19:34Z")

</div>

BTW, I notice that the filtering of the points takes about 5 ms while the entire solution spends the rest of some ~240 ms figuring out point id:s. According to the VS profiler it is the looking up of guids which wastes all this time. ☹

Is there any way one could avoid wasting so much valuable periods of grace only looking up silly guids?

// Rolf

---

<div class="post-metadata">

**Author:** ![DavidRutten](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/davidrutten/32/755688_2.png) [@DavidRutten](https://discourse.mcneel.com/u/DavidRutten)\
**Post date:** [June 12, 2018, 6:36pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/11 "2018-06-12T18:36:23Z")

</div>

I think the problem is that you’re invoking the constructor of the `GH_StructureIterator` with a null argument.

The `Component` field must be set to the actual component, because it is passed into the constructor and the iterator will throw if it’s given a null.

I guess it makes sense for the Component field to be assigned prior to the first ever call to `BeforeRunScript`, but I think at the moment it is assigned from `SolveInstance()` in the script component code.

I solved it by making the access instance property lazy.

[Point\_Inclusion\_Debug.gh](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/1/9/190f6991c7df9e5f013f983e96b198d3e6f770f8.gh) (2.6 MB)

---

<div class="post-metadata">

**Author:** ![DavidRutten](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/davidrutten/32/755688_2.png) [@DavidRutten](https://discourse.mcneel.com/u/DavidRutten)\
**Post date:** [June 12, 2018, 6:37pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/12 "2018-06-12T18:37:35Z")

</div>

You mean Rhino object guids or .NET type guids?

---

<div class="post-metadata">

**Author:** ![RIL](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ril/32/771758_2.png) [@RIL](https://discourse.mcneel.com/u/RIL)\
**Post date:** [June 12, 2018, 6:38pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/13 "2018-06-12T18:38:09Z")

</div>

Hm, I’ll have to look it up in the profiler log. bbs

**Edit** :  
I will have to read up on how to interpret this profiler, but this is the summary. Hm, it looks like it’s a .NET guid which is taxing here. Now I don’t know if this about elapsed time or accesses. I will have to read up on the profiler.

 ![bild](https://global.discourse-cdn.com/mcneel/original/3X/5/d/5d0314156941cae7384a79f46fd8512f676ddcd2.png)

// Rolf

---

<div class="post-metadata">

**Author:** ![gankeyu](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/gankeyu/32/487811_2.png) [@gankeyu](https://discourse.mcneel.com/u/gankeyu)\
**Post date:** [June 12, 2018, 7:19pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/14 "2018-06-12T19:19:44Z")

</div>

Traditionally, calls to `System.RuntimeType.GUID` occur frequently if there are many reflection / COM operations.

GH also uses a lot in `GH_Convert` class, when converting from a `object` to a RhinoCommon/GH datatype.

---

<div class="post-metadata">

**Author:** ![RIL](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ril/32/771758_2.png) [@RIL](https://discourse.mcneel.com/u/RIL)\
**Post date:** [June 12, 2018, 7:51pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/15 "2018-06-12T19:51:25Z")

</div>

> [@DavidRutten](#):
>
> Component field … is assigned from `SolveInstance()` in the script component code.

OK, that explains my troubles. Good to know the reason at least.

One nasty solution would be to set the Component member in the very GetDataAccess/ConstructCurrentDataAccess method where it’s needed:

```
this.Component = (IGH_Component) owner; // +this
object _instance = m_da_constructor.Invoke(new object[] { Component });

```

As for the oddities you commented in the code it was leftovers from a much larger experimental code producing several variants of the filtering algorithm (and the use of the m\_DA was supposed to break the code if BeforeRunScript wasn’t doing its thing).

And seriously: Thou Shalt Not Remove My Handcrafted Low-level Methods For Checking Distances. This is because Rhino .NET methods (like `DistanceToSquared()`) doesn’t run natively on the **G** PU, where it eventually may end up. 😛

I also noticed that you don’t seem to like my **m\_** `member` naming convention. That’s OK. As for me I prefer the \_var style for locals only. 🕶

Anyway, thank you very (very) much for the debugging! Happy enough to find a workaround. Your support is super!

// Rolf

---

<div class="post-metadata">

**Author:** ![RIL](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ril/32/771758_2.png) [@RIL](https://discourse.mcneel.com/u/RIL)\
**Post date:** [June 12, 2018, 7:59pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/16 "2018-06-12T19:59:51Z")

</div>

> [@gankeyu](#):
>
> GH also uses a lot in `GH_Convert` class, when converting from a boxed object to a RhinoCommon/GH datatype.

Hm. I’m not fully versed in the .NET boxing thing. Is that what is happening with the PointCloud items, like this: “point[i].Location”? However, a PointCloudItem is a class while the Location property returns a Struct, so it shouldn’t stem from that.

// Rolf

---

<div class="post-metadata">

**Author:** ![gankeyu](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/gankeyu/32/487811_2.png) [@gankeyu](https://discourse.mcneel.com/u/gankeyu)\
**Post date:** [June 12, 2018, 8:20pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/17 "2018-06-12T20:20:30Z")

</div>

Well, `PointCloudItem` is a class so it’s not a value type.

1. Every time you call a `PointCount[i]`, a new `PointCloudItem` class is created
2. `PointCloudItem.Location` involves many expensive calls (managed/unmanaged switching).

It is better to use `PointCloud.GetPoints()` first to retrieve a copy of points.

---

<div class="post-metadata">

**Author:** ![DavidRutten](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/davidrutten/32/755688_2.png) [@DavidRutten](https://discourse.mcneel.com/u/DavidRutten)\
**Post date:** [June 12, 2018, 9:56pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/18 "2018-06-12T21:56:20Z")

</div>

> [@RIL](#):
>
> This is because Rhino .NET methods (like `DistanceToSquared()` ) doesn’t run natively on the **G** PU, where it eventually may end up.

I think you may be optimising the wrong thing here 🙂

I had to rewrite and simplify the code so that I could wrap my own head around it. I know it’s not .NET standard but I prefer to prefix private and protected class level variables with an underscore. The `m_` style has fallen out of favour in recent years it seems and I applaud that.

---

<div class="post-metadata">

**Author:** ![RIL](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ril/32/771758_2.png) [@RIL](https://discourse.mcneel.com/u/RIL)\
**Post date:** [June 12, 2018, 10:06pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/19 "2018-06-12T22:06:55Z")

</div>

> [@DavidRutten](#):
>
> I think you may be optimising the wrong thing here

In this case yes. But that was clear only after profiling (80% of the original code stripped out where I also compared with the Rhino methods).

Anways, there are cases where the “handcrafted” (gpu) versions are doing a great job. For example when doing realtime collision tests between big meshes (50x50k vertices and the alike). In combination with RTree very “interesting speed” can be achieved.

It just so happened that I started testing the IGH\_DataAccess thing on this experimental testing component.

// Rolf

---

<div class="post-metadata">

**Author:** ![gankeyu](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/gankeyu/32/487811_2.png) [@gankeyu](https://discourse.mcneel.com/u/gankeyu)\
**Post date:** [June 12, 2018, 10:11pm UTC](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904/20 "2018-06-12T22:11:06Z")

</div>

Btw, I use C++AMP to code lightweight GPU programs

[Next page](https://discourse.mcneel.com/t/gh-compiling-scriptcomponent-doesnt-call-beforerunscript/64904.md?page=2)
