| 1 | <?xml version="1.0" encoding="UTF-8" ?> |
| 2 | <class name="MultiplayerAPIExtension" inherits="MultiplayerAPI" keywords="network" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> |
| 3 | <brief_description> |
| 4 | Base class used for extending the [MultiplayerAPI]. |
| 5 | </brief_description> |
| 6 | <description> |
| 7 | This class can be used to extend or replace the default [MultiplayerAPI] implementation via script or extensions. |
| 8 | The following example extend the default implementation ([SceneMultiplayer]) by logging every RPC being made, and every object being configured for replication. |
| 9 | [codeblocks] |
| 10 | [gdscript] |
| 11 | extends MultiplayerAPIExtension |
| 12 | class_name LogMultiplayer |
| 13 | |
| 14 | # We want to extend the default SceneMultiplayer. |
| 15 | var base_multiplayer = SceneMultiplayer.new() |
| 16 | |
| 17 | func _init(): |
| 18 | # Just passthrough base signals (copied to var to avoid cyclic reference) |
| 19 | var cts = connected_to_server |
| 20 | var cf = connection_failed |
| 21 | var sd = server_disconnected |
| 22 | var pc = peer_connected |
| 23 | var pd = peer_disconnected |
| 24 | base_multiplayer.connected_to_server.connect(func(): cts.emit()) |
| 25 | base_multiplayer.connection_failed.connect(func(): cf.emit()) |
| 26 | base_multiplayer.server_disconnected.connect(func(): sd.emit()) |
| 27 | base_multiplayer.peer_connected.connect(func(id): pc.emit(id)) |
| 28 | base_multiplayer.peer_disconnected.connect(func(id): pd.emit(id)) |
| 29 | |
| 30 | func _poll(): |
| 31 | return base_multiplayer.poll() |
| 32 | |
| 33 | # Log RPC being made and forward it to the default multiplayer. |
| 34 | func _rpc(peer: int, object: Object, method: StringName, args: Array) -> Error: |
| 35 | print("Got RPC for %d: %s::%s(%s)" % [peer, object, method, args]) |
| 36 | return base_multiplayer.rpc(peer, object, method, args) |
| 37 | |
| 38 | # Log configuration add. E.g. root path (nullptr, NodePath), replication (Node, Spawner|Synchronizer), custom. |
| 39 | func _object_configuration_add(object, config: Variant) -> Error: |
| 40 | if config is MultiplayerSynchronizer: |
| 41 | print("Adding synchronization configuration for %s. Synchronizer: %s" % [object, config]) |
| 42 | elif config is MultiplayerSpawner: |
| 43 | print("Adding node %s to the spawn list. Spawner: %s" % [object, config]) |
| 44 | return base_multiplayer.object_configuration_add(object, config) |
| 45 | |
| 46 | # Log configuration remove. E.g. root path (nullptr, NodePath), replication (Node, Spawner|Synchronizer), custom. |
| 47 | func _object_configuration_remove(object, config: Variant) -> Error: |
| 48 | if config is MultiplayerSynchronizer: |
| 49 | print("Removing synchronization configuration for %s. Synchronizer: %s" % [object, config]) |
| 50 | elif config is MultiplayerSpawner: |
| 51 | print("Removing node %s from the spawn list. Spawner: %s" % [object, config]) |
| 52 | return base_multiplayer.object_configuration_remove(object, config) |
| 53 | |
| 54 | # These can be optional, but in our case we want to extend SceneMultiplayer, so forward everything. |
| 55 | func _set_multiplayer_peer(p_peer: MultiplayerPeer): |
| 56 | base_multiplayer.multiplayer_peer = p_peer |
| 57 | |
| 58 | func _get_multiplayer_peer() -> MultiplayerPeer: |
| 59 | return base_multiplayer.multiplayer_peer |
| 60 | |
| 61 | func _get_unique_id() -> int: |
| 62 | return base_multiplayer.get_unique_id() |
| 63 | |
| 64 | func _get_remote_sender_id() -> int: |
| 65 | return base_multiplayer.get_remote_sender_id() |
| 66 | |
| 67 | func _get_peer_ids() -> PackedInt32Array: |
| 68 | return base_multiplayer.get_peers() |
| 69 | [/gdscript] |
| 70 | [/codeblocks] |
| 71 | Then in your main scene or in an autoload call [method SceneTree.set_multiplayer] to start using your custom [MultiplayerAPI]: |
| 72 | [codeblocks] |
| 73 | [gdscript] |
| 74 | # autoload.gd |
| 75 | func _enter_tree(): |
| 76 | # Sets our custom multiplayer as the main one in SceneTree. |
| 77 | get_tree().set_multiplayer(LogMultiplayer.new()) |
| 78 | [/gdscript] |
| 79 | [/codeblocks] |
| 80 | Native extensions can alternatively use the [method MultiplayerAPI.set_default_interface] method during initialization to configure themselves as the default implementation. |
| 81 | </description> |
| 82 | <tutorials> |
| 83 | </tutorials> |
| 84 | <methods> |
| 85 | <method name="_get_multiplayer_peer" qualifiers="virtual"> |
| 86 | <return type="MultiplayerPeer" /> |
| 87 | <description> |
| 88 | Called when the [member MultiplayerAPI.multiplayer_peer] is retrieved. |
| 89 | </description> |
| 90 | </method> |
| 91 | <method name="_get_peer_ids" qualifiers="virtual const"> |
| 92 | <return type="PackedInt32Array" /> |
| 93 | <description> |
| 94 | Callback for [method MultiplayerAPI.get_peers]. |
| 95 | </description> |
| 96 | </method> |
| 97 | <method name="_get_remote_sender_id" qualifiers="virtual const"> |
| 98 | <return type="int" /> |
| 99 | <description> |
| 100 | Callback for [method MultiplayerAPI.get_remote_sender_id]. |
| 101 | </description> |
| 102 | </method> |
| 103 | <method name="_get_unique_id" qualifiers="virtual const"> |
| 104 | <return type="int" /> |
| 105 | <description> |
| 106 | Callback for [method MultiplayerAPI.get_unique_id]. |
| 107 | </description> |
| 108 | </method> |
| 109 | <method name="_object_configuration_add" qualifiers="virtual"> |
| 110 | <return type="int" enum="Error" /> |
| 111 | <param index="0" name="object" type="Object" /> |
| 112 | <param index="1" name="configuration" type="Variant" /> |
| 113 | <description> |
| 114 | Callback for [method MultiplayerAPI.object_configuration_add]. |
| 115 | </description> |
| 116 | </method> |
| 117 | <method name="_object_configuration_remove" qualifiers="virtual"> |
| 118 | <return type="int" enum="Error" /> |
| 119 | <param index="0" name="object" type="Object" /> |
| 120 | <param index="1" name="configuration" type="Variant" /> |
| 121 | <description> |
| 122 | Callback for [method MultiplayerAPI.object_configuration_remove]. |
| 123 | </description> |
| 124 | </method> |
| 125 | <method name="_poll" qualifiers="virtual"> |
| 126 | <return type="int" enum="Error" /> |
| 127 | <description> |
| 128 | Callback for [method MultiplayerAPI.poll]. |
| 129 | </description> |
| 130 | </method> |
| 131 | <method name="_rpc" qualifiers="virtual"> |
| 132 | <return type="int" enum="Error" /> |
| 133 | <param index="0" name="peer" type="int" /> |
| 134 | <param index="1" name="object" type="Object" /> |
| 135 | <param index="2" name="method" type="StringName" /> |
| 136 | <param index="3" name="args" type="Array" /> |
| 137 | <description> |
| 138 | Callback for [method MultiplayerAPI.rpc]. |
| 139 | </description> |
| 140 | </method> |
| 141 | <method name="_set_multiplayer_peer" qualifiers="virtual"> |
| 142 | <return type="void" /> |
| 143 | <param index="0" name="multiplayer_peer" type="MultiplayerPeer" /> |
| 144 | <description> |
| 145 | Called when the [member MultiplayerAPI.multiplayer_peer] is set. |
| 146 | </description> |
| 147 | </method> |
| 148 | </methods> |
| 149 | </class> |