godot icon indicating copy to clipboard operation
godot copied to clipboard

Parse error when `preload` resource with build-in script.

Open zaevi opened this issue 2 years ago • 2 comments

Godot version

4.0.rc3

System information

Win11

Issue description

I write a resource with build-in script: (res://myresource.tres)

[gd_resource type="Resource" load_steps=2 format=3 uid="uid://bfbb3f86bl2kv"]

[sub_resource type="GDScript" id="GDScript_408b7"]
script/source = "extends Resource

@export var prop : String
@export var prop2 : int
"

[resource]
script = SubResource("GDScript_408b7")
prop = "myvalue"
prop2 = 123

When I preload the resource, the editor will throw parse error:

func _ready():
	# Parse Error: Could not find script "res://myresource.tres::GDScript_408b7"
	preload("res://myresource.tres")

It's ok to load and read properties:

	var res = load("res://myresource.tres")
	print(res.prop) # myvalue
	print(res.prop2) # 123

Steps to reproduce

check Main.gd below

Minimal reproduction project

preload_resource_script_test.zip

zaevi avatar Feb 23 '23 09:02 zaevi

Bug is still there on 4.1.1

Mysgym avatar Sep 18 '23 14:09 Mysgym

In reduce_preload, it will parse the attached gdscript. However, the parser only treats all the resource path as file path and doesn't handle the case that the path could be a subresource path. https://github.com/godotengine/godot/blob/1917bc3454e58fc56750b00e04aa25cb94d8d266/modules/gdscript/gdscript_cache.cpp#L227-L230

The documentation suggests creating a custom resource by setting the script in the inspector, but it didn't stress the script should be attached. When the user creates new script via inspector and save it in the editor, the script will not be saved as a separate file. Instead, it makes that script a subresource that cannot be preload. The documentation didn't include this possible issue with preload and subresource. Maybe we should update the documentation.

A workaround is to save the script as a file. If it is already a subresource, in the inspector: Right click on Script tab -> make unique -> Right click again -> save as.

EAinsley avatar Oct 02 '24 20:10 EAinsley