class_multiplayerapi.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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/MultiplayerAPI.xml.
  8. .. _class_MultiplayerAPI:
  9. MultiplayerAPI
  10. ==============
  11. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  12. **Inherited By:** :ref:`MultiplayerAPIExtension<class_MultiplayerAPIExtension>`, :ref:`SceneMultiplayer<class_SceneMultiplayer>`
  13. High-level multiplayer API interface.
  14. .. rst-class:: classref-introduction-group
  15. Description
  16. -----------
  17. Base class for high-level multiplayer API implementations. See also :ref:`MultiplayerPeer<class_MultiplayerPeer>`.
  18. By default, :ref:`SceneTree<class_SceneTree>` has a reference to an implementation of this class and uses it to provide multiplayer capabilities (i.e. RPCs) across the whole scene.
  19. It is possible to override the MultiplayerAPI instance used by specific tree branches by calling the :ref:`SceneTree.set_multiplayer<class_SceneTree_method_set_multiplayer>` method, effectively allowing to run both client and server in the same scene.
  20. It is also possible to extend or replace the default implementation via scripting or native extensions. See :ref:`MultiplayerAPIExtension<class_MultiplayerAPIExtension>` for details about extensions, :ref:`SceneMultiplayer<class_SceneMultiplayer>` for the details about the default implementation.
  21. .. rst-class:: classref-reftable-group
  22. Properties
  23. ----------
  24. .. table::
  25. :widths: auto
  26. +-----------------------------------------------+-------------------------------------------------------------------------+
  27. | :ref:`MultiplayerPeer<class_MultiplayerPeer>` | :ref:`multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>` |
  28. +-----------------------------------------------+-------------------------------------------------------------------------+
  29. .. rst-class:: classref-reftable-group
  30. Methods
  31. -------
  32. .. table::
  33. :widths: auto
  34. +-------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  35. | :ref:`MultiplayerAPI<class_MultiplayerAPI>` | :ref:`create_default_interface<class_MultiplayerAPI_method_create_default_interface>`\ (\ ) |static| |
  36. +-------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  37. | :ref:`StringName<class_StringName>` | :ref:`get_default_interface<class_MultiplayerAPI_method_get_default_interface>`\ (\ ) |static| |
  38. +-------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  39. | :ref:`PackedInt32Array<class_PackedInt32Array>` | :ref:`get_peers<class_MultiplayerAPI_method_get_peers>`\ (\ ) |
  40. +-------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  41. | :ref:`int<class_int>` | :ref:`get_remote_sender_id<class_MultiplayerAPI_method_get_remote_sender_id>`\ (\ ) |
  42. +-------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  43. | :ref:`int<class_int>` | :ref:`get_unique_id<class_MultiplayerAPI_method_get_unique_id>`\ (\ ) |
  44. +-------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  45. | :ref:`bool<class_bool>` | :ref:`has_multiplayer_peer<class_MultiplayerAPI_method_has_multiplayer_peer>`\ (\ ) |
  46. +-------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  47. | :ref:`bool<class_bool>` | :ref:`is_server<class_MultiplayerAPI_method_is_server>`\ (\ ) |
  48. +-------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  49. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`object_configuration_add<class_MultiplayerAPI_method_object_configuration_add>`\ (\ object\: :ref:`Object<class_Object>`, configuration\: :ref:`Variant<class_Variant>`\ ) |
  50. +-------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  51. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`object_configuration_remove<class_MultiplayerAPI_method_object_configuration_remove>`\ (\ object\: :ref:`Object<class_Object>`, configuration\: :ref:`Variant<class_Variant>`\ ) |
  52. +-------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  53. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`poll<class_MultiplayerAPI_method_poll>`\ (\ ) |
  54. +-------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  55. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`rpc<class_MultiplayerAPI_method_rpc>`\ (\ peer\: :ref:`int<class_int>`, object\: :ref:`Object<class_Object>`, method\: :ref:`StringName<class_StringName>`, arguments\: :ref:`Array<class_Array>` = []\ ) |
  56. +-------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  57. | |void| | :ref:`set_default_interface<class_MultiplayerAPI_method_set_default_interface>`\ (\ interface_name\: :ref:`StringName<class_StringName>`\ ) |static| |
  58. +-------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  59. .. rst-class:: classref-section-separator
  60. ----
  61. .. rst-class:: classref-descriptions-group
  62. Signals
  63. -------
  64. .. _class_MultiplayerAPI_signal_connected_to_server:
  65. .. rst-class:: classref-signal
  66. **connected_to_server**\ (\ ) :ref:`🔗<class_MultiplayerAPI_signal_connected_to_server>`
  67. Emitted when this MultiplayerAPI's :ref:`multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>` successfully connected to a server. Only emitted on clients.
  68. .. rst-class:: classref-item-separator
  69. ----
  70. .. _class_MultiplayerAPI_signal_connection_failed:
  71. .. rst-class:: classref-signal
  72. **connection_failed**\ (\ ) :ref:`🔗<class_MultiplayerAPI_signal_connection_failed>`
  73. Emitted when this MultiplayerAPI's :ref:`multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>` fails to establish a connection to a server. Only emitted on clients.
  74. .. rst-class:: classref-item-separator
  75. ----
  76. .. _class_MultiplayerAPI_signal_peer_connected:
  77. .. rst-class:: classref-signal
  78. **peer_connected**\ (\ id\: :ref:`int<class_int>`\ ) :ref:`🔗<class_MultiplayerAPI_signal_peer_connected>`
  79. Emitted when this MultiplayerAPI's :ref:`multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>` connects with a new peer. ID is the peer ID of the new peer. Clients get notified when other clients connect to the same server. Upon connecting to a server, a client also receives this signal for the server (with ID being 1).
  80. .. rst-class:: classref-item-separator
  81. ----
  82. .. _class_MultiplayerAPI_signal_peer_disconnected:
  83. .. rst-class:: classref-signal
  84. **peer_disconnected**\ (\ id\: :ref:`int<class_int>`\ ) :ref:`🔗<class_MultiplayerAPI_signal_peer_disconnected>`
  85. Emitted when this MultiplayerAPI's :ref:`multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>` disconnects from a peer. Clients get notified when other clients disconnect from the same server.
  86. .. rst-class:: classref-item-separator
  87. ----
  88. .. _class_MultiplayerAPI_signal_server_disconnected:
  89. .. rst-class:: classref-signal
  90. **server_disconnected**\ (\ ) :ref:`🔗<class_MultiplayerAPI_signal_server_disconnected>`
  91. Emitted when this MultiplayerAPI's :ref:`multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>` disconnects from server. Only emitted on clients.
  92. .. rst-class:: classref-section-separator
  93. ----
  94. .. rst-class:: classref-descriptions-group
  95. Enumerations
  96. ------------
  97. .. _enum_MultiplayerAPI_RPCMode:
  98. .. rst-class:: classref-enumeration
  99. enum **RPCMode**: :ref:`🔗<enum_MultiplayerAPI_RPCMode>`
  100. .. _class_MultiplayerAPI_constant_RPC_MODE_DISABLED:
  101. .. rst-class:: classref-enumeration-constant
  102. :ref:`RPCMode<enum_MultiplayerAPI_RPCMode>` **RPC_MODE_DISABLED** = ``0``
  103. Used with :ref:`Node.rpc_config<class_Node_method_rpc_config>` to disable a method or property for all RPC calls, making it unavailable. Default for all methods.
  104. .. _class_MultiplayerAPI_constant_RPC_MODE_ANY_PEER:
  105. .. rst-class:: classref-enumeration-constant
  106. :ref:`RPCMode<enum_MultiplayerAPI_RPCMode>` **RPC_MODE_ANY_PEER** = ``1``
  107. Used with :ref:`Node.rpc_config<class_Node_method_rpc_config>` to set a method to be callable remotely by any peer. Analogous to the ``@rpc("any_peer")`` annotation. Calls are accepted from all remote peers, no matter if they are node's authority or not.
  108. .. _class_MultiplayerAPI_constant_RPC_MODE_AUTHORITY:
  109. .. rst-class:: classref-enumeration-constant
  110. :ref:`RPCMode<enum_MultiplayerAPI_RPCMode>` **RPC_MODE_AUTHORITY** = ``2``
  111. Used with :ref:`Node.rpc_config<class_Node_method_rpc_config>` to set a method to be callable remotely only by the current multiplayer authority (which is the server by default). Analogous to the ``@rpc("authority")`` annotation. See :ref:`Node.set_multiplayer_authority<class_Node_method_set_multiplayer_authority>`.
  112. .. rst-class:: classref-section-separator
  113. ----
  114. .. rst-class:: classref-descriptions-group
  115. Property Descriptions
  116. ---------------------
  117. .. _class_MultiplayerAPI_property_multiplayer_peer:
  118. .. rst-class:: classref-property
  119. :ref:`MultiplayerPeer<class_MultiplayerPeer>` **multiplayer_peer** :ref:`🔗<class_MultiplayerAPI_property_multiplayer_peer>`
  120. .. rst-class:: classref-property-setget
  121. - |void| **set_multiplayer_peer**\ (\ value\: :ref:`MultiplayerPeer<class_MultiplayerPeer>`\ )
  122. - :ref:`MultiplayerPeer<class_MultiplayerPeer>` **get_multiplayer_peer**\ (\ )
  123. The peer object to handle the RPC system (effectively enabling networking when set). Depending on the peer itself, the MultiplayerAPI will become a network server (check with :ref:`is_server<class_MultiplayerAPI_method_is_server>`) and will set root node's network mode to authority, or it will become a regular client peer. All child nodes are set to inherit the network mode by default. Handling of networking-related events (connection, disconnection, new clients) is done by connecting to MultiplayerAPI's signals.
  124. .. rst-class:: classref-section-separator
  125. ----
  126. .. rst-class:: classref-descriptions-group
  127. Method Descriptions
  128. -------------------
  129. .. _class_MultiplayerAPI_method_create_default_interface:
  130. .. rst-class:: classref-method
  131. :ref:`MultiplayerAPI<class_MultiplayerAPI>` **create_default_interface**\ (\ ) |static| :ref:`🔗<class_MultiplayerAPI_method_create_default_interface>`
  132. Returns a new instance of the default MultiplayerAPI.
  133. .. rst-class:: classref-item-separator
  134. ----
  135. .. _class_MultiplayerAPI_method_get_default_interface:
  136. .. rst-class:: classref-method
  137. :ref:`StringName<class_StringName>` **get_default_interface**\ (\ ) |static| :ref:`🔗<class_MultiplayerAPI_method_get_default_interface>`
  138. Returns the default MultiplayerAPI implementation class name. This is usually ``"SceneMultiplayer"`` when :ref:`SceneMultiplayer<class_SceneMultiplayer>` is available. See :ref:`set_default_interface<class_MultiplayerAPI_method_set_default_interface>`.
  139. .. rst-class:: classref-item-separator
  140. ----
  141. .. _class_MultiplayerAPI_method_get_peers:
  142. .. rst-class:: classref-method
  143. :ref:`PackedInt32Array<class_PackedInt32Array>` **get_peers**\ (\ ) :ref:`🔗<class_MultiplayerAPI_method_get_peers>`
  144. Returns the peer IDs of all connected peers of this MultiplayerAPI's :ref:`multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>`.
  145. .. rst-class:: classref-item-separator
  146. ----
  147. .. _class_MultiplayerAPI_method_get_remote_sender_id:
  148. .. rst-class:: classref-method
  149. :ref:`int<class_int>` **get_remote_sender_id**\ (\ ) :ref:`🔗<class_MultiplayerAPI_method_get_remote_sender_id>`
  150. Returns the sender's peer ID for the RPC currently being executed.
  151. \ **Note:** This method returns ``0`` when called outside of an RPC. As such, the original peer ID may be lost when code execution is delayed (such as with GDScript's ``await`` keyword).
  152. .. rst-class:: classref-item-separator
  153. ----
  154. .. _class_MultiplayerAPI_method_get_unique_id:
  155. .. rst-class:: classref-method
  156. :ref:`int<class_int>` **get_unique_id**\ (\ ) :ref:`🔗<class_MultiplayerAPI_method_get_unique_id>`
  157. Returns the unique peer ID of this MultiplayerAPI's :ref:`multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>`.
  158. .. rst-class:: classref-item-separator
  159. ----
  160. .. _class_MultiplayerAPI_method_has_multiplayer_peer:
  161. .. rst-class:: classref-method
  162. :ref:`bool<class_bool>` **has_multiplayer_peer**\ (\ ) :ref:`🔗<class_MultiplayerAPI_method_has_multiplayer_peer>`
  163. Returns ``true`` if there is a :ref:`multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>` set.
  164. .. rst-class:: classref-item-separator
  165. ----
  166. .. _class_MultiplayerAPI_method_is_server:
  167. .. rst-class:: classref-method
  168. :ref:`bool<class_bool>` **is_server**\ (\ ) :ref:`🔗<class_MultiplayerAPI_method_is_server>`
  169. Returns ``true`` if this MultiplayerAPI's :ref:`multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>` is valid and in server mode (listening for connections).
  170. .. rst-class:: classref-item-separator
  171. ----
  172. .. _class_MultiplayerAPI_method_object_configuration_add:
  173. .. rst-class:: classref-method
  174. :ref:`Error<enum_@GlobalScope_Error>` **object_configuration_add**\ (\ object\: :ref:`Object<class_Object>`, configuration\: :ref:`Variant<class_Variant>`\ ) :ref:`🔗<class_MultiplayerAPI_method_object_configuration_add>`
  175. Notifies the MultiplayerAPI of a new ``configuration`` for the given ``object``. This method is used internally by :ref:`SceneTree<class_SceneTree>` to configure the root path for this MultiplayerAPI (passing ``null`` and a valid :ref:`NodePath<class_NodePath>` as ``configuration``). This method can be further used by MultiplayerAPI implementations to provide additional features, refer to specific implementation (e.g. :ref:`SceneMultiplayer<class_SceneMultiplayer>`) for details on how they use it.
  176. \ **Note:** This method is mostly relevant when extending or overriding the MultiplayerAPI behavior via :ref:`MultiplayerAPIExtension<class_MultiplayerAPIExtension>`.
  177. .. rst-class:: classref-item-separator
  178. ----
  179. .. _class_MultiplayerAPI_method_object_configuration_remove:
  180. .. rst-class:: classref-method
  181. :ref:`Error<enum_@GlobalScope_Error>` **object_configuration_remove**\ (\ object\: :ref:`Object<class_Object>`, configuration\: :ref:`Variant<class_Variant>`\ ) :ref:`🔗<class_MultiplayerAPI_method_object_configuration_remove>`
  182. Notifies the MultiplayerAPI to remove a ``configuration`` for the given ``object``. This method is used internally by :ref:`SceneTree<class_SceneTree>` to configure the root path for this MultiplayerAPI (passing ``null`` and an empty :ref:`NodePath<class_NodePath>` as ``configuration``). This method can be further used by MultiplayerAPI implementations to provide additional features, refer to specific implementation (e.g. :ref:`SceneMultiplayer<class_SceneMultiplayer>`) for details on how they use it.
  183. \ **Note:** This method is mostly relevant when extending or overriding the MultiplayerAPI behavior via :ref:`MultiplayerAPIExtension<class_MultiplayerAPIExtension>`.
  184. .. rst-class:: classref-item-separator
  185. ----
  186. .. _class_MultiplayerAPI_method_poll:
  187. .. rst-class:: classref-method
  188. :ref:`Error<enum_@GlobalScope_Error>` **poll**\ (\ ) :ref:`🔗<class_MultiplayerAPI_method_poll>`
  189. Method used for polling the MultiplayerAPI. You only need to worry about this if you set :ref:`SceneTree.multiplayer_poll<class_SceneTree_property_multiplayer_poll>` to ``false``. By default, :ref:`SceneTree<class_SceneTree>` will poll its MultiplayerAPI(s) for you.
  190. \ **Note:** This method results in RPCs being called, so they will be executed in the same context of this function (e.g. ``_process``, ``physics``, :ref:`Thread<class_Thread>`).
  191. .. rst-class:: classref-item-separator
  192. ----
  193. .. _class_MultiplayerAPI_method_rpc:
  194. .. rst-class:: classref-method
  195. :ref:`Error<enum_@GlobalScope_Error>` **rpc**\ (\ peer\: :ref:`int<class_int>`, object\: :ref:`Object<class_Object>`, method\: :ref:`StringName<class_StringName>`, arguments\: :ref:`Array<class_Array>` = []\ ) :ref:`🔗<class_MultiplayerAPI_method_rpc>`
  196. Sends an RPC to the target ``peer``. The given ``method`` will be called on the remote ``object`` with the provided ``arguments``. The RPC may also be called locally depending on the implementation and RPC configuration. See :ref:`Node.rpc<class_Node_method_rpc>` and :ref:`Node.rpc_config<class_Node_method_rpc_config>`.
  197. \ **Note:** Prefer using :ref:`Node.rpc<class_Node_method_rpc>`, :ref:`Node.rpc_id<class_Node_method_rpc_id>`, or ``my_method.rpc(peer, arg1, arg2, ...)`` (in GDScript), since they are faster. This method is mostly useful in conjunction with :ref:`MultiplayerAPIExtension<class_MultiplayerAPIExtension>` when extending or replacing the multiplayer capabilities.
  198. .. rst-class:: classref-item-separator
  199. ----
  200. .. _class_MultiplayerAPI_method_set_default_interface:
  201. .. rst-class:: classref-method
  202. |void| **set_default_interface**\ (\ interface_name\: :ref:`StringName<class_StringName>`\ ) |static| :ref:`🔗<class_MultiplayerAPI_method_set_default_interface>`
  203. Sets the default MultiplayerAPI implementation class. This method can be used by modules and extensions to configure which implementation will be used by :ref:`SceneTree<class_SceneTree>` when the engine starts.
  204. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  205. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  206. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  207. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  208. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  209. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  210. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  211. .. |void| replace:: :abbr:`void (No return value.)`