class_multiplayerapi.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/MultiplayerAPI.xml.
  6. .. _class_MultiplayerAPI:
  7. MultiplayerAPI
  8. ==============
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. **Inherited By:** :ref:`MultiplayerAPIExtension<class_MultiplayerAPIExtension>`, :ref:`SceneMultiplayer<class_SceneMultiplayer>`
  11. High-level multiplayer API interface.
  12. .. rst-class:: classref-introduction-group
  13. Description
  14. -----------
  15. Base class for high-level multiplayer API implementations. See also :ref:`MultiplayerPeer<class_MultiplayerPeer>`.
  16. 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.
  17. 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.
  18. 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.
  19. .. rst-class:: classref-reftable-group
  20. Properties
  21. ----------
  22. .. table::
  23. :widths: auto
  24. +-----------------------------------------------+-------------------------------------------------------------------------+
  25. | :ref:`MultiplayerPeer<class_MultiplayerPeer>` | :ref:`multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>` |
  26. +-----------------------------------------------+-------------------------------------------------------------------------+
  27. .. rst-class:: classref-reftable-group
  28. Methods
  29. -------
  30. .. table::
  31. :widths: auto
  32. +-------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  33. | :ref:`MultiplayerAPI<class_MultiplayerAPI>` | :ref:`create_default_interface<class_MultiplayerAPI_method_create_default_interface>` **(** **)** |static| |
  34. +-------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  35. | :ref:`StringName<class_StringName>` | :ref:`get_default_interface<class_MultiplayerAPI_method_get_default_interface>` **(** **)** |static| |
  36. +-------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  37. | :ref:`PackedInt32Array<class_PackedInt32Array>` | :ref:`get_peers<class_MultiplayerAPI_method_get_peers>` **(** **)** |
  38. +-------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  39. | :ref:`int<class_int>` | :ref:`get_remote_sender_id<class_MultiplayerAPI_method_get_remote_sender_id>` **(** **)** |
  40. +-------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  41. | :ref:`int<class_int>` | :ref:`get_unique_id<class_MultiplayerAPI_method_get_unique_id>` **(** **)** |
  42. +-------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  43. | :ref:`bool<class_bool>` | :ref:`has_multiplayer_peer<class_MultiplayerAPI_method_has_multiplayer_peer>` **(** **)** |
  44. +-------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  45. | :ref:`bool<class_bool>` | :ref:`is_server<class_MultiplayerAPI_method_is_server>` **(** **)** |
  46. +-------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  47. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`object_configuration_add<class_MultiplayerAPI_method_object_configuration_add>` **(** :ref:`Object<class_Object>` object, :ref:`Variant<class_Variant>` configuration **)** |
  48. +-------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  49. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`object_configuration_remove<class_MultiplayerAPI_method_object_configuration_remove>` **(** :ref:`Object<class_Object>` object, :ref:`Variant<class_Variant>` configuration **)** |
  50. +-------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  51. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`poll<class_MultiplayerAPI_method_poll>` **(** **)** |
  52. +-------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  53. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`rpc<class_MultiplayerAPI_method_rpc>` **(** :ref:`int<class_int>` peer, :ref:`Object<class_Object>` object, :ref:`StringName<class_StringName>` method, :ref:`Array<class_Array>` arguments=[] **)** |
  54. +-------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  55. | void | :ref:`set_default_interface<class_MultiplayerAPI_method_set_default_interface>` **(** :ref:`StringName<class_StringName>` interface_name **)** |static| |
  56. +-------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  57. .. rst-class:: classref-section-separator
  58. ----
  59. .. rst-class:: classref-descriptions-group
  60. Signals
  61. -------
  62. .. _class_MultiplayerAPI_signal_connected_to_server:
  63. .. rst-class:: classref-signal
  64. **connected_to_server** **(** **)**
  65. Emitted when this MultiplayerAPI's :ref:`multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>` successfully connected to a server. Only emitted on clients.
  66. .. rst-class:: classref-item-separator
  67. ----
  68. .. _class_MultiplayerAPI_signal_connection_failed:
  69. .. rst-class:: classref-signal
  70. **connection_failed** **(** **)**
  71. 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.
  72. .. rst-class:: classref-item-separator
  73. ----
  74. .. _class_MultiplayerAPI_signal_peer_connected:
  75. .. rst-class:: classref-signal
  76. **peer_connected** **(** :ref:`int<class_int>` id **)**
  77. 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).
  78. .. rst-class:: classref-item-separator
  79. ----
  80. .. _class_MultiplayerAPI_signal_peer_disconnected:
  81. .. rst-class:: classref-signal
  82. **peer_disconnected** **(** :ref:`int<class_int>` id **)**
  83. 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.
  84. .. rst-class:: classref-item-separator
  85. ----
  86. .. _class_MultiplayerAPI_signal_server_disconnected:
  87. .. rst-class:: classref-signal
  88. **server_disconnected** **(** **)**
  89. Emitted when this MultiplayerAPI's :ref:`multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>` disconnects from server. Only emitted on clients.
  90. .. rst-class:: classref-section-separator
  91. ----
  92. .. rst-class:: classref-descriptions-group
  93. Enumerations
  94. ------------
  95. .. _enum_MultiplayerAPI_RPCMode:
  96. .. rst-class:: classref-enumeration
  97. enum **RPCMode**:
  98. .. _class_MultiplayerAPI_constant_RPC_MODE_DISABLED:
  99. .. rst-class:: classref-enumeration-constant
  100. :ref:`RPCMode<enum_MultiplayerAPI_RPCMode>` **RPC_MODE_DISABLED** = ``0``
  101. 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.
  102. .. _class_MultiplayerAPI_constant_RPC_MODE_ANY_PEER:
  103. .. rst-class:: classref-enumeration-constant
  104. :ref:`RPCMode<enum_MultiplayerAPI_RPCMode>` **RPC_MODE_ANY_PEER** = ``1``
  105. 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.
  106. .. _class_MultiplayerAPI_constant_RPC_MODE_AUTHORITY:
  107. .. rst-class:: classref-enumeration-constant
  108. :ref:`RPCMode<enum_MultiplayerAPI_RPCMode>` **RPC_MODE_AUTHORITY** = ``2``
  109. 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>`.
  110. .. rst-class:: classref-section-separator
  111. ----
  112. .. rst-class:: classref-descriptions-group
  113. Property Descriptions
  114. ---------------------
  115. .. _class_MultiplayerAPI_property_multiplayer_peer:
  116. .. rst-class:: classref-property
  117. :ref:`MultiplayerPeer<class_MultiplayerPeer>` **multiplayer_peer**
  118. .. rst-class:: classref-property-setget
  119. - void **set_multiplayer_peer** **(** :ref:`MultiplayerPeer<class_MultiplayerPeer>` value **)**
  120. - :ref:`MultiplayerPeer<class_MultiplayerPeer>` **get_multiplayer_peer** **(** **)**
  121. 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.
  122. .. rst-class:: classref-section-separator
  123. ----
  124. .. rst-class:: classref-descriptions-group
  125. Method Descriptions
  126. -------------------
  127. .. _class_MultiplayerAPI_method_create_default_interface:
  128. .. rst-class:: classref-method
  129. :ref:`MultiplayerAPI<class_MultiplayerAPI>` **create_default_interface** **(** **)** |static|
  130. Returns a new instance of the default MultiplayerAPI.
  131. .. rst-class:: classref-item-separator
  132. ----
  133. .. _class_MultiplayerAPI_method_get_default_interface:
  134. .. rst-class:: classref-method
  135. :ref:`StringName<class_StringName>` **get_default_interface** **(** **)** |static|
  136. 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>`.
  137. .. rst-class:: classref-item-separator
  138. ----
  139. .. _class_MultiplayerAPI_method_get_peers:
  140. .. rst-class:: classref-method
  141. :ref:`PackedInt32Array<class_PackedInt32Array>` **get_peers** **(** **)**
  142. Returns the peer IDs of all connected peers of this MultiplayerAPI's :ref:`multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>`.
  143. .. rst-class:: classref-item-separator
  144. ----
  145. .. _class_MultiplayerAPI_method_get_remote_sender_id:
  146. .. rst-class:: classref-method
  147. :ref:`int<class_int>` **get_remote_sender_id** **(** **)**
  148. Returns the sender's peer ID for the RPC currently being executed.
  149. \ **Note:** If not inside an RPC this method will return 0.
  150. .. rst-class:: classref-item-separator
  151. ----
  152. .. _class_MultiplayerAPI_method_get_unique_id:
  153. .. rst-class:: classref-method
  154. :ref:`int<class_int>` **get_unique_id** **(** **)**
  155. Returns the unique peer ID of this MultiplayerAPI's :ref:`multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>`.
  156. .. rst-class:: classref-item-separator
  157. ----
  158. .. _class_MultiplayerAPI_method_has_multiplayer_peer:
  159. .. rst-class:: classref-method
  160. :ref:`bool<class_bool>` **has_multiplayer_peer** **(** **)**
  161. Returns ``true`` if there is a :ref:`multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>` set.
  162. .. rst-class:: classref-item-separator
  163. ----
  164. .. _class_MultiplayerAPI_method_is_server:
  165. .. rst-class:: classref-method
  166. :ref:`bool<class_bool>` **is_server** **(** **)**
  167. Returns ``true`` if this MultiplayerAPI's :ref:`multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>` is valid and in server mode (listening for connections).
  168. .. rst-class:: classref-item-separator
  169. ----
  170. .. _class_MultiplayerAPI_method_object_configuration_add:
  171. .. rst-class:: classref-method
  172. :ref:`Error<enum_@GlobalScope_Error>` **object_configuration_add** **(** :ref:`Object<class_Object>` object, :ref:`Variant<class_Variant>` configuration **)**
  173. 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.
  174. \ **Note:** This method is mostly relevant when extending or overriding the MultiplayerAPI behavior via :ref:`MultiplayerAPIExtension<class_MultiplayerAPIExtension>`.
  175. .. rst-class:: classref-item-separator
  176. ----
  177. .. _class_MultiplayerAPI_method_object_configuration_remove:
  178. .. rst-class:: classref-method
  179. :ref:`Error<enum_@GlobalScope_Error>` **object_configuration_remove** **(** :ref:`Object<class_Object>` object, :ref:`Variant<class_Variant>` configuration **)**
  180. 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.
  181. \ **Note:** This method is mostly relevant when extending or overriding the MultiplayerAPI behavior via :ref:`MultiplayerAPIExtension<class_MultiplayerAPIExtension>`.
  182. .. rst-class:: classref-item-separator
  183. ----
  184. .. _class_MultiplayerAPI_method_poll:
  185. .. rst-class:: classref-method
  186. :ref:`Error<enum_@GlobalScope_Error>` **poll** **(** **)**
  187. 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.
  188. \ **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>`).
  189. .. rst-class:: classref-item-separator
  190. ----
  191. .. _class_MultiplayerAPI_method_rpc:
  192. .. rst-class:: classref-method
  193. :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>` arguments=[] **)**
  194. 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>`.
  195. \ **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 augmenting or replacing the multiplayer capabilities.
  196. .. rst-class:: classref-item-separator
  197. ----
  198. .. _class_MultiplayerAPI_method_set_default_interface:
  199. .. rst-class:: classref-method
  200. void **set_default_interface** **(** :ref:`StringName<class_StringName>` interface_name **)** |static|
  201. 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.
  202. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  203. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  204. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  205. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  206. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  207. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  208. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`