From 00d5da2f09245d28d0a0f495e7448301c20cc101 Mon Sep 17 00:00:00 2001 From: Rindbee Date: Sat, 8 Jul 2023 19:38:27 +0800 Subject: [PATCH] Clear the previously set state when configuring for a new scene root node Saving a subscene causes the main scene to be re-instantiated. And the resource instance in the main scene will be reused when the main scene is re-instantiated. So for resources with `resource_local_to_scene` enabled, resetting state may be necessary (at least for `ViewportTexture`). (cherry picked from commit 4795c3cdfa5cebaaee6c5ca0ea070d0e7c4305e4) --- core/io/resource.cpp | 5 +++++ core/io/resource.h | 2 ++ scene/main/viewport.cpp | 17 +++++++++++------ scene/main/viewport.h | 2 ++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/core/io/resource.cpp b/core/io/resource.cpp index 6b8ec8d5f66..059bae32715 100644 --- a/core/io/resource.cpp +++ b/core/io/resource.cpp @@ -226,6 +226,7 @@ void Resource::configure_for_local_scene(Node *p_for_scene, HashMap plist; get_property_list(&plist); + reset_local_to_scene(); local_scene = p_for_scene; for (const PropertyInfo &E : plist) { @@ -386,6 +387,10 @@ void Resource::setup_local_to_scene() { emit_signal(SNAME("setup_local_to_scene_requested")); } +void Resource::reset_local_to_scene() { + // Restores the state as if setup_local_to_scene() hadn't been called. +} + Node *(*Resource::_get_local_scene_func)() = nullptr; void (*Resource::_update_configuration_warning)() = nullptr; diff --git a/core/io/resource.h b/core/io/resource.h index 5135664f363..0d9ad307764 100644 --- a/core/io/resource.h +++ b/core/io/resource.h @@ -86,6 +86,8 @@ protected: void _set_path(const String &p_path); void _take_over_path(const String &p_path); + virtual void reset_local_to_scene(); + public: static Node *(*_get_local_scene_func)(); //used by editor static void (*_update_configuration_warning)(); //used by editor diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index 7a303198771..8f6986a39ab 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -88,12 +88,7 @@ void ViewportTexture::setup_local_to_scene() { } } -void ViewportTexture::set_viewport_path_in_scene(const NodePath &p_path) { - if (path == p_path) { - return; - } - - path = p_path; +void ViewportTexture::reset_local_to_scene() { vp_changed = true; if (vp) { @@ -105,6 +100,16 @@ void ViewportTexture::set_viewport_path_in_scene(const NodePath &p_path) { proxy_ph = RS::get_singleton()->texture_2d_placeholder_create(); RS::get_singleton()->texture_proxy_update(proxy, proxy_ph); } +} + +void ViewportTexture::set_viewport_path_in_scene(const NodePath &p_path) { + if (path == p_path) { + return; + } + + path = p_path; + + reset_local_to_scene(); if (get_local_scene() && !path.is_empty()) { setup_local_to_scene(); diff --git a/scene/main/viewport.h b/scene/main/viewport.h index 0e72d09609b..5192e7c72a4 100644 --- a/scene/main/viewport.h +++ b/scene/main/viewport.h @@ -70,6 +70,8 @@ class ViewportTexture : public Texture2D { protected: static void _bind_methods(); + virtual void reset_local_to_scene() override; + public: void set_viewport_path_in_scene(const NodePath &p_path); NodePath get_viewport_path_in_scene() const;