MultiplayerAPIExtension

Inherits: MultiplayerAPI < RefCounted < Object

Base class used for extending the MultiplayerAPI.

Description

This class can be used to augment or replace the default MultiplayerAPI implementation via script or extensions.

The following example augment the default implementation (SceneMultiplayer) by logging every RPC being made, and every object being configured for replication.

GDScript

  1. extends MultiplayerAPIExtension
  2. class_name LogMultiplayer
  3. # We want to augment the default SceneMultiplayer.
  4. var base_multiplayer = SceneMultiplayer.new()
  5. func _init():
  6. # Just passthrough base signals (copied to var to avoid cyclic reference)
  7. var cts = connected_to_server
  8. var cf = connection_failed
  9. var pc = peer_connected
  10. var pd = peer_disconnected
  11. base_multiplayer.connected_to_server.connect(func(): cts.emit())
  12. base_multiplayer.connection_failed.connect(func(): cf.emit())
  13. base_multiplayer.peer_connected.connect(func(id): pc.emit(id))
  14. base_multiplayer.peer_disconnected.connect(func(id): pd.emit(id))
  15. func _poll():
  16. return base_multiplayer.poll()
  17. # Log RPC being made and forward it to the default multiplayer.
  18. func _rpc(peer: int, object: Object, method: StringName, args: Array) -> Error:
  19. print("Got RPC for %d: %s::%s(%s)" % [peer, object, method, args])
  20. return base_multiplayer.rpc(peer, object, method, args)
  21. # Log configuration add. E.g. root path (nullptr, NodePath), replication (Node, Spawner|Synchronizer), custom.
  22. func _object_configuration_add(object, config: Variant) -> Error:
  23. if config is MultiplayerSynchronizer:
  24. print("Adding synchronization configuration for %s. Synchronizer: %s" % [object, config])
  25. elif config is MultiplayerSpawner:
  26. print("Adding node %s to the spawn list. Spawner: %s" % [object, config])
  27. return base_multiplayer.object_configuration_add(object, config)
  28. # Log configuration remove. E.g. root path (nullptr, NodePath), replication (Node, Spawner|Synchronizer), custom.
  29. func _object_configuration_remove(object, config: Variant) -> Error:
  30. if config is MultiplayerSynchronizer:
  31. print("Removing synchronization configuration for %s. Synchronizer: %s" % [object, config])
  32. elif config is MultiplayerSpawner:
  33. print("Removing node %s from the spawn list. Spawner: %s" % [object, config])
  34. return base_multiplayer.object_configuration_remove(object, config)
  35. # These can be optional, but in our case we want to augment SceneMultiplayer, so forward everything.
  36. func _set_multiplayer_peer(p_peer: MultiplayerPeer):
  37. base_multiplayer.multiplayer_peer = p_peer
  38. func _get_multiplayer_peer() -> MultiplayerPeer:
  39. return base_multiplayer.multiplayer_peer
  40. func _get_unique_id() -> int:
  41. return base_multiplayer.get_unique_id()
  42. func _get_peer_ids() -> PackedInt32Array:
  43. return base_multiplayer.get_peers()

Then in your main scene or in an autoload call SceneTree.set_multiplayer to start using your custom MultiplayerAPI:

GDScript

  1. # autoload.gd
  2. func _enter_tree():
  3. # Sets our custom multiplayer as the main one in SceneTree.
  4. get_tree().set_multiplayer(LogMultiplayer.new())

Native extensions can alternatively use the MultiplayerAPI.set_default_interface method during initialization to configure themselves as the default implementation.

Methods

MultiplayerPeer

_get_multiplayer_peer() virtual

PackedInt32Array

_get_peer_ids() virtual const

int

_get_remote_sender_id() virtual const

int

_get_unique_id() virtual const

Error

_object_configuration_add(object: Object, configuration: Variant) virtual

Error

_object_configuration_remove(object: Object, configuration: Variant) virtual

Error

_poll() virtual

Error

_rpc(peer: int, object: Object, method: StringName, args: Array) virtual

void

_set_multiplayer_peer(multiplayer_peer: MultiplayerPeer) virtual


Method Descriptions

MultiplayerPeer _get_multiplayer_peer() virtual 🔗

Called when the MultiplayerAPI.multiplayer_peer is retrieved.


PackedInt32Array _get_peer_ids() virtual const 🔗

Callback for MultiplayerAPI.get_peers.


int _get_remote_sender_id() virtual const 🔗

Callback for MultiplayerAPI.get_remote_sender_id.


int _get_unique_id() virtual const 🔗

Callback for MultiplayerAPI.get_unique_id.


Error _object_configuration_add(object: Object, configuration: Variant) virtual 🔗

Callback for MultiplayerAPI.object_configuration_add.


Error _object_configuration_remove(object: Object, configuration: Variant) virtual 🔗

Callback for MultiplayerAPI.object_configuration_remove.


Error _poll() virtual 🔗

Callback for MultiplayerAPI.poll.


Error _rpc(peer: int, object: Object, method: StringName, args: Array) virtual 🔗

Callback for MultiplayerAPI.rpc.


void _set_multiplayer_peer(multiplayer_peer: MultiplayerPeer) virtual 🔗

Called when the MultiplayerAPI.multiplayer_peer is set.


User-contributed notes

Please read the User-contributed notes policy before submitting a comment.

Previous Next