Repository: godotengine/godot
File: editor/docks/scene_tree_dock.cpp (C++)

Code before:

				LocalVector<Node *> old_childs;

				ur->create_action(TTR("Replace Node"), UndoRedo::MERGE_DISABLE, selected);

				if (old_is_scene) {
					for (int i = 0; i < selected->get_child_count(); i++) {
						Node *child = selected->get_child(i);
						if (child->get_owner() == selected) {
							ur->add_do_method(selected, "remove_child", child);

							selected->remove_child(child);
							old_childs.push_back(child);
						}
					}
				}

				ur->add_do_method(this, "replace_node", selected, new_node, false);
				ur->add_do_method(new_node, "set_scene_file_path", new_scene_file_path);
				ur->add_do_reference(new_node);

				if (new_is_scene) {
					for (int i = 0; i < new_node->get_child_count(); i++) {
						Node *child = new_node->get_child(i);
						if (child->get_owner() == new_node) {
							ur->add_do_method(new_node, "add_child", child);
							ur->add_undo_method(child, "set_owner", new_node);
							ur->add_do_reference(child);
						}
					}
				}

				_replace_node(selected, new_node, false);
				new_node->set_scene_file_path(new_scene_file_path);

				if (new_is_scene) {
					for (int i = 0; i < new_node->get_child_count(); i++) {
						Node *child = new_node->get_child(i);
						if (child->get_owner() == new_node) {
							ur->add_undo_method(new_node, "remove_child", child);
						}
					}
				}

				ur->add_undo_method(this, "replace_node", new_node, selected, false);
				ur->add_undo_method(selected, "set_scene_file_path", old_scene_file_path);
				ur->add_undo_reference(selected);

				if (old_is_scene) {
					for (Node *child : old_childs) {
						ur->add_undo_method(selected, "add_child", child);

Review comment (suggestion):
```suggestion
							ur->add_undo_method(new_node, "add_child", child);
```

Code after:

			if (!_validate_no_foreign()) {
				break;
			}

			if (!_validate_no_instance()) {
				break;
			}

			if (reset_create_dialog) {
				create_dialog->set_base_type("Node");
				reset_create_dialog = false;
			}

			Node *selected = scene_tree->get_selected();
			const List<Node *> &top_node_list = editor_selection->get_top_selected_node_list();
			if (!selected && !top_node_list.is_empty()) {
				selected = top_node_list.front()->get();
			}

			if (selected) {
				create_dialog->popup_create(false, true, selected->get_class(), selected->get_name());
			}
		} break;
		case TOOL_EXTEND_SCRIPT: {
			attach_script_to_selected(true);
		} break;
		case TOOL_ATTACH_SCRIPT: {
			attach_script_to_selected(false);
		} break;
		case TOOL_DETACH_SCRIPT: {
			if (!profile_allow_script_editing) {
				break;
			}

			Array selection = editor_selection->get_selected_nodes();

			if (selection.is_empty()) {
				return;
			}

			EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
			undo_redo->create_action(TTR("Detach Script"), UndoRedo::MERGE_DISABLE, EditorNode::get_singleton()->get_edited_scene());

			for (int i = 0; i < selection.size(); i++) {
				Node *n = Object::cast_to<Node>(selection[i]);
				Ref<Script> existing = n->get_script();
				Ref<Script> empty = EditorNode::get_singleton()->get_object_custom_type_base(n);
				if (existing != empty) {
					undo_redo->add_do_method(n, "set_script", empty);
					undo_redo->add_undo_method(n, "set_script", existing);