From 12474fd87ae5de819eee58d0d0bf0b9e2c521cfb Mon Sep 17 00:00:00 2001 From: reduz Date: Mon, 23 May 2022 02:24:14 +0200 Subject: [PATCH] Improve MultiplayerSynchronizer editor usability * Add a button to add properties (which lets you select node and property) * Add ability to drag properties and drop them to the editor. * Made the editor transient (not always visible on the bottom) since its not needed most of the time. * Added the ability to pin the editor, in case dragging properties from other nodes is desired. --- doc/classes/MultiplayerSpawner.xml | 24 +- doc/classes/MultiplayerSynchronizer.xml | 6 +- doc/classes/SceneReplicationConfig.xml | 6 + editor/plugins/replication_editor_plugin.cpp | 255 +++++++++++++++++- editor/plugins/replication_editor_plugin.h | 32 ++- scene/multiplayer/multiplayer_spawner.cpp | 128 +++++++-- scene/multiplayer/multiplayer_spawner.h | 29 +- .../multiplayer/multiplayer_synchronizer.cpp | 2 +- scene/multiplayer/multiplayer_synchronizer.h | 2 +- .../scene_replication_interface.cpp | 2 +- scene/resources/scene_replication_config.cpp | 10 + scene/resources/scene_replication_config.h | 1 + 12 files changed, 456 insertions(+), 41 deletions(-) diff --git a/doc/classes/MultiplayerSpawner.xml b/doc/classes/MultiplayerSpawner.xml index 465db854554..4ca92728ff2 100644 --- a/doc/classes/MultiplayerSpawner.xml +++ b/doc/classes/MultiplayerSpawner.xml @@ -13,6 +13,28 @@ + + + + + + + + + + + + + + + + + + + + + + @@ -23,8 +45,6 @@ - - diff --git a/doc/classes/MultiplayerSynchronizer.xml b/doc/classes/MultiplayerSynchronizer.xml index e1f09483466..43355481b68 100644 --- a/doc/classes/MultiplayerSynchronizer.xml +++ b/doc/classes/MultiplayerSynchronizer.xml @@ -7,11 +7,11 @@ + + - - - + diff --git a/doc/classes/SceneReplicationConfig.xml b/doc/classes/SceneReplicationConfig.xml index aade8ac3beb..62c108a4770 100644 --- a/doc/classes/SceneReplicationConfig.xml +++ b/doc/classes/SceneReplicationConfig.xml @@ -19,6 +19,12 @@ + + + + + + diff --git a/editor/plugins/replication_editor_plugin.cpp b/editor/plugins/replication_editor_plugin.cpp index 2a7b3c7a556..6992b5443ba 100644 --- a/editor/plugins/replication_editor_plugin.cpp +++ b/editor/plugins/replication_editor_plugin.cpp @@ -37,6 +37,129 @@ #include "scene/gui/tree.h" #include "scene/multiplayer/multiplayer_synchronizer.h" +void ReplicationEditor::_pick_node_filter_text_changed(const String &p_newtext) { + TreeItem *root_item = pick_node->get_scene_tree()->get_scene_tree()->get_root(); + + Vector select_candidates; + Node *to_select = nullptr; + + String filter = pick_node->get_filter_line_edit()->get_text(); + + _pick_node_select_recursive(root_item, filter, select_candidates); + + if (!select_candidates.is_empty()) { + for (int i = 0; i < select_candidates.size(); ++i) { + Node *candidate = select_candidates[i]; + + if (((String)candidate->get_name()).to_lower().begins_with(filter.to_lower())) { + to_select = candidate; + break; + } + } + + if (!to_select) { + to_select = select_candidates[0]; + } + } + + pick_node->get_scene_tree()->set_selected(to_select); +} + +void ReplicationEditor::_pick_node_select_recursive(TreeItem *p_item, const String &p_filter, Vector &p_select_candidates) { + if (!p_item) { + return; + } + + NodePath np = p_item->get_metadata(0); + Node *node = get_node(np); + + if (!p_filter.is_empty() && ((String)node->get_name()).findn(p_filter) != -1) { + p_select_candidates.push_back(node); + } + + TreeItem *c = p_item->get_first_child(); + + while (c) { + _pick_node_select_recursive(c, p_filter, p_select_candidates); + c = c->get_next(); + } +} + +void ReplicationEditor::_pick_node_filter_input(const Ref &p_ie) { + Ref k = p_ie; + + if (k.is_valid()) { + switch (k->get_keycode()) { + case Key::UP: + case Key::DOWN: + case Key::PAGEUP: + case Key::PAGEDOWN: { + pick_node->get_scene_tree()->get_scene_tree()->gui_input(k); + pick_node->get_filter_line_edit()->accept_event(); + } break; + default: + break; + } + } +} + +void ReplicationEditor::_pick_node_selected(NodePath p_path) { + Node *root = current->get_node(current->get_root_path()); + ERR_FAIL_COND(!root); + Node *node = get_node(p_path); + ERR_FAIL_COND(!node); + NodePath path_to = root->get_path_to(node); + adding_node_path = path_to; + prop_selector->select_property_from_instance(node); +} + +void ReplicationEditor::_pick_new_property() { + if (current == nullptr) { + EditorNode::get_singleton()->show_warning(TTR("Select a replicator node in order to pick a property to add to it.")); + return; + } + Node *root = current->get_node(current->get_root_path()); + if (!root) { + EditorNode::get_singleton()->show_warning(TTR("Not possible to add a new property to synchronize without a root.")); + return; + } + pick_node->popup_scenetree_dialog(); + pick_node->get_filter_line_edit()->clear(); + pick_node->get_filter_line_edit()->grab_focus(); +} + +void ReplicationEditor::_add_sync_property(String p_path) { + config = current->get_replication_config(); + + if (config.is_valid() && config->has_property(p_path)) { + EditorNode::get_singleton()->show_warning(TTR("Property is already being synchronized.")); + return; + } + + UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo(); + undo_redo->create_action(TTR("Add property to synchronizer")); + + if (config.is_null()) { + config.instantiate(); + current->set_replication_config(config); + undo_redo->add_do_method(current, "set_replication_config", config); + undo_redo->add_undo_method(current, "set_replication_config", Ref()); + _update_config(); + } + + undo_redo->add_do_method(config.ptr(), "add_property", p_path); + undo_redo->add_undo_method(config.ptr(), "remove_property", p_path); + undo_redo->add_do_method(this, "_update_config"); + undo_redo->add_undo_method(this, "_update_config"); + undo_redo->commit_action(); +} + +void ReplicationEditor::_pick_node_property_selected(String p_name) { + String adding_prop_path = String(adding_node_path) + ":" + p_name; + + _add_sync_property(adding_prop_path); +} + /// ReplicationEditor ReplicationEditor::ReplicationEditor() { set_v_size_flags(SIZE_EXPAND_FILL); @@ -56,16 +179,44 @@ ReplicationEditor::ReplicationEditor() { vb->set_v_size_flags(SIZE_EXPAND_FILL); add_child(vb); + pick_node = memnew(SceneTreeDialog); + add_child(pick_node); + pick_node->register_text_enter(pick_node->get_filter_line_edit()); + pick_node->set_title(TTR("Pick a node to synchronize:")); + pick_node->connect("selected", callable_mp(this, &ReplicationEditor::_pick_node_selected)); + pick_node->get_filter_line_edit()->connect("text_changed", callable_mp(this, &ReplicationEditor::_pick_node_filter_text_changed)); + pick_node->get_filter_line_edit()->connect("gui_input", callable_mp(this, &ReplicationEditor::_pick_node_filter_input)); + + prop_selector = memnew(PropertySelector); + add_child(prop_selector); + prop_selector->connect("selected", callable_mp(this, &ReplicationEditor::_pick_node_property_selected)); + HBoxContainer *hb = memnew(HBoxContainer); vb->add_child(hb); + + add_pick_button = memnew(Button); + add_pick_button->connect("pressed", callable_mp(this, &ReplicationEditor::_pick_new_property)); + add_pick_button->set_text(TTR("Add property to sync..")); + hb->add_child(add_pick_button); + VSeparator *vs = memnew(VSeparator); + vs->set_custom_minimum_size(Size2(30 * EDSCALE, 0)); + hb->add_child(vs); + hb->add_child(memnew(Label(TTR("Path:")))); np_line_edit = memnew(LineEdit); np_line_edit->set_placeholder(":property"); np_line_edit->set_h_size_flags(SIZE_EXPAND_FILL); hb->add_child(np_line_edit); - add_button = memnew(Button); - add_button->connect("pressed", callable_mp(this, &ReplicationEditor::_add_pressed)); - add_button->set_text(TTR("Add")); - hb->add_child(add_button); + add_from_path_button = memnew(Button); + add_from_path_button->connect("pressed", callable_mp(this, &ReplicationEditor::_add_pressed)); + add_from_path_button->set_text(TTR("Add from path")); + hb->add_child(add_from_path_button); + vs = memnew(VSeparator); + vs->set_custom_minimum_size(Size2(30 * EDSCALE, 0)); + hb->add_child(vs); + pin = memnew(Button); + pin->set_flat(true); + pin->set_toggle_mode(true); + hb->add_child(pin); tree = memnew(Tree); tree->set_hide_root(true); @@ -85,19 +236,88 @@ ReplicationEditor::ReplicationEditor() { tree->connect("item_edited", callable_mp(this, &ReplicationEditor::_tree_item_edited)); tree->set_v_size_flags(SIZE_EXPAND_FILL); vb->add_child(tree); + + drop_label = memnew(Label); + drop_label->set_text(TTR("Add properties using the buttons above or\ndrag them them from the inspector and drop them here.")); + drop_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); + drop_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER); + tree->add_child(drop_label); + drop_label->set_anchors_and_offsets_preset(Control::PRESET_WIDE); + + tree->set_drag_forwarding(this); } void ReplicationEditor::_bind_methods() { ClassDB::bind_method(D_METHOD("_update_config"), &ReplicationEditor::_update_config); ClassDB::bind_method(D_METHOD("_update_checked", "property", "column", "checked"), &ReplicationEditor::_update_checked); + ClassDB::bind_method("_can_drop_data_fw", &ReplicationEditor::_can_drop_data_fw); + ClassDB::bind_method("_drop_data_fw", &ReplicationEditor::_drop_data_fw); + ADD_SIGNAL(MethodInfo("keying_changed")); } +bool ReplicationEditor::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const { + Dictionary d = p_data; + if (!d.has("type")) { + return false; + } + String t = d["type"]; + if (t != "obj_property") { + return false; + } + Object *obj = d["object"]; + if (!obj) { + return false; + } + Node *node = Object::cast_to(obj); + if (!node) { + return false; + } + + return true; +} + +void ReplicationEditor::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { + if (current == nullptr) { + EditorNode::get_singleton()->show_warning(TTR("Select a replicator node in order to pick a property to add to it.")); + return; + } + Node *root = current->get_node(current->get_root_path()); + if (!root) { + EditorNode::get_singleton()->show_warning(TTR("Not possible to add a new property to synchronize without a root.")); + return; + } + + Dictionary d = p_data; + if (!d.has("type")) { + return; + } + String t = d["type"]; + if (t != "obj_property") { + return; + } + Object *obj = d["object"]; + if (!obj) { + return; + } + Node *node = Object::cast_to(obj); + if (!node) { + return; + } + + String path = root->get_path_to(node); + path += ":" + String(d["property"]); + + _add_sync_property(path); +} + void ReplicationEditor::_notification(int p_what) { switch (p_what) { case NOTIFICATION_ENTER_TREE: case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { add_theme_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("panel"), SNAME("Panel"))); + add_pick_button->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons"))); + pin->set_icon(get_theme_icon(SNAME("Pin"), SNAME("EditorIcons"))); } break; case NOTIFICATION_VISIBILITY_CHANGED: { @@ -236,11 +456,15 @@ void ReplicationEditor::_update_config() { deleting = NodePath(); tree->clear(); tree->create_item(); + drop_label->set_visible(true); if (!config.is_valid()) { update_keying(); return; } TypedArray props = config->get_properties(); + if (props.size()) { + drop_label->set_visible(false); + } for (int i = 0; i < props.size(); i++) { const NodePath path = props[i]; _add_property(path, config->property_get_spawn(path), config->property_get_sync(path)); @@ -341,7 +565,9 @@ void ReplicationEditor::property_keyed(const String &p_property) { /// ReplicationEditorPlugin ReplicationEditorPlugin::ReplicationEditorPlugin() { repl_editor = memnew(ReplicationEditor); - EditorNode::get_singleton()->add_bottom_panel_item(TTR("Replication"), repl_editor); + button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Replication"), repl_editor); + button->hide(); + repl_editor->get_pin()->connect("pressed", callable_mp(this, &ReplicationEditorPlugin::_pinned)); } ReplicationEditorPlugin::~ReplicationEditorPlugin() { @@ -378,6 +604,17 @@ void ReplicationEditorPlugin::_node_removed(Node *p_node) { if (repl_editor->is_visible_in_tree()) { EditorNode::get_singleton()->hide_bottom_panel(); } + button->hide(); + repl_editor->get_pin()->set_pressed(false); + } +} + +void ReplicationEditorPlugin::_pinned() { + if (!repl_editor->get_pin()->is_pressed()) { + if (repl_editor->is_visible_in_tree()) { + EditorNode::get_singleton()->hide_bottom_panel(); + } + button->hide(); } } @@ -391,6 +628,14 @@ bool ReplicationEditorPlugin::handles(Object *p_object) const { void ReplicationEditorPlugin::make_visible(bool p_visible) { if (p_visible) { + //editor->hide_animation_player_editors(); + //editor->animation_panel_make_visible(true); + button->show(); EditorNode::get_singleton()->make_bottom_panel_item_visible(repl_editor); + } else if (!repl_editor->get_pin()->is_pressed()) { + if (repl_editor->is_visible_in_tree()) { + EditorNode::get_singleton()->hide_bottom_panel(); + } + button->hide(); } } diff --git a/editor/plugins/replication_editor_plugin.h b/editor/plugins/replication_editor_plugin.h index 08e86d1617d..b6de08a3a83 100644 --- a/editor/plugins/replication_editor_plugin.h +++ b/editor/plugins/replication_editor_plugin.h @@ -34,6 +34,10 @@ #include "editor/editor_plugin.h" #include "scene/resources/scene_replication_config.h" +#include "editor/editor_spin_slider.h" +#include "editor/property_editor.h" +#include "editor/property_selector.h" + class ConfirmationDialog; class MultiplayerSynchronizer; class Tree; @@ -46,14 +50,23 @@ private: AcceptDialog *error_dialog = nullptr; ConfirmationDialog *delete_dialog = nullptr; - Button *add_button = nullptr; + Button *add_pick_button = nullptr; + Button *add_from_path_button = nullptr; LineEdit *np_line_edit = nullptr; + Label *drop_label = nullptr; + Ref config; NodePath deleting; Tree *tree = nullptr; bool keying = false; + PropertySelector *prop_selector = nullptr; + SceneTreeDialog *pick_node = nullptr; + NodePath adding_node_path; + + Button *pin = nullptr; + Ref _get_class_icon(const Node *p_node); void _add_pressed(); @@ -64,6 +77,19 @@ private: void _dialog_closed(bool p_confirmed); void _add_property(const NodePath &p_property, bool p_spawn = true, bool p_sync = true); + void _pick_node_filter_text_changed(const String &p_newtext); + void _pick_node_select_recursive(TreeItem *p_item, const String &p_filter, Vector &p_select_candidates); + void _pick_node_filter_input(const Ref &p_ie); + void _pick_node_selected(NodePath p_path); + + void _pick_new_property(); + void _pick_node_property_selected(String p_name); + + bool _can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const; + void _drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from); + + void _add_sync_property(String p_path); + protected: static void _bind_methods(); @@ -76,6 +102,7 @@ public: MultiplayerSynchronizer *get_current() const { return current; } void property_keyed(const String &p_property); + Button *get_pin() { return pin; } ReplicationEditor(); ~ReplicationEditor() {} }; @@ -84,12 +111,15 @@ class ReplicationEditorPlugin : public EditorPlugin { GDCLASS(ReplicationEditorPlugin, EditorPlugin); private: + Button *button = nullptr; ReplicationEditor *repl_editor = nullptr; void _node_removed(Node *p_node); void _keying_changed(); void _property_keyed(const String &p_keyed, const Variant &p_value, bool p_advance); + void _pinned(); + protected: void _notification(int p_what); diff --git a/scene/multiplayer/multiplayer_spawner.cpp b/scene/multiplayer/multiplayer_spawner.cpp index a9b9ffa989c..ddd01d0a43f 100644 --- a/scene/multiplayer/multiplayer_spawner.cpp +++ b/scene/multiplayer/multiplayer_spawner.cpp @@ -35,12 +35,106 @@ #include "scene/main/window.h" #include "scene/scene_string_names.h" -void MultiplayerSpawner::_bind_methods() { - ClassDB::bind_method(D_METHOD("spawn", "data"), &MultiplayerSpawner::spawn, DEFVAL(Variant())); +#ifdef TOOLS_ENABLED +/* This is editor only */ +bool MultiplayerSpawner::_set(const StringName &p_name, const Variant &p_value) { + if (p_name == "_spawnable_scene_count") { + spawnable_scenes.resize(p_value); + notify_property_list_changed(); + return true; + } else { + String ns = p_name; + if (ns.begins_with("scenes/")) { + uint32_t index = ns.get_slicec('/', 1).to_int(); + ERR_FAIL_UNSIGNED_INDEX_V(index, spawnable_scenes.size(), false); + spawnable_scenes[index].path = p_value; + return true; + } + } + return false; +} - ClassDB::bind_method(D_METHOD("get_spawnable_scenes"), &MultiplayerSpawner::get_spawnable_scenes); - ClassDB::bind_method(D_METHOD("set_spawnable_scenes", "scenes"), &MultiplayerSpawner::set_spawnable_scenes); - ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "replication", PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "PackedScene"), (PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE)), "set_spawnable_scenes", "get_spawnable_scenes"); +bool MultiplayerSpawner::_get(const StringName &p_name, Variant &r_ret) const { + if (p_name == "_spawnable_scene_count") { + r_ret = spawnable_scenes.size(); + return true; + } else { + String ns = p_name; + if (ns.begins_with("scenes/")) { + uint32_t index = ns.get_slicec('/', 1).to_int(); + ERR_FAIL_UNSIGNED_INDEX_V(index, spawnable_scenes.size(), false); + r_ret = spawnable_scenes[index].path; + return true; + } + } + return false; +} + +void MultiplayerSpawner::_get_property_list(List *p_list) const { + p_list->push_back(PropertyInfo(Variant::INT, "_spawnable_scene_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ARRAY, "Scenes,scenes/")); + List exts; + ResourceLoader::get_recognized_extensions_for_type("PackedScene", &exts); + String ext_hint; + for (const String &E : exts) { + if (!ext_hint.is_empty()) { + ext_hint += ","; + } + ext_hint += "*." + E; + } + for (uint32_t i = 0; i < spawnable_scenes.size(); i++) { + p_list->push_back(PropertyInfo(Variant::STRING, "scenes/" + itos(i), PROPERTY_HINT_FILE, ext_hint, PROPERTY_USAGE_EDITOR)); + } +} +#endif +void MultiplayerSpawner::add_spawnable_scene(const String &p_path) { + SpawnableScene sc; + sc.path = p_path; + if (Engine::get_singleton()->is_editor_hint()) { + ERR_FAIL_COND(!FileAccess::exists(p_path)); + } else { + sc.cache = ResourceLoader::load(p_path); + ERR_FAIL_COND_MSG(sc.cache.is_null(), "Invalid spawnable scene: " + p_path); + } + spawnable_scenes.push_back(sc); +} +int MultiplayerSpawner::get_spawnable_scene_count() const { + return spawnable_scenes.size(); +} +String MultiplayerSpawner::get_spawnable_scene(int p_idx) const { + return spawnable_scenes[p_idx].path; +} +void MultiplayerSpawner::clear_spawnable_scenes() { + spawnable_scenes.clear(); +} + +Vector MultiplayerSpawner::_get_spawnable_scenes() const { + Vector ss; + ss.resize(spawnable_scenes.size()); + for (int i = 0; i < ss.size(); i++) { + ss.write[i] = spawnable_scenes[i].path; + } + return ss; +} + +void MultiplayerSpawner::_set_spawnable_scenes(const Vector &p_scenes) { + clear_spawnable_scenes(); + for (int i = 0; i < p_scenes.size(); i++) { + add_spawnable_scene(p_scenes[i]); + } +} + +void MultiplayerSpawner::_bind_methods() { + ClassDB::bind_method(D_METHOD("add_spawnable_scene", "path"), &MultiplayerSpawner::add_spawnable_scene); + ClassDB::bind_method(D_METHOD("get_spawnable_scene_count"), &MultiplayerSpawner::get_spawnable_scene_count); + ClassDB::bind_method(D_METHOD("get_spawnable_scene", "path"), &MultiplayerSpawner::get_spawnable_scene); + ClassDB::bind_method(D_METHOD("clear_spawnable_scenes"), &MultiplayerSpawner::clear_spawnable_scenes); + + ClassDB::bind_method(D_METHOD("_get_spawnable_scenes"), &MultiplayerSpawner::_get_spawnable_scenes); + ClassDB::bind_method(D_METHOD("_set_spawnable_scenes", "scenes"), &MultiplayerSpawner::_set_spawnable_scenes); + + ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "_spawnable_scenes", PROPERTY_HINT_NONE, "", (PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL)), "_set_spawnable_scenes", "_get_spawnable_scenes"); + + ClassDB::bind_method(D_METHOD("spawn", "data"), &MultiplayerSpawner::spawn, DEFVAL(Variant())); ClassDB::bind_method(D_METHOD("get_spawn_path"), &MultiplayerSpawner::get_spawn_path); ClassDB::bind_method(D_METHOD("set_spawn_path", "path"), &MultiplayerSpawner::set_spawn_path); @@ -118,7 +212,7 @@ void MultiplayerSpawner::_node_added(Node *p_node) { if (!parent || p_node->get_parent() != parent) { return; } - int id = get_scene_id(p_node->get_scene_file_path()); + int id = find_spawnable_scene_index_from_path(p_node->get_scene_file_path()); if (id == INVALID_ID) { return; } @@ -136,14 +230,6 @@ bool MultiplayerSpawner::is_auto_spawning() const { return auto_spawn; } -TypedArray MultiplayerSpawner::get_spawnable_scenes() { - return spawnable_scenes; -} - -void MultiplayerSpawner::set_spawnable_scenes(TypedArray p_scenes) { - spawnable_scenes = p_scenes; -} - NodePath MultiplayerSpawner::get_spawn_path() const { return spawn_path; } @@ -175,18 +261,16 @@ void MultiplayerSpawner::_node_exit(ObjectID p_id) { } } -int MultiplayerSpawner::get_scene_id(const String &p_scene) const { - for (int i = 0; i < spawnable_scenes.size(); i++) { - Ref ps = spawnable_scenes[i]; - ERR_CONTINUE(ps.is_null()); - if (ps->get_path() == p_scene) { +int MultiplayerSpawner::find_spawnable_scene_index_from_path(const String &p_scene) const { + for (uint32_t i = 0; i < spawnable_scenes.size(); i++) { + if (spawnable_scenes[i].path == p_scene) { return i; } } return INVALID_ID; } -int MultiplayerSpawner::get_spawn_id(const ObjectID &p_id) const { +int MultiplayerSpawner::find_spawnable_scene_index_from_object(const ObjectID &p_id) const { const SpawnInfo *info = tracked_nodes.getptr(p_id); return info ? info->id : INVALID_ID; } @@ -198,8 +282,8 @@ const Variant MultiplayerSpawner::get_spawn_argument(const ObjectID &p_id) const Node *MultiplayerSpawner::instantiate_scene(int p_id) { ERR_FAIL_COND_V_MSG(spawn_limit && spawn_limit <= tracked_nodes.size(), nullptr, "Spawn limit reached!"); - ERR_FAIL_INDEX_V(p_id, spawnable_scenes.size(), nullptr); - Ref scene = spawnable_scenes[p_id]; + ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_id, spawnable_scenes.size(), nullptr); + Ref scene = spawnable_scenes[p_id].cache; ERR_FAIL_COND_V(scene.is_null(), nullptr); return scene->instantiate(); } diff --git a/scene/multiplayer/multiplayer_spawner.h b/scene/multiplayer/multiplayer_spawner.h index 8fbc9c48038..e8abe702a0b 100644 --- a/scene/multiplayer/multiplayer_spawner.h +++ b/scene/multiplayer/multiplayer_spawner.h @@ -33,6 +33,7 @@ #include "scene/main/node.h" +#include "core/templates/local_vector.h" #include "core/variant/typed_array.h" #include "scene/resources/packed_scene.h" #include "scene/resources/scene_replication_config.h" @@ -46,7 +47,13 @@ public: }; private: - TypedArray spawnable_scenes; + struct SpawnableScene { + String path; + Ref cache; + }; + + LocalVector spawnable_scenes; + HashSet spawnable_ids; NodePath spawn_path; @@ -71,14 +78,26 @@ private: void _node_exit(ObjectID p_id); void _node_ready(ObjectID p_id); + Vector _get_spawnable_scenes() const; + void _set_spawnable_scenes(const Vector &p_scenes); + protected: static void _bind_methods(); void _notification(int p_what); +#ifdef TOOLS_ENABLED + bool _set(const StringName &p_name, const Variant &p_value); + bool _get(const StringName &p_name, Variant &r_ret) const; + void _get_property_list(List *p_list) const; +#endif public: Node *get_spawn_node() const { return spawn_node.is_valid() ? Object::cast_to(ObjectDB::get_instance(spawn_node)) : nullptr; } - TypedArray get_spawnable_scenes(); - void set_spawnable_scenes(TypedArray p_scenes); + + void add_spawnable_scene(const String &p_path); + int get_spawnable_scene_count() const; + String get_spawnable_scene(int p_idx) const; + void clear_spawnable_scenes(); + NodePath get_spawn_path() const; void set_spawn_path(const NodePath &p_path); uint32_t get_spawn_limit() const { return spawn_limit; } @@ -87,8 +106,8 @@ public: void set_auto_spawning(bool p_enabled); const Variant get_spawn_argument(const ObjectID &p_id) const; - int get_spawn_id(const ObjectID &p_id) const; - int get_scene_id(const String &p_path) const; + int find_spawnable_scene_index_from_object(const ObjectID &p_id) const; + int find_spawnable_scene_index_from_path(const String &p_path) const; Node *spawn(const Variant &p_data = Variant()); Node *instantiate_custom(const Variant &p_data); Node *instantiate_scene(int p_idx); diff --git a/scene/multiplayer/multiplayer_synchronizer.cpp b/scene/multiplayer/multiplayer_synchronizer.cpp index 33e845a7a37..34d5abf9f6c 100644 --- a/scene/multiplayer/multiplayer_synchronizer.cpp +++ b/scene/multiplayer/multiplayer_synchronizer.cpp @@ -96,7 +96,7 @@ void MultiplayerSynchronizer::_bind_methods() { ClassDB::bind_method(D_METHOD("set_replication_config", "config"), &MultiplayerSynchronizer::set_replication_config); ClassDB::bind_method(D_METHOD("get_replication_config"), &MultiplayerSynchronizer::get_replication_config); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "SceneReplicationConfig"), "set_replication_config", "get_replication_config"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "congiruation", PROPERTY_HINT_RESOURCE_TYPE, "SceneReplicationConfig"), "set_replication_config", "get_replication_config"); } void MultiplayerSynchronizer::_notification(int p_what) { diff --git a/scene/multiplayer/multiplayer_synchronizer.h b/scene/multiplayer/multiplayer_synchronizer.h index e856745379d..f61ef459dae 100644 --- a/scene/multiplayer/multiplayer_synchronizer.h +++ b/scene/multiplayer/multiplayer_synchronizer.h @@ -40,7 +40,7 @@ class MultiplayerSynchronizer : public Node { private: Ref replication_config; - NodePath root_path; + NodePath root_path = NodePath(".."); // Start with parent, like with AnimationPlayer. uint64_t interval_msec = 0; static Object *_get_prop_target(Object *p_obj, const NodePath &p_prop); diff --git a/scene/multiplayer/scene_replication_interface.cpp b/scene/multiplayer/scene_replication_interface.cpp index 19c69adb4af..e4715ceb885 100644 --- a/scene/multiplayer/scene_replication_interface.cpp +++ b/scene/multiplayer/scene_replication_interface.cpp @@ -167,7 +167,7 @@ Error SceneReplicationInterface::_send_spawn(Node *p_node, MultiplayerSpawner *p uint32_t nid = rep_state->ensure_net_id(oid); // Prepare custom arg and scene_id - uint8_t scene_id = p_spawner->get_spawn_id(oid); + uint8_t scene_id = p_spawner->find_spawnable_scene_index_from_object(oid); bool is_custom = scene_id == MultiplayerSpawner::INVALID_ID; Variant spawn_arg = p_spawner->get_spawn_argument(oid); int spawn_arg_size = 0; diff --git a/scene/resources/scene_replication_config.cpp b/scene/resources/scene_replication_config.cpp index 2acc0f19221..4aea04bf876 100644 --- a/scene/resources/scene_replication_config.cpp +++ b/scene/resources/scene_replication_config.cpp @@ -124,6 +124,15 @@ void SceneReplicationConfig::remove_property(const NodePath &p_path) { properties.erase(p_path); } +bool SceneReplicationConfig::has_property(const NodePath &p_path) const { + for (int i = 0; i < properties.size(); i++) { + if (properties[i].name == p_path) { + return true; + } + } + return false; +} + int SceneReplicationConfig::property_get_index(const NodePath &p_path) const { for (int i = 0; i < properties.size(); i++) { if (properties[i].name == p_path) { @@ -178,6 +187,7 @@ void SceneReplicationConfig::property_set_sync(const NodePath &p_path, bool p_en void SceneReplicationConfig::_bind_methods() { ClassDB::bind_method(D_METHOD("get_properties"), &SceneReplicationConfig::get_properties); ClassDB::bind_method(D_METHOD("add_property", "path", "index"), &SceneReplicationConfig::add_property, DEFVAL(-1)); + ClassDB::bind_method(D_METHOD("has_property", "path"), &SceneReplicationConfig::has_property); ClassDB::bind_method(D_METHOD("remove_property", "path"), &SceneReplicationConfig::remove_property); ClassDB::bind_method(D_METHOD("property_get_index", "path"), &SceneReplicationConfig::property_get_index); ClassDB::bind_method(D_METHOD("property_get_spawn", "path"), &SceneReplicationConfig::property_get_spawn); diff --git a/scene/resources/scene_replication_config.h b/scene/resources/scene_replication_config.h index b791be94145..ab3658d2a7e 100644 --- a/scene/resources/scene_replication_config.h +++ b/scene/resources/scene_replication_config.h @@ -73,6 +73,7 @@ public: void add_property(const NodePath &p_path, int p_index = -1); void remove_property(const NodePath &p_path); + bool has_property(const NodePath &p_path) const; int property_get_index(const NodePath &p_path) const; bool property_get_spawn(const NodePath &p_path);