Export Custom Type Variable on Inspector (Mono C#)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lazlo

Hi,
I would like to export a custom type variable on the inspector when i try to i get the error :
modules/mono/csharp_script.cpp:2219 - Unknown type of exported member: MyNode.m
modules/mono/csharp_script.cpp:2219 - Unknown type of exported member: MyNode.ms

MyCode :
public class MyType
{
public string name;
public int i;
}

public class MyNode : Node2D
{
    [Export]
    MyType[] ms;
    [Export]
    MyType m;
}

    public class MyType
{
    public string name;
    public int i;
}
 
public class MyNode : Node2D
{
    [Export]
    MyType[] ms;
    [Export]
    MyType m;
}

Is there a way to display custom types in Godot Inspector using C#?

:bust_in_silhouette: Reply From: Zylann

Godot doesn’t support exporting arbitrary types, it has to be a primitive type or a Resource (which can be a custom one).

I haven’t done that often and I don’t use C# in Godot, but here are some suggestions:
If you want to export your custom type you could make it inherit Resource and export it as Resource.
Then in the inspector, under the property, create a new Resource, and add your custom resource script on it. At least, that’s how it can work in GDScript.
If that doesn’t work, try to make your field a Resource instead of MyClass, then cast it to obtain your derived type.

You could bypass the extra step of adding resource + adding script by registering your class with a global class name so it will appear in the New Resource dialog (a Godot feature, not related to C#) but I haven’t tried with C#.

:bust_in_silhouette: Reply From: wmigor

Use Resource and this addon GitHub - wmigor/godot-mono-custom-resource-register: Godot plugin for register custom resources