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

In C#, resources not being loaded in Tool scripts #55596

Closed
blackears opened this issue Dec 3, 2021 · 2 comments
Closed

In C#, resources not being loaded in Tool scripts #55596

blackears opened this issue Dec 3, 2021 · 2 comments

Comments

@blackears
Copy link

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:

  • 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.

Minimal reproduction project

ResourceTestCs.zip
ResourceTestGDscript.zip

@raulsntos
Copy link
Member

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.

@blackears
Copy link
Author

Thanks. I didn't realize that the resources need to be marked [Tool] as well. It is working now.

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

4 participants