#Exporting Custom Resource Types from Tool Scripts in C#

1 messages · Page 1 of 1 (latest)

earnest wraith
#

I am encountering an issue with Exporting Custom Resources in C# from a Tool Script.

For example, I have a custom Resource type "TheResource" that I want to export.

    [Export(PropertyHint.ResourceType,"TheResource")]
    public TheResource MyResource
    {
        get { return _myResource; }
        set { _myResource = value; }
    }

    private TheResource _myResource = new TheResource();

It does export, however if I clear the Exported Resource and add a new TheResource from the editor, it gives me an error.

/root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:113 - System.InvalidCastException: Unable to cast object of type 'Godot.Resource' to type 'TheResource'.

So I was thinking, maybe Exported Attributes doesn't support custom Resources (but if it does, then why doesn't it show error like other unsupported data types??), so I tried with the base Resource class, but then I would be forced to cast it into the TheResource class, which is not possible. This just shifts the problem, shifts the error.

    [Export(PropertyHint.ResourceType,"TheResource")]
    public Resource MyResource
    {
        get { return _myResource; }
        set { _myResource = value as TheResource; }
    }

    private TheResource _myResource = new TheResource();

Then I was thinking, what if the _myResource ALSO had to be the base Resource class. And, it worked! However, it is now completely useless, as I cannot access the properties within my custom Resource. Again, this shifts the problem because when a resource is created in editor, it is a resource of base class.

    [Export(PropertyHint.ResourceType,"TheResource")]
    public Resource MyResource
    {
        get { return _myResource; }
        set { _myResource = value; }
    }

    private Resource _myResource = new TheResource();

    private void PrintNumber()
    {
        GD.Print(_myResource.number); // Error here
    }

Please help

#
    private void PrintNumber()
    {
        GD.Print(((TheResource)_myResource).number);
    }

also doesn't solve this, as the resource created by clicking "New TheResource" in editor is of the Resource class, not the "TheResource" class, therefore cannot be cast and gives an error.

#

If there was a way to force this button to create a "TheResource" resource instead of the base class Resource, this would very likely solve it, however I do not know a way.

#

If it helps, this is the "TheResource" resource

[GlobalClass]
public partial class TheResource : Resource
{
    [Export]

    public int number { get; set; }

}
earnest wraith
#

https://docs.godotengine.org/en/stable/tutorials/scripting/c_sharp/c_sharp_global_classes.html

After reading documentation, I have determined that the first example I showed should work. However, it doesn't. My code is basically identical to the example they show, so why do I get that error?

#

Update: It's because my script was a Tool. This issue only exists in Tool Scripts. Is there a way to get around this issue? Are Tool scripts limited to certain export types? What do I do?

earnest wraith
#

Exporting Custom Resource Types from Tool Scripts in C#