diff options
author | Ryan <73148864+Ryan-000@users.noreply.github.com> | 2024-02-08 13:26:55 -0500 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2024-03-07 14:53:58 +0100 |
commit | 42ce14043e3ee9290acf5f57f712071cdc3a3e5b (patch) | |
tree | c86f40b61d6c014bd3f5911e61fa8794fe1b353c | |
parent | 24f775089efd1c3e7419a523dd74dc24924b517c (diff) | |
download | redot-engine-42ce14043e3ee9290acf5f57f712071cdc3a3e5b.tar.gz |
Expose scene unique id functionality in Resource
-rw-r--r-- | core/io/resource.cpp | 15 | ||||
-rw-r--r-- | doc/classes/Resource.xml | 12 | ||||
-rw-r--r-- | editor/doc_tools.cpp | 2 |
3 files changed, 28 insertions, 1 deletions
diff --git a/core/io/resource.cpp b/core/io/resource.cpp index 7e8d0b43cd..6d32035d25 100644 --- a/core/io/resource.cpp +++ b/core/io/resource.cpp @@ -130,6 +130,16 @@ String Resource::generate_scene_unique_id() { } void Resource::set_scene_unique_id(const String &p_id) { + bool is_valid = true; + for (int i = 0; i < p_id.length(); i++) { + if (!is_ascii_identifier_char(p_id[i])) { + is_valid = false; + scene_unique_id = Resource::generate_scene_unique_id(); + break; + } + } + + ERR_FAIL_COND_MSG(!is_valid, "The scene unique ID must contain only letters, numbers, and underscores."); scene_unique_id = p_id; } @@ -532,6 +542,10 @@ void Resource::_bind_methods() { ClassDB::bind_method(D_METHOD("get_local_scene"), &Resource::get_local_scene); ClassDB::bind_method(D_METHOD("setup_local_to_scene"), &Resource::setup_local_to_scene); + ClassDB::bind_static_method("Resource", D_METHOD("generate_scene_unique_id"), &Resource::generate_scene_unique_id); + ClassDB::bind_method(D_METHOD("set_scene_unique_id", "id"), &Resource::set_scene_unique_id); + ClassDB::bind_method(D_METHOD("get_scene_unique_id"), &Resource::get_scene_unique_id); + ClassDB::bind_method(D_METHOD("emit_changed"), &Resource::emit_changed); ClassDB::bind_method(D_METHOD("duplicate", "subresources"), &Resource::duplicate, DEFVAL(false)); @@ -542,6 +556,7 @@ void Resource::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resource_local_to_scene"), "set_local_to_scene", "is_local_to_scene"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_path", "get_path"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_name"), "set_name", "get_name"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_scene_unique_id", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_scene_unique_id", "get_scene_unique_id"); MethodInfo get_rid_bind("_get_rid"); get_rid_bind.return_val.type = Variant::RID; diff --git a/doc/classes/Resource.xml b/doc/classes/Resource.xml index 85372288e7..cec936ac3e 100644 --- a/doc/classes/Resource.xml +++ b/doc/classes/Resource.xml @@ -59,6 +59,12 @@ [/codeblock] </description> </method> + <method name="generate_scene_unique_id" qualifiers="static"> + <return type="String" /> + <description> + Generates a unique identifier for a resource to be contained inside a [PackedScene], based on the current date, time, and a random value. The returned string is only composed of letters ([code]a[/code] to [code]y[/code]) and numbers ([code]0[/code] to [code]8[/code]). See also [member resource_scene_unique_id]. + </description> + </method> <method name="get_local_scene" qualifiers="const"> <return type="Node" /> <description> @@ -98,6 +104,12 @@ The unique path to this resource. If it has been saved to disk, the value will be its filepath. If the resource is exclusively contained within a scene, the value will be the [PackedScene]'s filepath, followed by a unique identifier. [b]Note:[/b] Setting this property manually may fail if a resource with the same path has already been previously loaded. If necessary, use [method take_over_path]. </member> + <member name="resource_scene_unique_id" type="String" setter="set_scene_unique_id" getter="get_scene_unique_id"> + An unique identifier relative to the this resource's scene. If left empty, the ID is automatically generated when this resource is saved inside a [PackedScene]. If the resource is not inside a scene, this property is empty by default. + [b]Note:[/b] When the [PackedScene] is saved, if multiple resources in the same scene use the same ID, only the earliest resource in the scene hierarchy keeps the original ID. The other resources are assigned new IDs from [method generate_scene_unique_id]. + [b]Note:[/b] Setting this property does not emit the [signal changed] signal. + [b]Warning:[/b] When setting, the ID must only consist of letters, numbers, and underscores. Otherwise, it will fail and default to a randomly generated ID. + </member> </members> <signals> <signal name="changed"> diff --git a/editor/doc_tools.cpp b/editor/doc_tools.cpp index ee3e54e9df..52b0d485cf 100644 --- a/editor/doc_tools.cpp +++ b/editor/doc_tools.cpp @@ -477,7 +477,7 @@ void DocTools::generate(BitField<GenerateFlags> p_flags) { } if (properties_from_instance) { - if (E.name == "resource_local_to_scene" || E.name == "resource_name" || E.name == "resource_path" || E.name == "script") { + if (E.name == "resource_local_to_scene" || E.name == "resource_name" || E.name == "resource_path" || E.name == "script" || E.name == "resource_scene_unique_id") { // Don't include spurious properties from Object property list. continue; } |