Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidCastException is thrown when trying to assign a c# resource to a tool Node in editor #80298

Open
Saplonily opened this issue Aug 5, 2023 · 2 comments

Comments

@Saplonily
Copy link

Saplonily commented Aug 5, 2023

Godot version

v4.2 dev.mono 16a9356

System information

Godot v4.2.dev.mono (16a9356) - Windows 10.0.19045 - Vulkan (Forward+) - integrated Intel(R) UHD Graphics 630 (Intel Corporation; 31.0.101.2121) - Intel(R) Core(TM) i5-10400 CPU @ 2.90GHz (12 Threads)

Issue description

InvalidCastException thrown when trying to assign a c# resource to a tool Node in editor.
Actually this bug has been here since c# Resource is supported.

The exception stack:

D:/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:112 - System.InvalidCastException: Unable to cast object of type 'Godot.Resource' to type 'CSharpResource'.
     at CSharpNode.SetGodotClassPropertyValue(godot_string_name& name, godot_variant& value) in D:\csres\Godot.SourceGenerators\Godot.SourceGenerators.ScriptPropertiesGenerator\CSharpNode_ScriptProperties.generated.cs:line 13
     at Godot.Bridge.CSharpInstanceBridge.Set(IntPtr godotObjectGCHandle, godot_string_name* name, godot_variant* value) in D:\godot\modules\mono\glue\GodotSharp\GodotSharp\Core\Bridge\CSharpInstanceBridge.cs:line 57

Steps to reproduce

  1. Make a new scene with a single Node Node
  2. Declare a new c# resource class:
using Godot;
[GlobalClass]
public partial class CSharpResource : Resource
{
    // some props...
    [Export] public int AInt { get; set; }
}
  1. Attach a c# script named CSharpNode.cs with a CSharpResource property and tool enabled(necessary) to the Node:
using Godot;
[GlobalClass, Tool]
public partial class CSharpNode : Node
{
    [Export] CSharpResource MyRes { get; set; }
}
  1. Reopen the scene to make Tool works.
  2. Assign a CSharpResource to the MyRes property.
  3. Then the exception is thrown.

Minimal reproduction project

csres.zip

@raulsntos
Copy link
Member

You forgot to add the [Tool] attribute to CSharpResource. Every script referenced in a Tool script needs to also be a Tool script so it can be instantiated in the editor.

@Saplonily
Copy link
Author

Saplonily commented Aug 6, 2023

You forgot to add the [Tool] attribute to CSharpResource. Every script referenced in a Tool script needs to also be a Tool script so it can be instantiated in the editor.

Thanks for replying! It works.
Maybe we can print a friendlier error about the need to add the Tool attribute instead of instantiating a Resource that leads to InvalidCastException?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants