Hi,
I usually use the serialization in Grasshopper C# Components through overriding the Write and Read methods and this works fine.
Is there a way to use serialization, using Read and Write (or equivalent) inside a C# script directly on Grasshoppes canvas?
No, you can’t use the Read and Write methods of C# scripting comp.
Yes, you can do your own (de)serialization in your own file, in that case consider use the Data Input and Data Ouput components. If you want to include it in the gh file, you must use gh objects, custom or native. Maybe you can use a panel to set your data or a text (or your data type) parameter.
Another alternative can be to use the GH_IO library to change the current document, but it sounds very problematic.
Hi Dani,
Thank for your reply. Lets consider I will follow the 2° option “include it in the gh file, you must use gh objects, custom or native.”. In this case the serialization is performed in a standard way, e.g. “BinaryFormatter”? Do you have any examples?
In that case you don’t (de)serialize, you just need set and get the persistant value with your data of gh object like a panel or parameter. Gh will do the rest.
Dear Dani,
Thanks for reply and link, was very usefull. Maybe was not very clear but what I really need, to exlain it better, is to store a value inside the C# Script like this:
private void RunScript()
{
Method() -> Value;
if storedValue == Value -> No action
if storedValue != Value -> Action, storedValue = Value;
}
string storedValue = “”;
Now, I tried to apply what I think you explained to me, but not work as expected:
a.) Is difficult to retrieve data;
b.) Data is not saved when i re-open the Gh file.
You can’t serialize data inside a scripting component. What you can do is to store a runtime variable inside the < Custom additional code >. But each time the component starts its instance, this value will return to its default or original value.
If you just need a boolean, why not to use a toogle button and keep it simple?
Maybe I was not clear. I not mean to get/set the value in a scripting component parameter, but in an isolated parameter in the canvas (all the components in the Params tab, Geometry and Primitive panels are parameters, same as input and output componet, but isolated). You can do that from code and use it as a database. But again, if you just need to store a simple boolean or integer, don’t get complicated, especially if you’re not familiar with these hacks, and use a simple solution, ie, a source parameter. In case you really need to change this source value, then yes, try what I suggest you before but in an isolated parameter, not in a scripting component parameter.
Hi Dani,
Thanks for your great support!! Principaly I wanted to avoid any component creation since I planned to do some very simple tests within a particular application. However I think i will try using a component that is more familiar for me to solve the problem.
A little late ^^, but I needed something similar and using the volatile data didn’t do the trick.
This however solved it for me and I figured I share it:
private void RunScript(double x, bool internalize, ref object A)
{
if (internalize)
{
((Grasshopper.Kernel.Parameters.Param_ScriptVariable) Component.Params.Input[0]).PersistentData.Clear();
((Grasshopper.Kernel.Parameters.Param_ScriptVariable) Component.Params.Input[0]).PersistentData.Append(new GH_Number(x));
Component.Params.Input[0].RemoveAllSources();
}
A = ((Grasshopper.Kernel.Parameters.Param_ScriptVariable) Component.Params.Input[0]).PersistentData.AllData(true);
}
I was looking to programmatically internalize inputs in script components and ran into the error: Unable to cast object of type 'Grasshopper.Kernel.Parameters.Param_ScriptVariable' to type 'Grasshopper.Kernel.GH_PersistentParam1[Grasshopper.Kernel.Types.GH_Number]'.`
So if you attach a number to input x and then set internalize to true, the same thing happens as if you used the context menu (internalize data) on the input of the scripted component.