You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm finding I'm running into a lot of strange errors when using custom resources in C#. Using GDScript, I can:
define a custom resource (let's call it ItemResource) and have it export a property of type Texture called 'icon'
create a scene (let's call it ItemButton and have it extend TextureButton) with a script marked as 'tool' which also exports a property of type 'resource'
override the _process() method of ItemButton to update the image based on the 'icon' field in the resource data
create a second scene (let's call it MainMenu) that has an instance of ItemButton in it
set the resource field of my button and see it update in the editor viewport
When I try to do the same thing in C#, it fails. First of all, the resource fields initially return null even if they have been set (I'm guessing some internal process has not finished setting up the resource yet). If you try to get the value of a field of type Texture before your resource has finished setting up, Godot will crash (as a work around, I have to check if one of the string fields on my resource is null before trying to get the Texture value). Trying to do any of this in a script marked [Tool] seems right out. Any tool script that attempts to access the resource in the _Process() method prints an unending stream of error messages to the console:
Unhandled Exception:
System.InvalidCastException: Specified cast is not valid.
at ItemButton._Process (System.Single delta) [0x00001] in <fee57e23638e4718980313c16c4a0742>:0
ERROR: System.InvalidCastException: Specified cast is not valid.
at: debug_send_unhandled_exception_error (modules/mono/mono_gd/gd_mono_utils.cpp:369)
Steps to reproduce
I'm attaching two zip files that demonstrate the problem. The first is in GDScript and demonstrates what I want to do. The second is in C# and does not work.
In your minimal reproduction project, the class ItemResource does not have the [Tool] attribute. In the editor, classes without the [Tool] attribute don't exist so they can't be instantiated, instead the editor will make an instance of the type Resource and that's why casting the Resource instance to ItemResource throws the exception.
I'm pretty sure this is working as intended, every class that you want to use in the editor needs to have the [Tool] attribute (including resources) so if you want to use a resource in a tool class, the resource class needs to be a tool too.
Godot version
3.4 stable
System information
Windows 10
Issue description
I'm finding I'm running into a lot of strange errors when using custom resources in C#. Using GDScript, I can:
When I try to do the same thing in C#, it fails. First of all, the resource fields initially return null even if they have been set (I'm guessing some internal process has not finished setting up the resource yet). If you try to get the value of a field of type Texture before your resource has finished setting up, Godot will crash (as a work around, I have to check if one of the string fields on my resource is null before trying to get the Texture value). Trying to do any of this in a script marked [Tool] seems right out. Any tool script that attempts to access the resource in the _Process() method prints an unending stream of error messages to the console:
Steps to reproduce
I'm attaching two zip files that demonstrate the problem. The first is in GDScript and demonstrates what I want to do. The second is in C# and does not work.
Minimal reproduction project
ResourceTestCs.zip
ResourceTestGDscript.zip
The text was updated successfully, but these errors were encountered: