Skip to content

Commit da02a46

Browse files
committed
Sync reparent
1 parent 98c7c2f commit da02a46

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

editor/scene_tree_dock.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,6 +2352,12 @@ void SceneTreeDock::_do_reparent(Node *p_new_parent, int p_position_in_parent, V
23522352
return; // Nothing to reparent.
23532353
}
23542354

2355+
Array paths;
2356+
for (Node *E : p_nodes) {
2357+
paths.append(E);
2358+
}
2359+
emit_signal("nodes_reparented", paths, scene_root->get_path_to(p_new_parent));
2360+
23552361
p_nodes.sort_custom<Node::Comparator>(); //Makes result reliable.
23562362

23572363
const int first_idx = p_position_in_parent == -1 ? p_new_parent->get_child_count(false) : p_position_in_parent;
@@ -4636,6 +4642,7 @@ void SceneTreeDock::_bind_methods() {
46364642
ADD_SIGNAL(MethodInfo("remote_tree_selected"));
46374643
ADD_SIGNAL(MethodInfo("add_node_used"));
46384644
ADD_SIGNAL(MethodInfo("node_created", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
4645+
ADD_SIGNAL(MethodInfo("nodes_reparented"), PropertyInfo(Variant::ARRAY, "nodes"), PropertyInfo(Variant::NODE_PATH, "new_parent"));
46394646
}
46404647

46414648
SceneTreeDock *SceneTreeDock::singleton = nullptr;

modules/multi_godot/multi_godot.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "editor/editor_file_system.h"
1111
#include "editor/editor_interface.h"
1212
#include "editor/editor_main_screen.h"
13+
#include "editor/editor_undo_redo_manager.h"
1314
#include "editor/gui/scene_tree_editor.h"
1415
#include "editor/plugins/script_editor_plugin.h"
1516
#include "editor/scene_tree_dock.h"
@@ -32,6 +33,7 @@ void MultiGodot::_bind_methods() {
3233
ClassDB::bind_method(D_METHOD("_on_lobby_chat_update", "this_lobby_id", "change_id", "making_change_id", "chat_state"), &MultiGodot::_on_lobby_chat_update);
3334
ClassDB::bind_method(D_METHOD("_on_p2p_session_request", "remote_id"), &MultiGodot::_on_p2p_session_request);
3435
ClassDB::bind_method(D_METHOD("_on_p2p_session_connect_fail", "steam_id", "session_error"), &MultiGodot::_on_p2p_session_connect_fail);
36+
ClassDB::bind_method(D_METHOD("_on_nodes_reparented", "nodes", "new_parent"), &MultiGodot::_on_nodes_reparented);
3537

3638
// Remote Callables
3739

@@ -49,7 +51,7 @@ void MultiGodot::_bind_methods() {
4951
ClassDB::bind_method(D_METHOD("_set_as_script_owner", "path"), &MultiGodot::_set_as_script_owner);
5052
ClassDB::bind_method(D_METHOD("_apply_action"), &MultiGodot::_apply_action);
5153
ClassDB::bind_method(D_METHOD("_instantiate_resource", "node_path", "resource_path", "type"), &MultiGodot::_instantiate_resource);
52-
ClassDB::bind_method(D_METHOD("_move_node", "old", "new"), &MultiGodot::_move_node);
54+
ClassDB::bind_method(D_METHOD("_reparent_nodes", "old", "new"), &MultiGodot::_reparent_nodes);
5355

5456
// Button signals
5557

@@ -116,6 +118,7 @@ void MultiGodot::_ready() {
116118

117119
filesystem_scanner.start(_threaded_filesystem_scanner, this);
118120

121+
SceneTreeDock::get_singleton()->connect("nodes_reparented", Callable(this, "_on_nodes_reparented"));
119122
button_notifier->connect("editor_tab_changed", Callable(this, "_on_editor_tab_changed"));
120123
button_notifier->connect("current_script_path_changed", Callable(this, "_on_current_script_path_changed"));
121124
steam->connect("lobby_created", Callable(this, "_on_lobby_created"));
@@ -1109,25 +1112,25 @@ void MultiGodot::_instantiate_resource(String node_path, String resource_path, S
11091112

11101113
}
11111114

1112-
void MultiGodot::_move_node(String current_path, String new_parent_path) {
1115+
void MultiGodot::_reparent_nodes(Array paths, String new_parent_path) {
11131116
SceneTreeEditor *scene_tree_editor = SceneTreeDock::get_singleton()->get_tree_editor();
11141117
Node *root = EditorNode::get_singleton()->get_edited_scene();
11151118

1116-
Node *moved = root->get_node(current_path);
1117-
if (!moved) {
1118-
print_error("Request to move node but the node at path " + current_path + " does not exist. Was it moved?");
1119-
return;
1120-
}
1121-
11221119
Node *parent = root->get_node(new_parent_path);
11231120
if (!parent) {
1124-
print_error("Request to move a node (that exists) to a parent that doesn't exist at path " + new_parent_path + ". No parents?");
1121+
print_error("Remote requested to reparent some nodes to a parent at path " + new_parent_path + " but it doesn't exist.");
11251122
return;
11261123
}
11271124

1128-
// Remove the child and give it to new parents.
1129-
moved->get_parent()->remove_child(moved);
1130-
parent->add_child(moved);
1125+
for (int i = 0; i < paths.size(); i++) {
1126+
String path = paths[i];
1127+
Node *node = root->get_node(path);
1128+
if (!node) {
1129+
print_error("Remote requested to reparent a node at path " + path + " but it doesn't exist.");
1130+
continue;
1131+
}
1132+
node->reparent(parent);
1133+
}
11311134
}
11321135

11331136
// SIGNALS
@@ -1286,6 +1289,15 @@ void MultiGodot::_on_current_script_path_changed(String path) {
12861289
_set_user_data_for_everyone("current_spectating_script", "");
12871290
}
12881291

1292+
void MultiGodot::_on_nodes_reparented(Array nodes, NodePath new_parent) {
1293+
if (recently_reparented_by_remote.has(new_parent)) {
1294+
recently_reparented_by_remote.remove_at(recently_reparented_by_remote.find(new_parent));
1295+
return;
1296+
}
1297+
1298+
_call_func(this, "_reparent_nodes", {nodes, new_parent});
1299+
}
1300+
12891301
// PLUGIN
12901302

12911303
MultiGodotPlugin::MultiGodotPlugin() {

modules/multi_godot/multi_godot.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class MultiGodot : public Node2D {
8080
Vector<String> new_files;
8181
Vector<String> deleted_files; // Both this and new_files will fill up if not cleaned up by the main thread.
8282
Vector<String> previous_property_names;
83+
Vector<String> recently_reparented_by_remote;
8384
Vector<Variant> previous_property_values;
8485
Vector<uint64_t> steam_ids;
8586
Vector<Action> undo_stack;
@@ -146,7 +147,7 @@ class MultiGodot : public Node2D {
146147
void _set_as_script_owner(String path);
147148
void _apply_action(int type, String node_path, String new_path, String new_name, String property_path, Variant value);
148149
void _instantiate_resource(String node_path, String resource_path, String type);
149-
void _move_node(String current_path, String new_path);
150+
void _reparent_nodes(Array paths, String new_path);
150151

151152
// SIGNALS
152153

@@ -158,6 +159,7 @@ class MultiGodot : public Node2D {
158159
void _on_p2p_session_connect_fail(uint64_t this_steam_id, int session_error);
159160
void _on_editor_tab_changed(int index);
160161
void _on_current_script_path_changed(String path);
162+
void _on_nodes_reparented(Array nodes, NodePath new_parent);
161163

162164
public:
163165
MultiGodot();

0 commit comments

Comments
 (0)