From d4dd859991205e6cecfa9a0553b89db47c983d0b Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Mon, 9 Aug 2021 22:46:23 +0200 Subject: [Net] MultiplayerReplicator with initial state. Move the former "spawnables" functions to a dedicated MultiplayerReplicator class. Support custom overrides in replicator. Spawn/despawn messages can now contain a state. The state can be automatically encoded/decoded by passing the desired object properties to `spawnable_config`. You can use script properties to optimize the state representation. 2 Callables can be also specified to completely override the default implementation for sending and receiving the spawn/despawn event. (9 bytes overhead, and there's room for improvement here). When using a custom implementation `spawn` and `despawn` can be called with any Object, `send_spawn`/`send_despawn` can receive any Variant as a state, and the path is not required. Two new functions, `spawn` and `despawn`, convey the implementation independent method for requesting a spawn/despawn of an Object, while `send_spawn` and `send_despawn` represent the more low-level send event for a Variant to be used by the custom implementations. --- doc/classes/MultiplayerAPI.xml | 91 +--------------------- doc/classes/MultiplayerReplicator.xml | 139 ++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 89 deletions(-) create mode 100644 doc/classes/MultiplayerReplicator.xml (limited to 'doc/classes') diff --git a/doc/classes/MultiplayerAPI.xml b/doc/classes/MultiplayerAPI.xml index b9f50ad02a..610b00efe9 100644 --- a/doc/classes/MultiplayerAPI.xml +++ b/doc/classes/MultiplayerAPI.xml @@ -66,34 +66,6 @@ Sends the given raw [code]bytes[/code] to a specific peer identified by [code]id[/code] (see [method MultiplayerPeer.set_target_peer]). Default ID is [code]0[/code], i.e. broadcast to all peers. - - - - - - - - Sends a despawn request for the scene identified by [code]scene_id[/code] to the given [code]peer_id[/code] (see [method MultiplayerPeer.set_target_peer]). If the scene is configured as [constant SPAWN_MODE_SERVER] (see [method spawnable_config]) and the request is sent by the server (see [method is_network_server]), the receiving peer(s) will automatically queue for deletion the node at [code]path[/code] and emit the signal [signal network_despawn]. In all other cases no deletion happens, and the signal [signal network_despawn_request] is emitted instead. - - - - - - - - - - Sends a spawn request for the scene identified by [code]scene_id[/code] to the given [code]peer_id[/code] (see [method MultiplayerPeer.set_target_peer]). If the scene is configured as [constant SPAWN_MODE_SERVER] (see [method spawnable_config]) and the request is sent by the server (see [method is_network_server]), the receiving peer(s) will automatically instantiate that scene, add it to the [SceneTree] at the given [code]path[/code] and emit the signal [signal network_spawn]. In all other cases no instantiation happens, and the signal [signal network_spawn_request] is emitted instead. - - - - - - - - Configures the MultiplayerAPI to track instances of the [PackedScene] idenfied by [code]scene_id[/code] (see [method ResourceLoader.get_resource_uid]) for the purpose of network replication. See [enum SpawnMode] for the possible configurations. - - @@ -106,6 +78,8 @@ If [code]true[/code], the MultiplayerAPI's [member network_peer] refuses new incoming connections. + + The root node to use for RPCs. Instead of an absolute path, a relative path will be used to find the node upon which the RPC should be executed. This effectively allows to have different branches of the scene tree to be managed by different MultiplayerAPI, allowing for example to run both client and server in the same scene. @@ -122,25 +96,6 @@ Emitted when this MultiplayerAPI's [member network_peer] fails to establish a connection to a server. Only emitted on clients. - - - - - - - Emitted on a client before deleting a local Node upon receiving a despawn request from the server. - - - - - - - - - - Emitted when a network despawn request has been received from a client, or for a [PackedScene] that has been configured as [constant SPAWN_MODE_CUSTOM]. - - @@ -160,39 +115,6 @@ Emitted when this MultiplayerAPI's [member network_peer] receive a [code]packet[/code] with custom data (see [method send_bytes]). ID is the peer ID of the peer that sent the packet. - - - - - - - Emitted on a client after a new Node is instantiated locally and added to the SceneTree upon receiving a spawn request from the server. - - - - - - - - - - Emitted when a network spawn request has been received from a client, or for a [PackedScene] that has been configured as [constant SPAWN_MODE_CUSTOM]. - - - - - - - Emitted when an instance of a [PackedScene] that has been configured for networking enters the [SceneTree]. See [method spawnable_config]. - - - - - - - Emitted when an instance of a [PackedScene] that has been configured for networking leaves the [SceneTree]. See [method spawnable_config]. - - Emitted when this MultiplayerAPI's [member network_peer] disconnects from server. Only emitted on clients. @@ -212,14 +134,5 @@ Used with [method Node.rpc_config] to set a method to be called or a property to be changed only on puppets for this node. Analogous to the [code]puppet[/code] keyword. Only accepts calls or property changes from the node's network master, see [method Node.set_network_master]. - - Used with [method spawnable_config] to identify a [PackedScene] that should not be replicated. - - - Used with [method spawnable_config] to identify a [PackedScene] that should be automatically replicated from server to clients. - - - Used with [method spawnable_config] to identify a [PackedScene] that can be manually replicated among peers. - diff --git a/doc/classes/MultiplayerReplicator.xml b/doc/classes/MultiplayerReplicator.xml new file mode 100644 index 0000000000..15029e181f --- /dev/null +++ b/doc/classes/MultiplayerReplicator.xml @@ -0,0 +1,139 @@ + + + + + + + + + + + + + + + + Decode the given [code]data[/code] representing a spawnable state into [code]object[/code] using the configuration associated with the provided [code]scene_id[/code]. This function is called automatically when a client receives a server spawn for a scene with [constant REPLICATION_MODE_SERVER]. See [method spawn_config]. + Tip: You may find this function useful in servers when parsing spawn requests from clients, or when implementing your own logic with [constant REPLICATION_MODE_CUSTOM]. + + + + + + + + + + + + + + + + Encode the given [code]object[/code] using the configuration associated with the provided [code]scene_id[/code]. This function is called automatically when the server spawns scenes with [constant REPLICATION_MODE_SERVER]. See [method spawn_config]. + Tip: You may find this function useful when requesting spawns from clients to server, or when implementing your own logic with [constant REPLICATION_MODE_CUSTOM]. + + + + + + + + + + Sends a despawn request for the scene identified by [code]scene_id[/code] to the given [code]peer_id[/code] (see [method MultiplayerPeer.set_target_peer]). If the scene is configured as [constant REPLICATION_MODE_SERVER] (see [method spawn_config]) and the request is sent by the server (see [method MultiplayerAPI.is_network_server]), the receiving peer(s) will automatically queue for deletion the node at [code]path[/code] and emit the signal [signal despawned]. In all other cases no deletion happens, and the signal [signal despawn_requested] is emitted instead. + + + + + + + + + + Sends a spawn request for the scene identified by [code]scene_id[/code] to the given [code]peer_id[/code] (see [method MultiplayerPeer.set_target_peer]). If the scene is configured as [constant REPLICATION_MODE_SERVER] (see [method spawn_config]) and the request is sent by the server (see [method MultiplayerAPI.is_network_server]), the receiving peer(s) will automatically instantiate that scene, add it to the [SceneTree] at the given [code]path[/code] and emit the signal [signal spawned]. In all other cases no instantiation happens, and the signal [signal spawn_requested] is emitted instead. + + + + + + + + + + + + + + + + + + + Configures the MultiplayerAPI to track instances of the [PackedScene] identified by [code]scene_id[/code] (see [method ResourceLoader.get_resource_uid]) for the purpose of network replication. When [code]mode[/code] is [constant REPLICATION_MODE_SERVER], the specified [code]properties[/code] will also be replicated to clients during the initial spawn. + Tip: You can use a custom property in the scene main script to return a customly optimized state representation. + + + + + + + + + + + + Emitted when a network despawn request has been received from a client, or for a [PackedScene] that has been configured as [constant REPLICATION_MODE_CUSTOM]. + + + + + + + Emitted on a client before deleting a local Node upon receiving a despawn request from the server. + + + + + + + Emitted when an instance of a [PackedScene] that has been configured for networking enters the [SceneTree]. See [method spawn_config]. + + + + + + + Emitted when an instance of a [PackedScene] that has been configured for networking leaves the [SceneTree]. See [method spawn_config]. + + + + + + + + + + Emitted when a network spawn request has been received from a client, or for a [PackedScene] that has been configured as [constant REPLICATION_MODE_CUSTOM]. + + + + + + + Emitted on a client after a new Node is instantiated locally and added to the SceneTree upon receiving a spawn request from the server. + + + + + + Used with [method spawn_config] to identify a [PackedScene] that should not be replicated. + + + Used with [method spawn_config] to identify a [PackedScene] that should be automatically replicated from server to clients. + + + Used with [method spawn_config] to identify a [PackedScene] that can be manually replicated among peers. + + + -- cgit v1.2.3