godot icon indicating copy to clipboard operation
godot copied to clipboard

C# Unable to cast custom class to internal class even if it inherits internal class if using Tool scripts

Open Hanprogramer opened this issue 1 year ago • 1 comments

Tested versions

  • Reproducible in Godot 4.2 C#

System information

Godot v4.2.1.stable.mono - Windows 10.0.22000 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 2050 (NVIDIA; 31.0.15.4618) - 13th Gen Intel(R) Core(TM) i5-13420H (12 Threads)

Issue description

So my class TableContainer inherits VBoxContainer. I exported this class from a regular Control class (which is a [Tool] script). When I try to assign the node via editor I got this error:

 /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:112 - System.InvalidCastException: Unable to cast object of type 'Godot.VBoxContainer' to type 'exported_class'.
     at Control.RestoreGodotObjectData(GodotSerializationInfo info) in D:\...\Godot.SourceGenerators\Godot.SourceGenerators.ScriptSerializationGenerator\Control_ScriptSerialization.generated.cs:line 19
     at Godot.Bridge.CSharpInstanceBridge.DeserializeState(IntPtr godotObjectGCHandle, godot_dictionary* propertiesState, godot_dictionary* signalEventsState) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/CSharpInstanceBridge.cs:line 255

Which will work fine if I remove the [Tool] attribute from the Control class I made and works fine if i add it to the TableContainer class as well.

I'm not sure if this is intended behavior, but maybe if it is there should be a better error message explaining it. (p.s. I needed it to be [Tool] because I'm writing editor plugin)

Steps to reproduce

  • Create empty project.
  • Create empty Control scene
  • Create another empty Control scene (TableContainer class)
  • Export the TableContainer class from the main Control scene
  • Add [Tool] to the main script
  • Set the value in editor
  • Error

Minimal reproduction project (MRP)

CSharpTestBed.zip

Hanprogramer avatar Mar 24 '24 18:03 Hanprogramer

Hello. If I understand correctly, you're saying that, in a [Tool] context, the following works fine.

[Tool]
public partial class Foo : Control
{
  [Export]
  public Bar MyBar;
}

[Tool]
public partial class Bar : Control
{
}

But the following does not.

[Tool]
public partial class Foo : Control
{
  [Export]
  public Bar MyBar;
}

public partial class Bar : Control
{
}

If so, this is expected, as Bar would not be available in this context without using the [Tool] attribute.

paulloz avatar Mar 24 '24 19:03 paulloz

Ah yes. That's exactly the case. Then it would be better to add more descriptive error messages for that i think. Because in the editor it's kinda vague. Anyways issue closed

Hanprogramer avatar Mar 25 '24 14:03 Hanprogramer