Bladeren bron

Merge pull request #90432 from Rindbee/correctly-replace-scene-root-when-must_reload

Correctly replace scene root when `must_reload` in `EditorData::check_and_update_scene()`
Rémi Verschelde 1 jaar geleden
bovenliggende
commit
9c5e968bbc

+ 1 - 2
doc/classes/Node.xml

@@ -795,9 +795,8 @@
 			<return type="void" />
 			<param index="0" name="node" type="Node" />
 			<param index="1" name="keep_groups" type="bool" default="false" />
-			<param index="2" name="keep_children" type="bool" default="true" />
 			<description>
-				Replaces this node by the given [param node]. If [param keep_children] is [code]true[/code] all children of this node are moved to [param node].
+				Replaces this node by the given [param node]. All children of this node are moved to [param node].
 				If [param keep_groups] is [code]true[/code], the [param node] is added to the same groups that the replaced node is in (see [method add_to_group]).
 				[b]Warning:[/b] The replaced node is removed from the tree, but it is [b]not[/b] deleted. To prevent memory leaks, store a reference to the node in a variable, or use [method Object.free].
 			</description>

+ 1 - 2
editor/editor_data.cpp

@@ -722,8 +722,7 @@ bool EditorData::check_and_update_scene(int p_idx) {
 
 		new_scene->set_scene_file_path(edited_scene[p_idx].root->get_scene_file_path());
 		Node *old_root = edited_scene[p_idx].root;
-		edited_scene.write[p_idx].root = new_scene;
-		old_root->replace_by(new_scene, false, false);
+		EditorNode::get_singleton()->set_edited_scene(new_scene);
 		memdelete(old_root);
 		edited_scene.write[p_idx].selection = new_selection;
 

+ 1 - 3
editor/editor_node.cpp

@@ -3670,9 +3670,7 @@ void EditorNode::set_edited_scene(Node *p_scene) {
 		if (old_edited_scene_root->get_parent() == scene_root) {
 			scene_root->remove_child(old_edited_scene_root);
 		}
-		if (old_edited_scene_root->is_connected("replacing_by", callable_mp(this, &EditorNode::set_edited_scene))) {
-			old_edited_scene_root->disconnect(SNAME("replacing_by"), callable_mp(this, &EditorNode::set_edited_scene));
-		}
+		old_edited_scene_root->disconnect(SNAME("replacing_by"), callable_mp(this, &EditorNode::set_edited_scene));
 	}
 	get_editor_data().set_edited_scene_root(p_scene);
 

+ 0 - 7
misc/extension_api_validation/4.2-stable.expected

@@ -241,13 +241,6 @@ Validate extension JSON: Error: Field 'classes/AcceptDialog/methods/remove_butto
 Changed argument type to the more specific one actually expected by the method. Compatibility method registered.
 
 
-GH-89992
---------
-Validate extension JSON: Error: Field 'classes/Node/methods/replace_by/arguments': size changed value in new API, from 2 to 3.
-
-Added optional argument to prevent children to be reparented during replace_by. Compatibility method registered.
-
-
 GH-88047
 --------
 Validate extension JSON: Error: Field 'classes/AStar2D/methods/get_id_path/arguments': size changed value in new API, from 2 to 3.

+ 0 - 41
scene/main/node.compat.inc

@@ -1,41 +0,0 @@
-/**************************************************************************/
-/*  node.compat.inc                                                       */
-/**************************************************************************/
-/*                         This file is part of:                          */
-/*                             GODOT ENGINE                               */
-/*                        https://godotengine.org                         */
-/**************************************************************************/
-/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
-/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
-/*                                                                        */
-/* Permission is hereby granted, free of charge, to any person obtaining  */
-/* a copy of this software and associated documentation files (the        */
-/* "Software"), to deal in the Software without restriction, including    */
-/* without limitation the rights to use, copy, modify, merge, publish,    */
-/* distribute, sublicense, and/or sell copies of the Software, and to     */
-/* permit persons to whom the Software is furnished to do so, subject to  */
-/* the following conditions:                                              */
-/*                                                                        */
-/* The above copyright notice and this permission notice shall be         */
-/* included in all copies or substantial portions of the Software.        */
-/*                                                                        */
-/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
-/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
-/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
-/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
-/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
-/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
-/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
-/**************************************************************************/
-
-#ifndef DISABLE_DEPRECATED
-
-void Node::_replace_by_bind_compat_89992(Node *p_node, bool p_keep_data) {
-	replace_by(p_node, p_keep_data, true);
-}
-
-void Node::_bind_compatibility_methods() {
-	ClassDB::bind_compatibility_method(D_METHOD("replace_by", "node", "keep_groups"), &Node::_replace_by_bind_compat_89992, DEFVAL(false));
-}
-
-#endif

+ 24 - 27
scene/main/node.cpp

@@ -29,7 +29,6 @@
 /**************************************************************************/
 
 #include "node.h"
-#include "node.compat.inc"
 
 #include "core/config/project_settings.h"
 #include "core/core_string_names.h"
@@ -3006,7 +3005,7 @@ static void find_owned_by(Node *p_by, Node *p_node, List<Node *> *p_owned) {
 	}
 }
 
-void Node::replace_by(Node *p_node, bool p_keep_groups, bool p_keep_children) {
+void Node::replace_by(Node *p_node, bool p_keep_groups) {
 	ERR_THREAD_GUARD
 	ERR_FAIL_NULL(p_node);
 	ERR_FAIL_COND(p_node->data.parent);
@@ -3027,13 +3026,13 @@ void Node::replace_by(Node *p_node, bool p_keep_groups, bool p_keep_children) {
 	_replace_connections_target(p_node);
 
 	if (data.owner) {
-		if (p_keep_children) {
-			for (int i = 0; i < get_child_count(); i++) {
-				find_owned_by(data.owner, get_child(i), &owned_by_owner);
-			}
+		for (int i = 0; i < get_child_count(); i++) {
+			find_owned_by(data.owner, get_child(i), &owned_by_owner);
 		}
+
 		_clean_up_owner();
 	}
+
 	Node *parent = data.parent;
 	int index_in_parent = get_index(false);
 
@@ -3045,33 +3044,31 @@ void Node::replace_by(Node *p_node, bool p_keep_groups, bool p_keep_children) {
 
 	emit_signal(SNAME("replacing_by"), p_node);
 
-	if (p_keep_children) {
-		while (get_child_count()) {
-			Node *child = get_child(0);
-			remove_child(child);
-			if (!child->is_owned_by_parent()) {
-				// add the custom children to the p_node
-				Node *child_owner = child->get_owner() == this ? p_node : child->get_owner();
-				child->set_owner(nullptr);
-				p_node->add_child(child);
-				child->set_owner(child_owner);
-			}
+	while (get_child_count()) {
+		Node *child = get_child(0);
+		remove_child(child);
+		if (!child->is_owned_by_parent()) {
+			// add the custom children to the p_node
+			Node *child_owner = child->get_owner() == this ? p_node : child->get_owner();
+			child->set_owner(nullptr);
+			p_node->add_child(child);
+			child->set_owner(child_owner);
 		}
+	}
 
-		for (Node *E : owned) {
-			if (E->data.owner != p_node) {
-				E->set_owner(p_node);
-			}
+	p_node->set_owner(owner);
+	for (Node *E : owned) {
+		if (E->data.owner != p_node) {
+			E->set_owner(p_node);
 		}
+	}
 
-		for (Node *E : owned_by_owner) {
-			if (E->data.owner != owner) {
-				E->set_owner(owner);
-			}
+	for (Node *E : owned_by_owner) {
+		if (E->data.owner != owner) {
+			E->set_owner(owner);
 		}
 	}
 
-	p_node->set_owner(owner);
 	p_node->set_scene_file_path(get_scene_file_path());
 }
 
@@ -3598,7 +3595,7 @@ void Node::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("create_tween"), &Node::create_tween);
 
 	ClassDB::bind_method(D_METHOD("duplicate", "flags"), &Node::duplicate, DEFVAL(DUPLICATE_USE_INSTANTIATION | DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS));
-	ClassDB::bind_method(D_METHOD("replace_by", "node", "keep_groups", "keep_children"), &Node::replace_by, DEFVAL(false), DEFVAL(true));
+	ClassDB::bind_method(D_METHOD("replace_by", "node", "keep_groups"), &Node::replace_by, DEFVAL(false));
 
 	ClassDB::bind_method(D_METHOD("set_scene_instance_load_placeholder", "load_placeholder"), &Node::set_scene_instance_load_placeholder);
 	ClassDB::bind_method(D_METHOD("get_scene_instance_load_placeholder"), &Node::get_scene_instance_load_placeholder);

+ 1 - 6
scene/main/node.h

@@ -310,11 +310,6 @@ private:
 	Variant _call_thread_safe_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
 
 protected:
-#ifndef DISABLE_DEPRECATED
-	void _replace_by_bind_compat_89992(Node *p_node, bool p_keep_data = false);
-	static void _bind_compatibility_methods();
-#endif // DISABLE_DEPRECATED
-
 	void _block() { data.blocked++; }
 	void _unblock() { data.blocked--; }
 
@@ -634,7 +629,7 @@ public:
 		return binds;
 	}
 
-	void replace_by(Node *p_node, bool p_keep_groups = false, bool p_keep_children = true);
+	void replace_by(Node *p_node, bool p_keep_data = false);
 
 	void set_process_mode(ProcessMode p_mode);
 	ProcessMode get_process_mode() const;