class_multiplayerapiextension.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.2/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/4.2/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 passthrough 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. func _poll():
  33. return base_multiplayer.poll()
  34. # Log RPC being made and forward it to the default multiplayer.
  35. func _rpc(peer: int, object: Object, method: StringName, args: Array) -> Error:
  36. print("Got RPC for %d: %s::%s(%s)" % [peer, object, method, args])
  37. return base_multiplayer.rpc(peer, object, method, args)
  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. # Log configuration remove. E.g. root path (nullptr, NodePath), replication (Node, Spawner|Synchronizer), custom.
  46. func _object_configuration_remove(object, config: Variant) -> Error:
  47. if config is MultiplayerSynchronizer:
  48. print("Removing synchronization configuration for %s. Synchronizer: %s" % [object, config])
  49. elif config is MultiplayerSpawner:
  50. print("Removing node %s from the spawn list. Spawner: %s" % [object, config])
  51. return base_multiplayer.object_configuration_remove(object, config)
  52. # These can be optional, but in our case we want to augment SceneMultiplayer, so forward everything.
  53. func _set_multiplayer_peer(p_peer: MultiplayerPeer):
  54. base_multiplayer.multiplayer_peer = p_peer
  55. func _get_multiplayer_peer() -> MultiplayerPeer:
  56. return base_multiplayer.multiplayer_peer
  57. func _get_unique_id() -> int:
  58. return base_multiplayer.get_unique_id()
  59. func _get_peer_ids() -> PackedInt32Array:
  60. return base_multiplayer.get_peers()
  61. 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>`:
  62. .. tabs::
  63. .. code-tab:: gdscript
  64. # autoload.gd
  65. func _enter_tree():
  66. # Sets our custom multiplayer as the main one in SceneTree.
  67. get_tree().set_multiplayer(LogMultiplayer.new())
  68. 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.
  69. .. rst-class:: classref-reftable-group
  70. Methods
  71. -------
  72. .. table::
  73. :widths: auto
  74. +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  75. | :ref:`MultiplayerPeer<class_MultiplayerPeer>` | :ref:`_get_multiplayer_peer<class_MultiplayerAPIExtension_private_method__get_multiplayer_peer>` **(** **)** |virtual| |
  76. +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  77. | :ref:`PackedInt32Array<class_PackedInt32Array>` | :ref:`_get_peer_ids<class_MultiplayerAPIExtension_private_method__get_peer_ids>` **(** **)** |virtual| |const| |
  78. +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  79. | :ref:`int<class_int>` | :ref:`_get_remote_sender_id<class_MultiplayerAPIExtension_private_method__get_remote_sender_id>` **(** **)** |virtual| |const| |
  80. +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  81. | :ref:`int<class_int>` | :ref:`_get_unique_id<class_MultiplayerAPIExtension_private_method__get_unique_id>` **(** **)** |virtual| |const| |
  82. +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  83. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`_object_configuration_add<class_MultiplayerAPIExtension_private_method__object_configuration_add>` **(** :ref:`Object<class_Object>` object, :ref:`Variant<class_Variant>` configuration **)** |virtual| |
  84. +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  85. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`_object_configuration_remove<class_MultiplayerAPIExtension_private_method__object_configuration_remove>` **(** :ref:`Object<class_Object>` object, :ref:`Variant<class_Variant>` configuration **)** |virtual| |
  86. +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  87. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`_poll<class_MultiplayerAPIExtension_private_method__poll>` **(** **)** |virtual| |
  88. +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  89. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`_rpc<class_MultiplayerAPIExtension_private_method__rpc>` **(** :ref:`int<class_int>` peer, :ref:`Object<class_Object>` object, :ref:`StringName<class_StringName>` method, :ref:`Array<class_Array>` args **)** |virtual| |
  90. +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  91. | void | :ref:`_set_multiplayer_peer<class_MultiplayerAPIExtension_private_method__set_multiplayer_peer>` **(** :ref:`MultiplayerPeer<class_MultiplayerPeer>` multiplayer_peer **)** |virtual| |
  92. +-------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  93. .. rst-class:: classref-section-separator
  94. ----
  95. .. rst-class:: classref-descriptions-group
  96. Method Descriptions
  97. -------------------
  98. .. _class_MultiplayerAPIExtension_private_method__get_multiplayer_peer:
  99. .. rst-class:: classref-method
  100. :ref:`MultiplayerPeer<class_MultiplayerPeer>` **_get_multiplayer_peer** **(** **)** |virtual|
  101. Called when the :ref:`MultiplayerAPI.multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>` is retrieved.
  102. .. rst-class:: classref-item-separator
  103. ----
  104. .. _class_MultiplayerAPIExtension_private_method__get_peer_ids:
  105. .. rst-class:: classref-method
  106. :ref:`PackedInt32Array<class_PackedInt32Array>` **_get_peer_ids** **(** **)** |virtual| |const|
  107. Callback for :ref:`MultiplayerAPI.get_peers<class_MultiplayerAPI_method_get_peers>`.
  108. .. rst-class:: classref-item-separator
  109. ----
  110. .. _class_MultiplayerAPIExtension_private_method__get_remote_sender_id:
  111. .. rst-class:: classref-method
  112. :ref:`int<class_int>` **_get_remote_sender_id** **(** **)** |virtual| |const|
  113. Callback for :ref:`MultiplayerAPI.get_remote_sender_id<class_MultiplayerAPI_method_get_remote_sender_id>`.
  114. .. rst-class:: classref-item-separator
  115. ----
  116. .. _class_MultiplayerAPIExtension_private_method__get_unique_id:
  117. .. rst-class:: classref-method
  118. :ref:`int<class_int>` **_get_unique_id** **(** **)** |virtual| |const|
  119. Callback for :ref:`MultiplayerAPI.get_unique_id<class_MultiplayerAPI_method_get_unique_id>`.
  120. .. rst-class:: classref-item-separator
  121. ----
  122. .. _class_MultiplayerAPIExtension_private_method__object_configuration_add:
  123. .. rst-class:: classref-method
  124. :ref:`Error<enum_@GlobalScope_Error>` **_object_configuration_add** **(** :ref:`Object<class_Object>` object, :ref:`Variant<class_Variant>` configuration **)** |virtual|
  125. Callback for :ref:`MultiplayerAPI.object_configuration_add<class_MultiplayerAPI_method_object_configuration_add>`.
  126. .. rst-class:: classref-item-separator
  127. ----
  128. .. _class_MultiplayerAPIExtension_private_method__object_configuration_remove:
  129. .. rst-class:: classref-method
  130. :ref:`Error<enum_@GlobalScope_Error>` **_object_configuration_remove** **(** :ref:`Object<class_Object>` object, :ref:`Variant<class_Variant>` configuration **)** |virtual|
  131. Callback for :ref:`MultiplayerAPI.object_configuration_remove<class_MultiplayerAPI_method_object_configuration_remove>`.
  132. .. rst-class:: classref-item-separator
  133. ----
  134. .. _class_MultiplayerAPIExtension_private_method__poll:
  135. .. rst-class:: classref-method
  136. :ref:`Error<enum_@GlobalScope_Error>` **_poll** **(** **)** |virtual|
  137. Callback for :ref:`MultiplayerAPI.poll<class_MultiplayerAPI_method_poll>`.
  138. .. rst-class:: classref-item-separator
  139. ----
  140. .. _class_MultiplayerAPIExtension_private_method__rpc:
  141. .. rst-class:: classref-method
  142. :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|
  143. Callback for :ref:`MultiplayerAPI.rpc<class_MultiplayerAPI_method_rpc>`.
  144. .. rst-class:: classref-item-separator
  145. ----
  146. .. _class_MultiplayerAPIExtension_private_method__set_multiplayer_peer:
  147. .. rst-class:: classref-method
  148. void **_set_multiplayer_peer** **(** :ref:`MultiplayerPeer<class_MultiplayerPeer>` multiplayer_peer **)** |virtual|
  149. Called when the :ref:`MultiplayerAPI.multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>` is set.
  150. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  151. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  152. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  153. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  154. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  155. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  156. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`