Hey,

I'm desesperatly trying to build a plugin to edit my custom resource.

Here's my custom resource class:

using Godot;
using System;

public class ResourceTest : Resource
{

}

And the plugin code:

#if TOOLS
using Godot;
using System;

[Tool]
public class MyPlugin : EditorPlugin
{
    public override void _EnterTree()
    {

        AddCustomType("ResourceTest", "Resource", GD.Load<Script>("res://ResourceTest.cs"), null);
    }

    public override void _ExitTree()
    {
        RemoveCustomType("ResourceTest");
    }

    public override bool Handles(Godot.Object @object)
    {
        //here @object is always a Resource and never a ResourceTest
        return base.Handles(@object);
    }
}
#endif

The problem is: the @object parameter in the Handles function is never a ResourceTest, even when I actually open a ResourceTest in the editor, its class is always Resource and I can't cast it to ResourceTest. It's like the resource object lost its base class in the process. How can I do?

Thanks in advance for your help!

Welcome to the forums @Kahel!

How are you casting it to ResourceTest? In my C# plugin, I think I've been able to cast using code like CustomClass = (CustomClass)Variable without any issue, though you will want to type check first as I believe this type of casting will crash if it cannot be cast correctly.

Thank you!

I used the following syntax:

if(@object is ResourceTest resourceTest)
{
    //do stuff
}

but it never enter the if scope because @object can't be cast to ResourceTest

And when I simply use ResourceTest resourceTest = (ResourceTest)@object; I got a runtime error " System.InvalidCastException: Specified cast is not valid."

I started to think you can't create a plugin for a custom c# resource type but it seems you succeed to make it somehow.

EDIT: I figured it out! I needed to put [Tool] in the ResourceTest.cs script

Awesome, I’m glad you were able to figure it out! And thanks for sharing the solution :)

2 years later