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

Code before:
				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_undo_method(new_node, "add_child", child, true);
					ur->add_undo_method(child, "set_owner", new_node);
					ur->add_undo_reference(child);
				}
			}
		}

		Vector2 selected_pos;
		if (selected->has_method("get_position")) {
			selected_pos = selected->call("get_position");
		}
		_replace_node(selected, new_node, false);
		new_node->set_scene_file_path(new_scene_file_path);

		if (new_node->has_method("set_position")) {
			new_node->call("set_position", selected_pos);
		}

		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) {

Review comment (suggestion):
```suggestion
		Variant selected_pos;
```
Handles both 2D and 3D nodes.

Code after:
		bool old_is_scene = !old_scene_file_path.is_empty();
		bool new_is_scene = !new_scene_file_path.is_empty();

		LocalVector<Node *> old_children;

		ur->create_action(TTR("Paste Node(s) as Replacement"), UndoRedo::MERGE_ALL, 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_children.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_undo_method(new_node, "add_child", child, true);
					ur->add_undo_method(child, "set_owner", new_node);
					ur->add_undo_reference(child);
				}
			}
		}

		Variant selected_pos;
		if (selected->has_method("get_position")) {
			selected_pos = selected->call("get_position");
		}
		_replace_node(selected, new_node, false);
		new_node->set_scene_file_path(new_scene_file_path);

		if (new_node->has_method("set_position")) {
			new_node->call("set_position", selected_pos);
		}

		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);
				}