Skip to content

Commit c6b6278

Browse files
committed
Merge pull request godotengine#102737 from KoBeWi/uids_are_creeping_everywhere
Store `_custom_type_script` meta as String
2 parents bd87c3a + 38d0e82 commit c6b6278

File tree

5 files changed

+49
-15
lines changed

5 files changed

+49
-15
lines changed

editor/editor_data.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "editor/multi_node_edit.h"
4040
#include "editor/plugins/editor_context_menu_plugin.h"
4141
#include "editor/plugins/editor_plugin.h"
42+
#include "scene/property_utils.h"
4243
#include "scene/resources/packed_scene.h"
4344

4445
void EditorSelectionHistory::cleanup_history() {
@@ -536,15 +537,18 @@ Variant EditorData::instantiate_custom_type(const String &p_type, const String &
536537
if (get_custom_types()[p_inherits][i].name == p_type) {
537538
Ref<Script> script = get_custom_types()[p_inherits][i].script;
538539

539-
Variant ob = ClassDB::instantiate(p_inherits);
540-
ERR_FAIL_COND_V(!ob, Variant());
540+
// Store in a variant to initialize the refcount if needed.
541+
Variant v = ClassDB::instantiate(p_inherits);
542+
ERR_FAIL_COND_V(!v, Variant());
543+
Object *ob = v;
544+
541545
Node *n = Object::cast_to<Node>(ob);
542546
if (n) {
543547
n->set_name(p_type);
544548
}
545-
n->set_meta(SceneStringName(_custom_type_script), script);
546-
((Object *)ob)->set_script(script);
547-
return ob;
549+
PropertyUtils::assign_custom_type_script(ob, script);
550+
ob->set_script(script);
551+
return v;
548552
}
549553
}
550554
}
@@ -988,12 +992,13 @@ Variant EditorData::script_class_instance(const String &p_class) {
988992
Ref<Script> script = script_class_load_script(p_class);
989993
if (script.is_valid()) {
990994
// Store in a variant to initialize the refcount if needed.
991-
Variant obj = ClassDB::instantiate(script->get_instance_base_type());
992-
if (obj) {
993-
Object::cast_to<Object>(obj)->set_meta(SceneStringName(_custom_type_script), script);
994-
obj.operator Object *()->set_script(script);
995+
Variant v = ClassDB::instantiate(script->get_instance_base_type());
996+
if (v) {
997+
Object *obj = v;
998+
PropertyUtils::assign_custom_type_script(obj, script);
999+
obj->set_script(script);
9951000
}
996-
return obj;
1001+
return v;
9971002
}
9981003
}
9991004
return Variant();

editor/editor_node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4751,7 +4751,7 @@ Ref<Script> EditorNode::get_object_custom_type_base(const Object *p_object) cons
47514751

47524752
const Node *node = Object::cast_to<const Node>(p_object);
47534753
if (node && node->has_meta(SceneStringName(_custom_type_script))) {
4754-
return node->get_meta(SceneStringName(_custom_type_script));
4754+
return PropertyUtils::get_custom_type_script(node);
47554755
}
47564756

47574757
Ref<Script> scr = p_object->get_script();

editor/scene_tree_dock.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,7 +2834,7 @@ void SceneTreeDock::_update_script_button() {
28342834
Ref<Script> cts;
28352835

28362836
if (n->has_meta(SceneStringName(_custom_type_script))) {
2837-
cts = n->get_meta(SceneStringName(_custom_type_script));
2837+
cts = PropertyUtils::get_custom_type_script(n);
28382838
}
28392839

28402840
if (selection.size() == 1) {
@@ -3114,7 +3114,7 @@ void SceneTreeDock::_replace_node(Node *p_node, Node *p_by_node, bool p_keep_pro
31143114

31153115
// If we're dealing with a custom node type, we need to create a default instance of the custom type instead of the native type for property comparison.
31163116
if (oldnode->has_meta(SceneStringName(_custom_type_script))) {
3117-
Ref<Script> cts = oldnode->get_meta(SceneStringName(_custom_type_script));
3117+
Ref<Script> cts = PropertyUtils::get_custom_type_script(oldnode);
31183118
default_oldnode = Object::cast_to<Node>(get_editor_data()->script_class_instance(cts->get_global_name()));
31193119
if (default_oldnode) {
31203120
default_oldnode->set_name(cts->get_global_name());
@@ -3618,7 +3618,7 @@ void SceneTreeDock::_script_dropped(const String &p_file, NodePath p_to) {
36183618
} else {
36193619
// Check if dropped script is compatible.
36203620
if (n->has_meta(SceneStringName(_custom_type_script))) {
3621-
Ref<Script> ct_scr = n->get_meta(SceneStringName(_custom_type_script));
3621+
Ref<Script> ct_scr = PropertyUtils::get_custom_type_script(n);
36223622
if (!scr->inherits_script(ct_scr)) {
36233623
String custom_type_name = ct_scr->get_global_name();
36243624

scene/property_utils.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Variant PropertyUtils::get_property_default_value(const Object *p_object, const
9292
// Handle special case "script" property, where the default value is either null or the custom type script.
9393
// Do this only if there's no states stack cache to trace for default values.
9494
if (!p_states_stack_cache && p_property == CoreStringName(script) && p_object->has_meta(SceneStringName(_custom_type_script))) {
95-
Ref<Script> ct_scr = p_object->get_meta(SceneStringName(_custom_type_script));
95+
Ref<Script> ct_scr = get_custom_type_script(p_object);
9696
if (r_is_valid) {
9797
*r_is_valid = true;
9898
}
@@ -282,3 +282,29 @@ Vector<SceneState::PackState> PropertyUtils::get_node_states_stack(const Node *p
282282
}
283283
return states_stack_ret;
284284
}
285+
286+
void PropertyUtils::assign_custom_type_script(Object *p_object, const Ref<Script> &p_script) {
287+
ERR_FAIL_NULL(p_object);
288+
ERR_FAIL_COND(p_script.is_null());
289+
290+
const String &path = p_script->get_path();
291+
ERR_FAIL_COND(!path.is_resource_file());
292+
293+
ResourceUID::ID script_uid = ResourceLoader::get_resource_uid(path);
294+
if (script_uid != ResourceUID::INVALID_ID) {
295+
p_object->set_meta(SceneStringName(_custom_type_script), ResourceUID::get_singleton()->id_to_text(script_uid));
296+
}
297+
}
298+
299+
Ref<Script> PropertyUtils::get_custom_type_script(const Object *p_object) {
300+
Variant custom_script = p_object->get_meta(SceneStringName(_custom_type_script));
301+
#ifndef DISABLE_DEPRECATED
302+
if (custom_script.get_type() == Variant::OBJECT) {
303+
// Convert old script meta.
304+
Ref<Script> script_object(custom_script);
305+
assign_custom_type_script(const_cast<Object *>(p_object), script_object);
306+
return script_object;
307+
}
308+
#endif
309+
return ResourceLoader::load(custom_script);
310+
}

scene/property_utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class PropertyUtils {
4646
// in the tree, since every owner found while traversing towards the root gets a chance
4747
// to override property values.)
4848
static Vector<SceneState::PackState> get_node_states_stack(const Node *p_node, const Node *p_owner = nullptr, bool *r_instantiated_by_owner = nullptr);
49+
50+
static void assign_custom_type_script(Object *p_object, const Ref<Script> &p_script);
51+
static Ref<Script> get_custom_type_script(const Object *p_object);
4952
};
5053

5154
#endif // PROPERTY_UTILS_H

0 commit comments

Comments
 (0)