Skip to content

Commit b7c5aec

Browse files
author
Nathan Franke
committedJul 31, 2022
document multiplayer replication classes, small changes to multiplayer spawner
1 parent 66d1bb8 commit b7c5aec

6 files changed

+78
-17
lines changed
 

‎modules/multiplayer/doc_classes/MultiplayerSpawner.xml

+25-7
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,83 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<class name="MultiplayerSpawner" inherits="Node" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
33
<brief_description>
4+
Automatically replicates spawnable nodes from the authority to other multiplayer peers.
45
</brief_description>
56
<description>
6-
This node uses [method MultiplayerAPI.object_configuration_add] to notify spawns passing the spawned node as the [code]object[/code] and itself as the [code]configuration[/code], and [method MultiplayerAPI.object_configuration_remove] to notify despawns in a similar way.
7+
Spawnable scenes can be configured in the editor or through code (see [method add_spawnable_scene]).
8+
Also supports custom node spawns through [method spawn], calling [method _spawn_custom] on all peers.
9+
10+
Internally, [MultiplayerSpawner] uses [method MultiplayerAPI.object_configuration_add] to notify spawns passing the spawned node as the [code]object[/code] and itself as the [code]configuration[/code], and [method MultiplayerAPI.object_configuration_remove] to notify despawns in a similar way.
711
</description>
812
<tutorials>
913
</tutorials>
1014
<methods>
1115
<method name="_spawn_custom" qualifiers="virtual">
12-
<return type="Object" />
16+
<return type="Node" />
1317
<argument index="0" name="data" type="Variant" />
1418
<description>
19+
Method called on all peers when a custom spawn was requested by the authority using [method spawn]. Should return a [Node] that is not in the scene tree.
20+
21+
[b]Note:[/b] Spawned nodes should [b]not[/b] be added to the scene with `add_child`. This is done automatically.
1522
</description>
1623
</method>
1724
<method name="add_spawnable_scene">
1825
<return type="void" />
1926
<argument index="0" name="path" type="String" />
2027
<description>
28+
Adds a scene path to spawnable scenes, making it automatically replicated from the multiplayer authority to other peers when added as children of the node pointed by [member spawn_path].
2129
</description>
2230
</method>
2331
<method name="clear_spawnable_scenes">
2432
<return type="void" />
2533
<description>
34+
Clears all spawnable scenes. Does not despawn existing instances on remote peers.
2635
</description>
2736
</method>
2837
<method name="get_spawnable_scene" qualifiers="const">
2938
<return type="String" />
30-
<argument index="0" name="path" type="int" />
39+
<argument index="0" name="index" type="int" />
3140
<description>
41+
Returns the spawnable scene path by index.
3242
</description>
3343
</method>
3444
<method name="get_spawnable_scene_count" qualifiers="const">
3545
<return type="int" />
3646
<description>
47+
Returns the count of spawnable scene paths.
3748
</description>
3849
</method>
3950
<method name="spawn">
4051
<return type="Node" />
4152
<argument index="0" name="data" type="Variant" default="null" />
4253
<description>
54+
Requests a custom spawn, with [code]data[/code] passed to [method _spawn_custom] on all peers. Returns the locally spawned node instance already inside the scene tree, and added as a child of the node pointed by [member spawn_path].
55+
56+
[b]Note:[/b] Spawnable scenes are spawned automatically. [method spawn] is only needed for custom spawns.
4357
</description>
4458
</method>
4559
</methods>
4660
<members>
4761
<member name="spawn_limit" type="int" setter="set_spawn_limit" getter="get_spawn_limit" default="0">
62+
Maximum nodes that is allowed to be spawned by this spawner. Includes both spawnable scenes and custom spawns.
63+
64+
When set to [code]0[/code] (the default), there is no limit.
4865
</member>
4966
<member name="spawn_path" type="NodePath" setter="set_spawn_path" getter="get_spawn_path" default="NodePath(&quot;&quot;)">
67+
Path to the spawn root. Spawnable scenes that are added as direct children are replicated to other peers.
5068
</member>
5169
</members>
5270
<signals>
5371
<signal name="despawned">
54-
<argument index="0" name="scene_id" type="int" />
55-
<argument index="1" name="node" type="Node" />
72+
<argument index="0" name="node" type="Node" />
5673
<description>
74+
Emitted when a spawnable scene or custom spawn was despawned by the multiplayer authority. Only called on puppets.
5775
</description>
5876
</signal>
5977
<signal name="spawned">
60-
<argument index="0" name="scene_id" type="int" />
61-
<argument index="1" name="node" type="Node" />
78+
<argument index="0" name="node" type="Node" />
6279
<description>
80+
Emitted when a spawnable scene or custom spawn was spawned by the multiplayer authority. Only called on puppets.
6381
</description>
6482
</signal>
6583
</signals>
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<class name="MultiplayerSynchronizer" inherits="Node" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
33
<brief_description>
4+
Synchronizes properties from the multiplayer authority to the remote peers.
45
</brief_description>
56
<description>
6-
The [MultiplayerSynchronizer] uses [method MultiplayerAPI.object_configuration_add] to notify synchronization start passing the [Node] at [member root_path] as the [code]object[/code] and itself as the [code]configuration[/code], and uses [method MultiplayerAPI.object_configuration_remove] to notify synchronization end in a similar way.
7+
By default, [MultiplayerSynchronizer] synchronizes configured properties to all peers.
8+
Visiblity can be handled directly with [method set_visibility_for] or as-needed with [method add_visibility_filter] and [method update_visibility].
9+
10+
[MultiplayerSpawner]s will handle nodes according to visibility of synchronizers as long as the node at [member root_path] was spawned by one.
11+
12+
Internally, [MultiplayerSynchronizer] uses [method MultiplayerAPI.object_configuration_add] to notify synchronization start passing the [Node] at [member root_path] as the [code]object[/code] and itself as the [code]configuration[/code], and uses [method MultiplayerAPI.object_configuration_remove] to notify synchronization end in a similar way.
713
</description>
814
<tutorials>
915
</tutorials>
@@ -12,59 +18,76 @@
1218
<return type="void" />
1319
<argument index="0" name="filter" type="Callable" />
1420
<description>
21+
Adds a peer visibility filter for this synchronizer.
22+
23+
[code]filter[/code] should take a peer id [int] and return a [bool].
1524
</description>
1625
</method>
1726
<method name="get_visibility_for" qualifiers="const">
1827
<return type="bool" />
1928
<argument index="0" name="peer" type="int" />
2029
<description>
30+
Queries the current visibility for peer [code]peer[/code].
2131
</description>
2232
</method>
2333
<method name="remove_visibility_filter">
2434
<return type="void" />
2535
<argument index="0" name="filter" type="Callable" />
2636
<description>
37+
Removes a peer visiblity filter from this synchronizer.
2738
</description>
2839
</method>
2940
<method name="set_visibility_for">
3041
<return type="void" />
3142
<argument index="0" name="peer" type="int" />
3243
<argument index="1" name="visible" type="bool" />
3344
<description>
45+
Sets the visibility of [code]peer[/code] to [code]visible[/code]. If [code]peer[/code] is [code]0[/code], the value of [member public_visibility] will be updated instead.
3446
</description>
3547
</method>
3648
<method name="update_visibility">
3749
<return type="void" />
3850
<argument index="0" name="for_peer" type="int" default="0" />
3951
<description>
52+
Updates the visibility of [code]peer[/code] according to visibility filters. If [code]peer[/code] is [code]0[/code] (the default), all peers' visibilties are updated.
4053
</description>
4154
</method>
4255
</methods>
4356
<members>
4457
<member name="public_visibility" type="bool" setter="set_visibility_public" getter="is_visibility_public" default="true">
58+
Whether synchronization should be visible to all peers by default. See [method set_visibility_for] and [method add_visibility_filter] for ways of configuring fine-grained visibility options.
4559
</member>
4660
<member name="replication_config" type="SceneReplicationConfig" setter="set_replication_config" getter="get_replication_config">
61+
Resource containing which properties to synchronize.
4762
</member>
4863
<member name="replication_interval" type="float" setter="set_replication_interval" getter="get_replication_interval" default="0.0">
64+
Time interval between synchronizes. When set to [code]0.0[/code] (the default), synchronizes happen every network process frame.
4965
</member>
5066
<member name="root_path" type="NodePath" setter="set_root_path" getter="get_root_path" default="NodePath(&quot;..&quot;)">
67+
Node path that replicated properties are relative to.
68+
If [member root_path] was spawned by a [MultiplayerSpawner], the node will be also be spawned and despawned based on this synchronizer visibility options.
5169
</member>
5270
<member name="visibility_update_mode" type="int" setter="set_visibility_update_mode" getter="get_visibility_update_mode" enum="MultiplayerSynchronizer.VisibilityUpdateMode" default="0">
71+
Specifies when visibility filters are updated (see [enum VisibilityUpdateMode] for options).
5372
</member>
5473
</members>
5574
<signals>
5675
<signal name="visibility_changed">
5776
<argument index="0" name="for_peer" type="int" />
5877
<description>
78+
Emitted when visibility of [code]for_peer[/code] is updated. See [method update_visibility].
5979
</description>
6080
</signal>
6181
</signals>
6282
<constants>
6383
<constant name="VISIBILITY_PROCESS_IDLE" value="0" enum="VisibilityUpdateMode">
84+
Visibility filters are updated every idle process frame.
6485
</constant>
6586
<constant name="VISIBILITY_PROCESS_PHYSICS" value="1" enum="VisibilityUpdateMode">
87+
Visibility filters are updated every physics process frame.
6688
</constant>
6789
<constant name="VISIBILITY_PROCESS_NONE" value="2" enum="VisibilityUpdateMode">
90+
Visibility filters are not updated automatically, and must be updated manually by calling [method update_visibility].
6891
</constant>
6992
</constants>
7093
</class>

‎modules/multiplayer/doc_classes/SceneReplicationConfig.xml

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<class name="SceneReplicationConfig" inherits="Resource" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
33
<brief_description>
4+
Configuration for properties to synchronize with a [MultiplayerSynchronizer].
45
</brief_description>
56
<description>
67
</description>
@@ -12,55 +13,64 @@
1213
<argument index="0" name="path" type="NodePath" />
1314
<argument index="1" name="index" type="int" default="-1" />
1415
<description>
16+
Adds the property identified by the given [code]path[/code] to the list of the properties being synchronized, optionally passing an [code]index[/code].
1517
</description>
1618
</method>
1719
<method name="get_properties" qualifiers="const">
1820
<return type="NodePath[]" />
1921
<description>
22+
Returns a list of synchronized property [NodePath]s.
2023
</description>
2124
</method>
2225
<method name="has_property" qualifiers="const">
2326
<return type="bool" />
2427
<argument index="0" name="path" type="NodePath" />
2528
<description>
29+
Returns whether the given [code]path[/code] is configured for synchronization.
2630
</description>
2731
</method>
2832
<method name="property_get_index" qualifiers="const">
2933
<return type="int" />
3034
<argument index="0" name="path" type="NodePath" />
3135
<description>
36+
Finds the index of the given [code]path[/code].
3237
</description>
3338
</method>
3439
<method name="property_get_spawn">
3540
<return type="bool" />
3641
<argument index="0" name="path" type="NodePath" />
3742
<description>
43+
Returns whether the property identified by the given [code]path[/code] is configured to be synchronized on spawn.
3844
</description>
3945
</method>
4046
<method name="property_get_sync">
4147
<return type="bool" />
4248
<argument index="0" name="path" type="NodePath" />
4349
<description>
50+
Returns whether the property identified by the given [code]path[/code] is configured to be synchronized on process.
4451
</description>
4552
</method>
4653
<method name="property_set_spawn">
4754
<return type="void" />
4855
<argument index="0" name="path" type="NodePath" />
4956
<argument index="1" name="enabled" type="bool" />
5057
<description>
58+
Sets whether the property identified by the given [code]path[/code] is configured to be synchronized on spawn.
5159
</description>
5260
</method>
5361
<method name="property_set_sync">
5462
<return type="void" />
5563
<argument index="0" name="path" type="NodePath" />
5664
<argument index="1" name="enabled" type="bool" />
5765
<description>
66+
Sets whether the property identified by the given [code]path[/code] is configured to be synchronized on process.
5867
</description>
5968
</method>
6069
<method name="remove_property">
6170
<return type="void" />
6271
<argument index="0" name="path" type="NodePath" />
6372
<description>
73+
Removes the property identified by the given [code]path[/code] from the configuration.
6474
</description>
6575
</method>
6676
</methods>

‎modules/multiplayer/multiplayer_spawner.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void MultiplayerSpawner::_set_spawnable_scenes(const Vector<String> &p_scenes) {
126126
void MultiplayerSpawner::_bind_methods() {
127127
ClassDB::bind_method(D_METHOD("add_spawnable_scene", "path"), &MultiplayerSpawner::add_spawnable_scene);
128128
ClassDB::bind_method(D_METHOD("get_spawnable_scene_count"), &MultiplayerSpawner::get_spawnable_scene_count);
129-
ClassDB::bind_method(D_METHOD("get_spawnable_scene", "path"), &MultiplayerSpawner::get_spawnable_scene);
129+
ClassDB::bind_method(D_METHOD("get_spawnable_scene", "index"), &MultiplayerSpawner::get_spawnable_scene);
130130
ClassDB::bind_method(D_METHOD("clear_spawnable_scenes"), &MultiplayerSpawner::clear_spawnable_scenes);
131131

132132
ClassDB::bind_method(D_METHOD("_get_spawnable_scenes"), &MultiplayerSpawner::_get_spawnable_scenes);
@@ -146,8 +146,8 @@ void MultiplayerSpawner::_bind_methods() {
146146

147147
GDVIRTUAL_BIND(_spawn_custom, "data");
148148

149-
ADD_SIGNAL(MethodInfo("despawned", PropertyInfo(Variant::INT, "scene_id"), PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
150-
ADD_SIGNAL(MethodInfo("spawned", PropertyInfo(Variant::INT, "scene_id"), PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
149+
ADD_SIGNAL(MethodInfo("despawned", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
150+
ADD_SIGNAL(MethodInfo("spawned", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
151151
}
152152

153153
void MultiplayerSpawner::_update_spawn_node() {
@@ -277,12 +277,11 @@ Node *MultiplayerSpawner::instantiate_scene(int p_id) {
277277

278278
Node *MultiplayerSpawner::instantiate_custom(const Variant &p_data) {
279279
ERR_FAIL_COND_V_MSG(spawn_limit && spawn_limit <= tracked_nodes.size(), nullptr, "Spawn limit reached!");
280-
Object *obj = nullptr;
281280
Node *node = nullptr;
282-
if (GDVIRTUAL_CALL(_spawn_custom, p_data, obj)) {
283-
node = Object::cast_to<Node>(obj);
281+
if (GDVIRTUAL_CALL(_spawn_custom, p_data, node)) {
282+
return node;
284283
}
285-
return node;
284+
ERR_FAIL_V_MSG(nullptr, "Method '_spawn_custom' is not implemented on this peer.");
286285
}
287286

288287
Node *MultiplayerSpawner::spawn(const Variant &p_data) {

‎modules/multiplayer/multiplayer_spawner.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ class MultiplayerSpawner : public Node {
9191
void _get_property_list(List<PropertyInfo> *p_list) const;
9292
#endif
9393
public:
94-
Node *get_spawn_node() const { return spawn_node.is_valid() ? Object::cast_to<Node>(ObjectDB::get_instance(spawn_node)) : nullptr; }
94+
Node *get_spawn_node() const {
95+
return spawn_node.is_valid() ? Object::cast_to<Node>(ObjectDB::get_instance(spawn_node)) : nullptr;
96+
}
9597

9698
void add_spawnable_scene(const String &p_path);
9799
int get_spawnable_scene_count() const;
@@ -110,7 +112,7 @@ class MultiplayerSpawner : public Node {
110112
Node *instantiate_custom(const Variant &p_data);
111113
Node *instantiate_scene(int p_idx);
112114

113-
GDVIRTUAL1R(Object *, _spawn_custom, const Variant &);
115+
GDVIRTUAL1R(Node *, _spawn_custom, const Variant &);
114116

115117
MultiplayerSpawner() {}
116118
};

‎modules/multiplayer/scene_replication_interface.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ Error SceneReplicationInterface::on_spawn_receive(int p_from, const uint8_t *p_b
396396
pending_buffer_size = state_len;
397397
}
398398
parent->add_child(node);
399+
spawner->emit_signal(SNAME("spawned"), node);
400+
399401
pending_spawn = ObjectID();
400402
pending_buffer = nullptr;
401403
pending_buffer_size = 0;
@@ -411,10 +413,17 @@ Error SceneReplicationInterface::on_despawn_receive(int p_from, const uint8_t *p
411413
Error err = rep_state->peer_del_remote(p_from, net_id, &node);
412414
ERR_FAIL_COND_V(err != OK, err);
413415
ERR_FAIL_COND_V(!node, ERR_BUG);
416+
417+
MultiplayerSpawner *spawner = rep_state->get_spawner(node->get_instance_id());
418+
ERR_FAIL_COND_V(!spawner, ERR_DOES_NOT_EXIST);
419+
ERR_FAIL_COND_V(p_from != spawner->get_multiplayer_authority(), ERR_UNAUTHORIZED);
420+
414421
if (node->get_parent() != nullptr) {
415422
node->get_parent()->remove_child(node);
416423
}
417424
node->queue_delete();
425+
spawner->emit_signal(SNAME("despawned"), node);
426+
418427
return OK;
419428
}
420429

0 commit comments

Comments
 (0)