Does DA.GetData make a copy? - Grasshopper2024-03-28T13:40:19Zhttps://www.grasshopper3d.com/forum/topics/does-da-getdata-make-a-copy?feed=yes&xn_auth=noSorry I made a different mist…tag:www.grasshopper3d.com,2017-05-18:2985220:Comment:17537102017-05-18T08:56:48.499ZBenjamin Felbrichhttps://www.grasshopper3d.com/profile/BenjaminFelbrich
<p>Sorry I made a different mistake here:<br></br><br></br>My problem was that in the second component I initialized another instance of myClass before assigning it. Like this:</p>
<blockquote><p><br></br>MyClass instance = new MyClass();</p>
<p>DA.GetData(0, ref instance);</p>
</blockquote>
<p></p>
<p>..this instance created internal problems, since i shouldn't initialize two instances.<br></br>It works when I set it to null instead:</p>
<blockquote><p>MyClass instance = null;<br></br>DA.GetData(0, ref…</p>
</blockquote>
<p>Sorry I made a different mistake here:<br/><br/>My problem was that in the second component I initialized another instance of myClass before assigning it. Like this:</p>
<blockquote><p><br/>MyClass instance = new MyClass();</p>
<p>DA.GetData(0, ref instance);</p>
</blockquote>
<p></p>
<p>..this instance created internal problems, since i shouldn't initialize two instances.<br/>It works when I set it to null instead:</p>
<blockquote><p>MyClass instance = null;<br/>DA.GetData(0, ref instance);</p>
</blockquote>
<p><br/>That was wrong thinking from my side. Maybe someone will find this text useful some day.<br/><br/>Thanks<br/>Ben</p> Okay so for custom classes my…tag:www.grasshopper3d.com,2017-05-18:2985220:Comment:17535462017-05-18T08:38:39.375ZBenjamin Felbrichhttps://www.grasshopper3d.com/profile/BenjaminFelbrich
<p>Okay so for custom classes my best bet would be Tom's suggestion to link and refer to my instance in the global document scope, correct?</p>
<p>Okay so for custom classes my best bet would be Tom's suggestion to link and refer to my instance in the global document scope, correct?</p> GH_ObjectWrapper is a last re…tag:www.grasshopper3d.com,2017-05-18:2985220:Comment:17534062017-05-18T08:20:33.198ZDavid Ruttenhttps://www.grasshopper3d.com/profile/DavidRutten
<p>GH_ObjectWrapper is a last resort type which is used to encapsulate data for which no suitable IGH_Goo implementation can be found. GH_ObjectWrappers do not know how to do anything with the data they contain (read it, write it, copy it, transform it, ...) so it will definitely not copy the values.</p>
<p></p>
<p>The major downside of using GH_ObjectWrapper is that it will probably fail to be written to a gh file in case it is internalised in any parameter.</p>
<p>GH_ObjectWrapper is a last resort type which is used to encapsulate data for which no suitable IGH_Goo implementation can be found. GH_ObjectWrappers do not know how to do anything with the data they contain (read it, write it, copy it, transform it, ...) so it will definitely not copy the values.</p>
<p></p>
<p>The major downside of using GH_ObjectWrapper is that it will probably fail to be written to a gh file in case it is internalised in any parameter.</p> Hello David,thanks a lot for…tag:www.grasshopper3d.com,2017-05-18:2985220:Comment:17534482017-05-18T08:14:27.748ZBenjamin Felbrichhttps://www.grasshopper3d.com/profile/BenjaminFelbrich
<p>Hello David,<br></br><br></br>thanks a lot for the reply. And for custom classes I'd use GH_ObjectWrapper??<br></br>I'm thinking of something like:</p>
<blockquote><p><br></br>GH_ObjectWrapper obj= null;</p>
<p>if (!DA.GetData(0, ref obj)) return;</p>
<p></p>
<p>MyClass mc = (MyClass)obj.Value;</p>
<p></p>
</blockquote>
<p>...to point me to the original instance?<br></br><br></br><br></br>Best<br></br>Ben<br></br><br></br>Edit: <br></br><em>"When making a GHA, and you want to modify the data in the input, you must always make a copy…</em></p>
<p>Hello David,<br/><br/>thanks a lot for the reply. And for custom classes I'd use GH_ObjectWrapper??<br/>I'm thinking of something like:</p>
<blockquote><p><br/>GH_ObjectWrapper obj= null;</p>
<p>if (!DA.GetData(0, ref obj)) return;</p>
<p></p>
<p>MyClass mc = (MyClass)obj.Value;</p>
<p></p>
</blockquote>
<p>...to point me to the original instance?<br/><br/><br/>Best<br/>Ben<br/><br/>Edit: <br/><em>"When making a GHA, and you want to modify the data in the input, you must always make a copy yourself. It's the only way to be sure."</em></p>
<p>I don't want to change the data of myClass downstream, I just want to retrieve information from it. Is that save?</p> If you ask for the IGH_Goo ty…tag:www.grasshopper3d.com,2017-05-18:2985220:Comment:17535392017-05-18T07:40:11.490ZDavid Ruttenhttps://www.grasshopper3d.com/profile/DavidRutten
<p>If you ask for the IGH_Goo type, then you always get the original data:</p>
<blockquote><p>GH_Brep brep = null;</p>
<p>if (!DA.GetData(0, ref brep)) return;</p>
</blockquote>
<p></p>
<p>No copying. If you ask for a derived type, then it depends on how the conversion is defined. For almost all (I'd say <em>all</em>, but I can't remember for sure) native types this is also done without copying. I.e.:</p>
<blockquote><p>Brep brep = null;</p>
<p>if (!DA.GetData(0, ref brep))…</p>
</blockquote>
<p>If you ask for the IGH_Goo type, then you always get the original data:</p>
<blockquote><p>GH_Brep brep = null;</p>
<p>if (!DA.GetData(0, ref brep)) return;</p>
</blockquote>
<p></p>
<p>No copying. If you ask for a derived type, then it depends on how the conversion is defined. For almost all (I'd say <em>all</em>, but I can't remember for sure) native types this is also done without copying. I.e.:</p>
<blockquote><p>Brep brep = null;</p>
<p>if (!DA.GetData(0, ref brep)) return;</p>
</blockquote>
<p></p>
<p>also gives you a reference to the actual brep that is inside the GH_Brep instance, again no copying.</p>
<p></p>
<p>When you use VB/C# script components, then you always get a safe instance you can change. I did this because I figured scripters would be less savvy than VS developers about making sure data is copied whenever necessary.</p>
<p></p>
<p>When making a GHA, and you want to modify the data in the input, you must <em>always</em> make a copy yourself. It's the only way to be sure.</p> Thanks a lot Tom!I see, that…tag:www.grasshopper3d.com,2017-05-18:2985220:Comment:17535342017-05-18T07:18:43.523ZBenjamin Felbrichhttps://www.grasshopper3d.com/profile/BenjaminFelbrich
<p>Thanks a lot Tom!<br/><br/>I see, that was my assumption. I was thinking about passing pointers as well, but since myClass is a reference type I can not use pointers in C# (apparently C# only supports pointers for value types and structs of such).<br/><br/>Your idea of making a sort of custom pointer is very good, I'll try that.<br/><br/>Thanks again! Cheers</p>
<p>Thanks a lot Tom!<br/><br/>I see, that was my assumption. I was thinking about passing pointers as well, but since myClass is a reference type I can not use pointers in C# (apparently C# only supports pointers for value types and structs of such).<br/><br/>Your idea of making a sort of custom pointer is very good, I'll try that.<br/><br/>Thanks again! Cheers</p>