godot icon indicating copy to clipboard operation
godot copied to clipboard

GDScript: Add error message when a GDScript resource fails to load.

Open anvilfolk opened this issue 2 years ago • 1 comments

Currently, GDScripts who are only loaded through ResourceLoader::load(), like Autoloads, do not have a pathway to announce there is an error in their code. This contributes to significant confusion when errors occur in projects when autoloads are involved. At least partially closes #78230.

anvilfolk avatar Jun 21 '23 21:06 anvilfolk

Added the cherrypick:4.1 label as this would help 4.1 users with errors in their code, not a new feature per se.

adamscott avatar Jun 22 '23 18:06 adamscott

Recently encountered this, and wanted to open a PR to fix but found this. @anvilfolk are you still working on this? Looks like it's ready, just needs the suggested patch applied?

Happy to help test or contribute if any help is needed here

OmarShehata avatar Jul 30 '23 23:07 OmarShehata

Recently encountered this, and wanted to open a PR to fix but found this. @anvilfolk are you still working on this? Looks like it's ready, just needs the suggested patch applied?

Happy to help test or contribute if any help is needed here

Sorry, life's been a bit of a mess. I'm hoping to have a little more time in the coming weeks and want to work on this again, yes :)

@Rindbee so if !scr->is_valid(), then the error is usually that the file was not found? Whereas if scr->is_valid(), then the file was found, but there may be some parsing/analyzing/compilation error? Does that sound right?

anvilfolk avatar Jul 31 '23 12:07 anvilfolk

@Rindbee so if !scr->is_valid(), then the error is usually that the file was not found?

This usually means an error occurred in GDScript::load_source_code().

https://github.com/godotengine/godot/blob/3fa8fad26b97a8af20e7996b7e17d8f23fc04b89/modules/gdscript/gdscript_cache.cpp#L257-L261

Other than that, it's a valid script.

This is for the first load, subsequent loads are a bit different.

https://github.com/godotengine/godot/blob/3fa8fad26b97a8af20e7996b7e17d8f23fc04b89/modules/gdscript/gdscript_cache.cpp#L296-L299

Rindbee avatar Jul 31 '23 12:07 Rindbee

A couple of notes:

  • I'm using ERR_PRINT_ED in an attempt to generate a visible error in the editor, but unfortunately when autoloads are loaded, the editor isn't quite open yet so the error does not appear.
  • ERR_PRINT_ED does still print an error to the console, so when you load your project in the editor, the OS console will have the error log.
  • When you run your game from the editor, the errors will show up in the editor console.
  • This approach results in "duplicate" errors, e.g. Godot will still complain about the auloload not inheriting from Node, but will now also print another error stating why (parse error, for example).
  • ~~Will only print errors if we are not passing the error back to the caller - that means it's ResourceFormatLoaderGDScript::load's responsibility to report the error.~~ This is actually not possible because loading tasks do use return errors but silently ignore them.

I don't think we can get around the "duplicated" errors, since ResourceLoader::load() specifically does not want to fail due to GDScript parsing errors, and will return OK if the parsing fails.

anvilfolk avatar Jul 31 '23 15:07 anvilfolk

Thanks!

akien-mga avatar Aug 03 '23 13:08 akien-mga

Cherry-picked for 4.1.2.

YuriSizov avatar Sep 21 '23 13:09 YuriSizov