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

[GDScript] Loading non-existing gdscript returns valid instance. #76941

Closed
zaevi opened this issue May 11, 2023 · 4 comments · Fixed by #76954
Closed

[GDScript] Loading non-existing gdscript returns valid instance. #76941

zaevi opened this issue May 11, 2023 · 4 comments · Fixed by #76954

Comments

@zaevi
Copy link
Contributor

zaevi commented May 11, 2023

Godot version

4.1.dev

System information

Win11

Issue description

I load a non-existing GDScript, but it still returns a valid instance instead of null.

@tool
extends EditorScript

func _run():
	var s = ResourceLoader.load("non_exists.gd", "", ResourceLoader.CACHE_MODE_IGNORE)
	print(s)
	assert(s == null)
  Attempt to open script 'res://non_exists.gd' resulted in error 'File not found'.
<GDScript#-9223369015982753437>
  res://new_script.gd:7 - Assertion failed.

Steps to reproduce

N/A

Minimal reproduction project

N/A

@zaevi
Copy link
Contributor Author

zaevi commented May 11, 2023

Ref<Resource> ResourceFormatLoaderGDScript::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
if (r_error) {
*r_error = ERR_FILE_CANT_OPEN;
}
Error err;
Ref<GDScript> scr = GDScriptCache::get_full_script(p_path, err, "", p_cache_mode == CACHE_MODE_IGNORE);
if (scr.is_null()) {
// Don't fail loading because of parsing error.
scr.instantiate();
}
if (r_error) {
*r_error = OK;
}
return scr;
}

Seems if not exist it will instantiate. cc @vnen

@Rindbee
Copy link
Contributor

Rindbee commented May 11, 2023

Currently, there doesn't seem to be a distinction between errors in parsing and errors in load_source_code(). When load_source_code() fails, GDScriptCache::get_shallow_script() should return Ref<GDScript>() without caching.

script->load_source_code(p_path);

@KoBeWi
Copy link
Member

KoBeWi commented May 11, 2023

This leads to a bug where opening a scene with missing script dependencies will open them as empty scripts.

@anvilfolk
Copy link
Contributor

anvilfolk commented Jun 15, 2023

I was looking at the linked PR and I'm not 100% it's the best approach. I think the real issue is that in C++, ResourceLoader::load() has a return parameter for the error errors, but you can't do that in GDScript ResourceLoader.load().

I suspect the right behavior is still to return the most-parsed/analyzed script possible, but still have access to an error of some sort... though I have no idea how we'd go about implementing that, unless we had some weird ResourceLoader.error(), which would attempt the load (possibly cache so it's fast to grab the resource after), but then return the error instead of the resource 🙃

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

Successfully merging a pull request may close this issue.

5 participants