class_multiplayerapiextension.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/4.0/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/4.0/doc/classes/MultiplayerAPIExtension.xml.
  6. .. _class_MultiplayerAPIExtension:
  7. MultiplayerAPIExtension
  8. =======================
  9. **Inherits:** :ref:`MultiplayerAPI<class_MultiplayerAPI>` **<** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. Base class used for extending the :ref:`MultiplayerAPI<class_MultiplayerAPI>`.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. This class can be used to augment or replace the default :ref:`MultiplayerAPI<class_MultiplayerAPI>` implementation via script or extensions.
  15. The following example augment the default implementation (:ref:`SceneMultiplayer<class_SceneMultiplayer>`) by logging every RPC being made, and every object being configured for replication.
  16. .. tabs::
  17. .. code-tab:: gdscript
  18. extends MultiplayerAPIExtension
  19. class_name LogMultiplayer
  20. # We want to augment the default SceneMultiplayer.
  21. var base_multiplayer = SceneMultiplayer.new()
  22. func _init():
  23. # Just passthourgh base signals (copied to var to avoid cyclic reference)
  24. var cts = connected_to_server
  25. var cf = connection_failed
  26. var pc = peer_connected
  27. var pd = peer_disconnected
  28. base_multiplayer.connected_to_server.connect(func(): cts.emit())
  29. base_multiplayer.connection_failed.connect(func(): cf.emit())
  30. base_multiplayer.peer_connected.connect(func(id): pc.emit(id))
  31. base_multiplayer.peer_disconnected.connect(func(id): pd.emit(id))
  32. # Log RPC being made and forward it to the default multiplayer.
  33. func _rpc(peer: int, object: Object, method: StringName, args: Array) -> int: # Error
  34. print("Got RPC for %d: %s::%s(%s)" % [peer, object, method, args])
  35. return base_multiplayer.rpc(peer, object, method, args)
  36. # Log configuration add. E.g. root path (nullptr, NodePath), replication (Node, Spawner|Synchronizer), custom.
  37. func _object_configuration_add(object, config: Variant) -> int: # Error
  38. if config is MultiplayerSynchronizer:
  39. print("Adding synchronization configuration for %s. Synchronizer: %s" % [object, config])
  40. elif config is MultiplayerSpawner:
  41. print("Adding node %s to the spawn list. Spawner: %s" % [object, config])
  42. return base_multiplayer.object_configuration_add(object, config)
  43. # Log configuration remove. E.g. root path (nullptr, NodePath), replication (Node, Spawner|Synchronizer), custom.
  44. func _object_configuration_remove(object, config: Variant) -> int: # Error
  45. if config is MultiplayerSynchronizer:
  46. print("Removing synchronization configuration for %s. Synchronizer: %s" % [object, config])
  47. elif config is MultiplayerSpawner:
  48. print("Removing node %s from the spawn list. Spawner: %s" % [object, config])
  49. return base_multiplayer.object_configuration_remove(object, config)
  50. # These can be optional, but in our case we want to augment SceneMultiplayer, so forward everything.
  51. func _set_multiplayer_peer(p_peer: MultiplayerPeer):
  52. base_multiplayer.multiplayer_peer = p_peer
  53. func _get_multiplayer_peer() -> MultiplayerPeer:
  54. return base_multiplayer.multiplayer_peer
  55. func _get_unique_id() -> int:
  56. return base_multiplayer.get_unique_id()
  57. func _get_peer_ids() -> PackedInt32Array:
  58. return base_multiplayer.get_peers()
  59. Then in your main scene or in an autoload call :ref:`SceneTree.set_multiplayer<class_SceneTree_method_set_multiplayer>` to start using your custom :ref:`MultiplayerAPI<class_MultiplayerAPI>`:
  60. .. tabs::
  61. .. code-tab:: gdscript
  62. # autoload.gd
  63. func _enter_tree():
  64. # Sets our custom multiplayer as the main one in SceneTree.
  65. get_tree().set_multiplayer(LogMultiplayer.new())
  66. Native extensions can alternatively use the :ref:`MultiplayerAPI.set_default_interface<class_MultiplayerAPI_method_set_default_interface>` method during initialization to configure themselves as the default implementation.
  67. .. rst-class:: classref-reftable-group
  68. Methods
  69. -------
  70. .. table::
  71. :widths: auto
  72. +-------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  73. | :ref:`MultiplayerPeer<class_MultiplayerPeer>` | :ref:`_get_multiplayer_peer<class_MultiplayerAPIExtension_method__get_multiplayer_peer>` **(** **)** |virtual| |
  74. +-------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  75. | :ref:`PackedInt32Array<class_PackedInt32Array>` | :ref:`_get_peer_ids<class_MultiplayerAPIExtension_method__get_peer_ids>` **(** **)** |virtual| |const| |
  76. +-------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  77. | :ref:`int<class_int>` | :ref:`_get_remote_sender_id<class_MultiplayerAPIExtension_method__get_remote_sender_id>` **(** **)** |virtual| |const| |
  78. +-------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  79. | :ref:`int<class_int>` | :ref:`_get_unique_id<class_MultiplayerAPIExtension_method__get_unique_id>` **(** **)** |virtual| |const| |
  80. +-------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  81. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`_object_configuration_add<class_MultiplayerAPIExtension_method__object_configuration_add>` **(** :ref:`Object<class_Object>` object, :ref:`Variant<class_Variant>` configuration **)** |virtual| |
  82. +-------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  83. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`_object_configuration_remove<class_MultiplayerAPIExtension_method__object_configuration_remove>` **(** :ref:`Object<class_Object>` object, :ref:`Variant<class_Variant>` configuration **)** |virtual| |
  84. +-------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  85. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`_poll<class_MultiplayerAPIExtension_method__poll>` **(** **)** |virtual| |
  86. +-------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  87. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`_rpc<class_MultiplayerAPIExtension_method__rpc>` **(** :ref:`int<class_int>` peer, :ref:`Object<class_Object>` object, :ref:`StringName<class_StringName>` method, :ref:`Array<class_Array>` args **)** |virtual| |
  88. +-------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  89. | void | :ref:`_set_multiplayer_peer<class_MultiplayerAPIExtension_method__set_multiplayer_peer>` **(** :ref:`MultiplayerPeer<class_MultiplayerPeer>` multiplayer_peer **)** |virtual| |
  90. +-------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  91. .. rst-class:: classref-section-separator
  92. ----
  93. .. rst-class:: classref-descriptions-group
  94. Method Descriptions
  95. -------------------
  96. .. _class_MultiplayerAPIExtension_method__get_multiplayer_peer:
  97. .. rst-class:: classref-method
  98. :ref:`MultiplayerPeer<class_MultiplayerPeer>` **_get_multiplayer_peer** **(** **)** |virtual|
  99. Called when the :ref:`MultiplayerAPI.multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>` is retrieved.
  100. .. rst-class:: classref-item-separator
  101. ----
  102. .. _class_MultiplayerAPIExtension_method__get_peer_ids:
  103. .. rst-class:: classref-method
  104. :ref:`PackedInt32Array<class_PackedInt32Array>` **_get_peer_ids** **(** **)** |virtual| |const|
  105. Callback for :ref:`MultiplayerAPI.get_peers<class_MultiplayerAPI_method_get_peers>`.
  106. .. rst-class:: classref-item-separator
  107. ----
  108. .. _class_MultiplayerAPIExtension_method__get_remote_sender_id:
  109. .. rst-class:: classref-method
  110. :ref:`int<class_int>` **_get_remote_sender_id** **(** **)** |virtual| |const|
  111. Callback for :ref:`MultiplayerAPI.get_remote_sender_id<class_MultiplayerAPI_method_get_remote_sender_id>`.
  112. .. rst-class:: classref-item-separator
  113. ----
  114. .. _class_MultiplayerAPIExtension_method__get_unique_id:
  115. .. rst-class:: classref-method
  116. :ref:`int<class_int>` **_get_unique_id** **(** **)** |virtual| |const|
  117. Callback for :ref:`MultiplayerAPI.get_unique_id<class_MultiplayerAPI_method_get_unique_id>`.
  118. .. rst-class:: classref-item-separator
  119. ----
  120. .. _class_MultiplayerAPIExtension_method__object_configuration_add:
  121. .. rst-class:: classref-method
  122. :ref:`Error<enum_@GlobalScope_Error>` **_object_configuration_add** **(** :ref:`Object<class_Object>` object, :ref:`Variant<class_Variant>` configuration **)** |virtual|
  123. Callback for :ref:`MultiplayerAPI.object_configuration_add<class_MultiplayerAPI_method_object_configuration_add>`.
  124. .. rst-class:: classref-item-separator
  125. ----
  126. .. _class_MultiplayerAPIExtension_method__object_configuration_remove:
  127. .. rst-class:: classref-method
  128. :ref:`Error<enum_@GlobalScope_Error>` **_object_configuration_remove** **(** :ref:`Object<class_Object>` object, :ref:`Variant<class_Variant>` configuration **)** |virtual|
  129. Callback for :ref:`MultiplayerAPI.object_configuration_remove<class_MultiplayerAPI_method_object_configuration_remove>`.
  130. .. rst-class:: classref-item-separator
  131. ----
  132. .. _class_MultiplayerAPIExtension_method__poll:
  133. .. rst-class:: classref-method
  134. :ref:`Error<enum_@GlobalScope_Error>` **_poll** **(** **)** |virtual|
  135. Callback for :ref:`MultiplayerAPI.poll<class_MultiplayerAPI_method_poll>`.
  136. .. rst-class:: classref-item-separator
  137. ----
  138. .. _class_MultiplayerAPIExtension_method__rpc:
  139. .. rst-class:: classref-method
  140. :ref:`Error<enum_@GlobalScope_Error>` **_rpc** **(** :ref:`int<class_int>` peer, :ref:`Object<class_Object>` object, :ref:`StringName<class_StringName>` method, :ref:`Array<class_Array>` args **)** |virtual|
  141. Callback for :ref:`MultiplayerAPI.rpc<class_MultiplayerAPI_method_rpc>`.
  142. .. rst-class:: classref-item-separator
  143. ----
  144. .. _class_MultiplayerAPIExtension_method__set_multiplayer_peer:
  145. .. rst-class:: classref-method
  146. void **_set_multiplayer_peer** **(** :ref:`MultiplayerPeer<class_MultiplayerPeer>` multiplayer_peer **)** |virtual|
  147. Called when the :ref:`MultiplayerAPI.multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>` is set.
  148. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  149. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  150. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  151. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  152. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  153. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`