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 reload generates error when created dynamically (no backing script) #65263

Closed
bitwes opened this issue Sep 2, 2022 · 3 comments · Fixed by #67714
Closed

GDScript reload generates error when created dynamically (no backing script) #65263

bitwes opened this issue Sep 2, 2022 · 3 comments · Fixed by #67714

Comments

@bitwes
Copy link

bitwes commented Sep 2, 2022

Godot version

v4.0.alpha15.official.432b25d36

System information

OSX 10.15.7

Issue description

When you create a GDScript object, set the source, and then call reload it generates the following error:

ERROR: Attempt to open script '' resulted in error 'File not found'.
   at: load_source_code (modules/gdscript/gdscript.cpp:1064)

Steps to reproduce

This script creates a simple GDScript from a string of source code, creates an instance and calls a method on it.

extends SceneTree

func _init():
	var script_source = '' + \
	"func hello_world():\n" + \
	"\tprint('--- hello world ---')"

	print(script_source)

	var DynScript = GDScript.new()
	DynScript.source_code = script_source
	print('pre-reload')
	DynScript.reload()
	print('post-reload')

	var inst = DynScript.new()
	inst.hello_world()

	quit()

The output shows the error occurs on the reload call.

Godot Engine v4.0.alpha15.official.432b25d36 - https://godotengine.org
Vulkan API 1.1.216 - Using Vulkan Device #0: AMD - AMD Radeon Pro 450
 
Registered camera FaceTime HD Camera (Built-in) with id 1 position 0 at index 0
func hello_world():
        print('--- hello world ---')
pre-reload
ERROR: Attempt to open script '' resulted in error 'File not found'.
   at: load_source_code (modules/gdscript/gdscript.cpp:1064)
post-reload
--- hello world ---

Minimal reproduction project

See steps to reproduce

@L4Vo5
Copy link
Contributor

L4Vo5 commented Sep 17, 2022

This can be reproduced with just these two lines

var DynScript = GDScript.new()
DynScript.reload()

The error doesn't happen if a non-empty resource path is set, even if the path is completely invalid

var DynScript = GDScript.new()
DynScript.resource_path = "\n :)"
DynScript.reload()

@bitwes
Copy link
Author

bitwes commented Sep 21, 2022

With the resource_path trick I was able to get rid of all the error messages with this workaround. It's gross, but there's no more red error messages in my output now.

var _created_script_count = 0
func create_script_from_source(source):
	_created_script_count += 1

	var DynamicScript = GDScript.new()
	DynamicScript.source_code = source
	# The resource_path must be unique or Godot thinks it is trying
	# to load something it has already loaded and generates an error like
	# ERROR: Another resource is loaded from path 'workaround for godot issue #65263' (possible cyclic resource inclusion).
	DynamicScript.resource_path = str('workaround for godot issue #65263 (', _created_script_count, ')')
	var result = DynamicScript.reload()

	return DynamicScript

@adamscott
Copy link
Member

I added a check in my PR to not (re)load source code if the path is empty. I needed to do this to handle empty paths with my fix for cyclic references purposes, but it should fix your issue as well.

Repository owner moved this from To Assess to Done in 4.x Priority Issues Nov 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants