summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2024-06-03 15:58:33 +0200
committerRémi Verschelde <rverschelde@gmail.com>2024-06-03 15:58:33 +0200
commitef065d2c5a6c10e8ae008dc27c012155e5ecc95a (patch)
treeea858edeb57721114c02b9b29a75ba21aa981a02
parent0d11108a01ca5017e055e498c579a271f3153ee1 (diff)
parent653a8b113a9677e534fa7061f39c7f56b3ed3663 (diff)
downloadredot-engine-ef065d2c5a6c10e8ae008dc27c012155e5ecc95a.tar.gz
Merge pull request #90487 from preslavnpetrov/export-script-typed-node-variables
Register the export info correctly when a script is used as the variable type for Node
-rw-r--r--modules/gdscript/gdscript_parser.cpp36
-rw-r--r--modules/gdscript/tests/scripts/parser/features/export_variable.gd6
-rw-r--r--modules/gdscript/tests/scripts/parser/features/export_variable.notest.gd2
-rw-r--r--modules/gdscript/tests/scripts/parser/features/export_variable.out4
4 files changed, 38 insertions, 10 deletions
diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp
index 9a4c92f601..771ccf47b7 100644
--- a/modules/gdscript/gdscript_parser.cpp
+++ b/modules/gdscript/gdscript_parser.cpp
@@ -4315,39 +4315,55 @@ bool GDScriptParser::export_annotations(const AnnotationNode *p_annotation, Node
return false;
}
break;
- case GDScriptParser::DataType::CLASS:
+ case GDScriptParser::DataType::CLASS: {
+ StringName class_name;
+ if (export_type.class_type) {
+ class_name = export_type.class_type->get_global_name();
+ }
+ if (class_name == StringName()) {
+ push_error(R"(Script export type must be a global class.)", p_annotation);
+ return false;
+ }
if (ClassDB::is_parent_class(export_type.native_type, SNAME("Resource"))) {
variable->export_info.type = Variant::OBJECT;
variable->export_info.hint = PROPERTY_HINT_RESOURCE_TYPE;
- variable->export_info.hint_string = export_type.to_string();
+ variable->export_info.hint_string = class_name;
} else if (ClassDB::is_parent_class(export_type.native_type, SNAME("Node"))) {
variable->export_info.type = Variant::OBJECT;
variable->export_info.hint = PROPERTY_HINT_NODE_TYPE;
- variable->export_info.hint_string = export_type.to_string();
+ variable->export_info.hint_string = class_name;
} else {
push_error(R"(Export type can only be built-in, a resource, a node, or an enum.)", p_annotation);
return false;
}
+ } break;
- break;
case GDScriptParser::DataType::SCRIPT: {
StringName class_name;
- StringName native_base;
if (export_type.script_type.is_valid()) {
- class_name = export_type.script_type->get_language()->get_global_class_name(export_type.script_type->get_path());
- native_base = export_type.script_type->get_instance_base_type();
+ class_name = export_type.script_type->get_global_name();
}
if (class_name == StringName()) {
Ref<Script> script = ResourceLoader::load(export_type.script_path, SNAME("Script"));
if (script.is_valid()) {
- class_name = script->get_language()->get_global_class_name(export_type.script_path);
- native_base = script->get_instance_base_type();
+ class_name = script->get_global_name();
}
}
- if (class_name != StringName() && native_base != StringName() && ClassDB::is_parent_class(native_base, SNAME("Resource"))) {
+ if (class_name == StringName()) {
+ push_error(R"(Script export type must be a global class.)", p_annotation);
+ return false;
+ }
+ if (ClassDB::is_parent_class(export_type.native_type, SNAME("Resource"))) {
variable->export_info.type = Variant::OBJECT;
variable->export_info.hint = PROPERTY_HINT_RESOURCE_TYPE;
variable->export_info.hint_string = class_name;
+ } else if (ClassDB::is_parent_class(export_type.native_type, SNAME("Node"))) {
+ variable->export_info.type = Variant::OBJECT;
+ variable->export_info.hint = PROPERTY_HINT_NODE_TYPE;
+ variable->export_info.hint_string = class_name;
+ } else {
+ push_error(R"(Export type can only be built-in, a resource, a node, or an enum.)", p_annotation);
+ return false;
}
} break;
case GDScriptParser::DataType::ENUM: {
diff --git a/modules/gdscript/tests/scripts/parser/features/export_variable.gd b/modules/gdscript/tests/scripts/parser/features/export_variable.gd
index 8b343de5ef..483e6cab0d 100644
--- a/modules/gdscript/tests/scripts/parser/features/export_variable.gd
+++ b/modules/gdscript/tests/scripts/parser/features/export_variable.gd
@@ -1,6 +1,8 @@
+class_name ExportVariableTest
extends Node
const Utils = preload("../../utils.notest.gd")
+const PreloadedScript = preload("./export_variable.notest.gd")
# Built-in types.
@export var test_weak_int = 1
@@ -20,6 +22,10 @@ const Utils = preload("../../utils.notest.gd")
@export var test_image: Image
@export var test_timer: Timer
+# Global custom classes.
+@export var test_global_class: ExportVariableTest
+@export var test_preloaded_script: PreloadedScript
+
# Arrays.
@export var test_array: Array
@export var test_array_bool: Array[bool]
diff --git a/modules/gdscript/tests/scripts/parser/features/export_variable.notest.gd b/modules/gdscript/tests/scripts/parser/features/export_variable.notest.gd
new file mode 100644
index 0000000000..6d064351c1
--- /dev/null
+++ b/modules/gdscript/tests/scripts/parser/features/export_variable.notest.gd
@@ -0,0 +1,2 @@
+class_name ExportPreloadedClassTest
+extends Node
diff --git a/modules/gdscript/tests/scripts/parser/features/export_variable.out b/modules/gdscript/tests/scripts/parser/features/export_variable.out
index 99d7b27130..bb094e14b4 100644
--- a/modules/gdscript/tests/scripts/parser/features/export_variable.out
+++ b/modules/gdscript/tests/scripts/parser/features/export_variable.out
@@ -23,6 +23,10 @@ var test_image: Image = null
hint=RESOURCE_TYPE hint_string="Image" usage=DEFAULT|SCRIPT_VARIABLE class_name=&"Image"
var test_timer: Timer = null
hint=NODE_TYPE hint_string="Timer" usage=DEFAULT|SCRIPT_VARIABLE class_name=&"Timer"
+var test_global_class: ExportVariableTest = null
+ hint=NODE_TYPE hint_string="ExportVariableTest" usage=DEFAULT|SCRIPT_VARIABLE class_name=&"ExportVariableTest"
+var test_preloaded_script: ExportPreloadedClassTest = null
+ hint=NODE_TYPE hint_string="ExportPreloadedClassTest" usage=DEFAULT|SCRIPT_VARIABLE class_name=&"ExportPreloadedClassTest"
var test_array: Array = []
hint=NONE hint_string="" usage=DEFAULT|SCRIPT_VARIABLE class_name=&""
var test_array_bool: Array = Array[bool]([])