class_multiplayerapiextension.rst 14 KB

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