class_multiplayerapi.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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/3.6/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/3.6/doc/classes/MultiplayerAPI.xml.
  6. .. _class_MultiplayerAPI:
  7. MultiplayerAPI
  8. ==============
  9. **Inherits:** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  10. High-level multiplayer API.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. This class implements most of the logic behind the high-level multiplayer API. See also :ref:`NetworkedMultiplayerPeer<class_NetworkedMultiplayerPeer>`.
  15. By default, :ref:`SceneTree<class_SceneTree>` has a reference to this class that is used to provide multiplayer capabilities (i.e. RPC/RSET) across the whole scene.
  16. It is possible to override the MultiplayerAPI instance used by specific Nodes by setting the :ref:`Node.custom_multiplayer<class_Node_property_custom_multiplayer>` property, effectively allowing to run both client and server in the same scene.
  17. \ **Note:** The high-level multiplayer API protocol is an implementation detail and isn't meant to be used by non-Godot servers. It may change without notice.
  18. .. rst-class:: classref-reftable-group
  19. Properties
  20. ----------
  21. .. table::
  22. :widths: auto
  23. +-----------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+-----------+
  24. | :ref:`bool<class_bool>` | :ref:`allow_object_decoding<class_MultiplayerAPI_property_allow_object_decoding>` | ``false`` |
  25. +-----------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+-----------+
  26. | :ref:`NetworkedMultiplayerPeer<class_NetworkedMultiplayerPeer>` | :ref:`network_peer<class_MultiplayerAPI_property_network_peer>` | |
  27. +-----------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+-----------+
  28. | :ref:`bool<class_bool>` | :ref:`refuse_new_network_connections<class_MultiplayerAPI_property_refuse_new_network_connections>` | ``false`` |
  29. +-----------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+-----------+
  30. | :ref:`Node<class_Node>` | :ref:`root_node<class_MultiplayerAPI_property_root_node>` | |
  31. +-----------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+-----------+
  32. .. rst-class:: classref-reftable-group
  33. Methods
  34. -------
  35. .. table::
  36. :widths: auto
  37. +-----------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  38. | void | :ref:`clear<class_MultiplayerAPI_method_clear>` **(** **)** |
  39. +-----------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  40. | :ref:`PoolIntArray<class_PoolIntArray>` | :ref:`get_network_connected_peers<class_MultiplayerAPI_method_get_network_connected_peers>` **(** **)** |const| |
  41. +-----------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  42. | :ref:`int<class_int>` | :ref:`get_network_unique_id<class_MultiplayerAPI_method_get_network_unique_id>` **(** **)** |const| |
  43. +-----------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  44. | :ref:`int<class_int>` | :ref:`get_rpc_sender_id<class_MultiplayerAPI_method_get_rpc_sender_id>` **(** **)** |const| |
  45. +-----------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  46. | :ref:`bool<class_bool>` | :ref:`has_network_peer<class_MultiplayerAPI_method_has_network_peer>` **(** **)** |const| |
  47. +-----------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  48. | :ref:`bool<class_bool>` | :ref:`is_network_server<class_MultiplayerAPI_method_is_network_server>` **(** **)** |const| |
  49. +-----------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | void | :ref:`poll<class_MultiplayerAPI_method_poll>` **(** **)** |
  51. +-----------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  52. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`send_bytes<class_MultiplayerAPI_method_send_bytes>` **(** :ref:`PoolByteArray<class_PoolByteArray>` bytes, :ref:`int<class_int>` id=0, :ref:`TransferMode<enum_NetworkedMultiplayerPeer_TransferMode>` mode=2 **)** |
  53. +-----------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  54. .. rst-class:: classref-section-separator
  55. ----
  56. .. rst-class:: classref-descriptions-group
  57. Signals
  58. -------
  59. .. _class_MultiplayerAPI_signal_connected_to_server:
  60. .. rst-class:: classref-signal
  61. **connected_to_server** **(** **)**
  62. Emitted when this MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_property_network_peer>` successfully connected to a server. Only emitted on clients.
  63. .. rst-class:: classref-item-separator
  64. ----
  65. .. _class_MultiplayerAPI_signal_connection_failed:
  66. .. rst-class:: classref-signal
  67. **connection_failed** **(** **)**
  68. Emitted when this MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_property_network_peer>` fails to establish a connection to a server. Only emitted on clients.
  69. .. rst-class:: classref-item-separator
  70. ----
  71. .. _class_MultiplayerAPI_signal_network_peer_connected:
  72. .. rst-class:: classref-signal
  73. **network_peer_connected** **(** :ref:`int<class_int>` id **)**
  74. Emitted when this MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_property_network_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).
  75. .. rst-class:: classref-item-separator
  76. ----
  77. .. _class_MultiplayerAPI_signal_network_peer_disconnected:
  78. .. rst-class:: classref-signal
  79. **network_peer_disconnected** **(** :ref:`int<class_int>` id **)**
  80. Emitted when this MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_property_network_peer>` disconnects from a peer. Clients get notified when other clients disconnect from the same server.
  81. .. rst-class:: classref-item-separator
  82. ----
  83. .. _class_MultiplayerAPI_signal_network_peer_packet:
  84. .. rst-class:: classref-signal
  85. **network_peer_packet** **(** :ref:`int<class_int>` id, :ref:`PoolByteArray<class_PoolByteArray>` packet **)**
  86. Emitted when this MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_property_network_peer>` receive a ``packet`` with custom data (see :ref:`send_bytes<class_MultiplayerAPI_method_send_bytes>`). ID is the peer ID of the peer that sent the packet.
  87. .. rst-class:: classref-item-separator
  88. ----
  89. .. _class_MultiplayerAPI_signal_server_disconnected:
  90. .. rst-class:: classref-signal
  91. **server_disconnected** **(** **)**
  92. Emitted when this MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_property_network_peer>` disconnects from server. Only emitted on clients.
  93. .. rst-class:: classref-section-separator
  94. ----
  95. .. rst-class:: classref-descriptions-group
  96. Enumerations
  97. ------------
  98. .. _enum_MultiplayerAPI_RPCMode:
  99. .. rst-class:: classref-enumeration
  100. enum **RPCMode**:
  101. .. _class_MultiplayerAPI_constant_RPC_MODE_DISABLED:
  102. .. rst-class:: classref-enumeration-constant
  103. :ref:`RPCMode<enum_MultiplayerAPI_RPCMode>` **RPC_MODE_DISABLED** = ``0``
  104. Used with :ref:`Node.rpc_config<class_Node_method_rpc_config>` or :ref:`Node.rset_config<class_Node_method_rset_config>` to disable a method or property for all RPC calls, making it unavailable. Default for all methods.
  105. .. _class_MultiplayerAPI_constant_RPC_MODE_REMOTE:
  106. .. rst-class:: classref-enumeration-constant
  107. :ref:`RPCMode<enum_MultiplayerAPI_RPCMode>` **RPC_MODE_REMOTE** = ``1``
  108. Used with :ref:`Node.rpc_config<class_Node_method_rpc_config>` or :ref:`Node.rset_config<class_Node_method_rset_config>` to set a method to be called or a property to be changed only on the remote end, not locally. Analogous to the ``remote`` keyword. Calls and property changes are accepted from all remote peers, no matter if they are node's master or puppets.
  109. .. _class_MultiplayerAPI_constant_RPC_MODE_MASTER:
  110. .. rst-class:: classref-enumeration-constant
  111. :ref:`RPCMode<enum_MultiplayerAPI_RPCMode>` **RPC_MODE_MASTER** = ``2``
  112. Used with :ref:`Node.rpc_config<class_Node_method_rpc_config>` or :ref:`Node.rset_config<class_Node_method_rset_config>` to set a method to be called or a property to be changed only on the network master for this node. Analogous to the ``master`` keyword. Only accepts calls or property changes from the node's network puppets, see :ref:`Node.set_network_master<class_Node_method_set_network_master>`.
  113. .. _class_MultiplayerAPI_constant_RPC_MODE_PUPPET:
  114. .. rst-class:: classref-enumeration-constant
  115. :ref:`RPCMode<enum_MultiplayerAPI_RPCMode>` **RPC_MODE_PUPPET** = ``3``
  116. Used with :ref:`Node.rpc_config<class_Node_method_rpc_config>` or :ref:`Node.rset_config<class_Node_method_rset_config>` to set a method to be called or a property to be changed only on puppets for this node. Analogous to the ``puppet`` keyword. Only accepts calls or property changes from the node's network master, see :ref:`Node.set_network_master<class_Node_method_set_network_master>`.
  117. .. _class_MultiplayerAPI_constant_RPC_MODE_SLAVE:
  118. .. rst-class:: classref-enumeration-constant
  119. :ref:`RPCMode<enum_MultiplayerAPI_RPCMode>` **RPC_MODE_SLAVE** = ``3``
  120. *Deprecated.* Use :ref:`RPC_MODE_PUPPET<class_MultiplayerAPI_constant_RPC_MODE_PUPPET>` instead. Analogous to the ``slave`` keyword.
  121. .. _class_MultiplayerAPI_constant_RPC_MODE_REMOTESYNC:
  122. .. rst-class:: classref-enumeration-constant
  123. :ref:`RPCMode<enum_MultiplayerAPI_RPCMode>` **RPC_MODE_REMOTESYNC** = ``4``
  124. Behave like :ref:`RPC_MODE_REMOTE<class_MultiplayerAPI_constant_RPC_MODE_REMOTE>` but also make the call or property change locally. Analogous to the ``remotesync`` keyword.
  125. .. _class_MultiplayerAPI_constant_RPC_MODE_SYNC:
  126. .. rst-class:: classref-enumeration-constant
  127. :ref:`RPCMode<enum_MultiplayerAPI_RPCMode>` **RPC_MODE_SYNC** = ``4``
  128. *Deprecated.* Use :ref:`RPC_MODE_REMOTESYNC<class_MultiplayerAPI_constant_RPC_MODE_REMOTESYNC>` instead. Analogous to the ``sync`` keyword.
  129. .. _class_MultiplayerAPI_constant_RPC_MODE_MASTERSYNC:
  130. .. rst-class:: classref-enumeration-constant
  131. :ref:`RPCMode<enum_MultiplayerAPI_RPCMode>` **RPC_MODE_MASTERSYNC** = ``5``
  132. Behave like :ref:`RPC_MODE_MASTER<class_MultiplayerAPI_constant_RPC_MODE_MASTER>` but also make the call or property change locally. Analogous to the ``mastersync`` keyword.
  133. .. _class_MultiplayerAPI_constant_RPC_MODE_PUPPETSYNC:
  134. .. rst-class:: classref-enumeration-constant
  135. :ref:`RPCMode<enum_MultiplayerAPI_RPCMode>` **RPC_MODE_PUPPETSYNC** = ``6``
  136. Behave like :ref:`RPC_MODE_PUPPET<class_MultiplayerAPI_constant_RPC_MODE_PUPPET>` but also make the call or property change locally. Analogous to the ``puppetsync`` keyword.
  137. .. rst-class:: classref-section-separator
  138. ----
  139. .. rst-class:: classref-descriptions-group
  140. Property Descriptions
  141. ---------------------
  142. .. _class_MultiplayerAPI_property_allow_object_decoding:
  143. .. rst-class:: classref-property
  144. :ref:`bool<class_bool>` **allow_object_decoding** = ``false``
  145. .. rst-class:: classref-property-setget
  146. - void **set_allow_object_decoding** **(** :ref:`bool<class_bool>` value **)**
  147. - :ref:`bool<class_bool>` **is_object_decoding_allowed** **(** **)**
  148. If ``true`` (or if the :ref:`network_peer<class_MultiplayerAPI_property_network_peer>` has :ref:`PacketPeer.allow_object_decoding<class_PacketPeer_property_allow_object_decoding>` set to ``true``), the MultiplayerAPI will allow encoding and decoding of object during RPCs/RSETs.
  149. \ **Warning:** Deserialized objects can contain code which gets executed. Do not use this option if the serialized object comes from untrusted sources to avoid potential security threats such as remote code execution.
  150. .. rst-class:: classref-item-separator
  151. ----
  152. .. _class_MultiplayerAPI_property_network_peer:
  153. .. rst-class:: classref-property
  154. :ref:`NetworkedMultiplayerPeer<class_NetworkedMultiplayerPeer>` **network_peer**
  155. .. rst-class:: classref-property-setget
  156. - void **set_network_peer** **(** :ref:`NetworkedMultiplayerPeer<class_NetworkedMultiplayerPeer>` value **)**
  157. - :ref:`NetworkedMultiplayerPeer<class_NetworkedMultiplayerPeer>` **get_network_peer** **(** **)**
  158. 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_network_server<class_MultiplayerAPI_method_is_network_server>`) and will set root node's network mode to master, or it will become a regular peer with root node set to puppet. 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.
  159. .. rst-class:: classref-item-separator
  160. ----
  161. .. _class_MultiplayerAPI_property_refuse_new_network_connections:
  162. .. rst-class:: classref-property
  163. :ref:`bool<class_bool>` **refuse_new_network_connections** = ``false``
  164. .. rst-class:: classref-property-setget
  165. - void **set_refuse_new_network_connections** **(** :ref:`bool<class_bool>` value **)**
  166. - :ref:`bool<class_bool>` **is_refusing_new_network_connections** **(** **)**
  167. If ``true``, the MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_property_network_peer>` refuses new incoming connections.
  168. .. rst-class:: classref-item-separator
  169. ----
  170. .. _class_MultiplayerAPI_property_root_node:
  171. .. rst-class:: classref-property
  172. :ref:`Node<class_Node>` **root_node**
  173. .. rst-class:: classref-property-setget
  174. - void **set_root_node** **(** :ref:`Node<class_Node>` value **)**
  175. - :ref:`Node<class_Node>` **get_root_node** **(** **)**
  176. The root node to use for RPCs. Instead of an absolute path, a relative path will be used to find the node upon which the RPC should be executed.
  177. This effectively allows to have different branches of the scene tree to be managed by different MultiplayerAPI, allowing for example to run both client and server in the same scene.
  178. .. rst-class:: classref-section-separator
  179. ----
  180. .. rst-class:: classref-descriptions-group
  181. Method Descriptions
  182. -------------------
  183. .. _class_MultiplayerAPI_method_clear:
  184. .. rst-class:: classref-method
  185. void **clear** **(** **)**
  186. Clears the current MultiplayerAPI network state (you shouldn't call this unless you know what you are doing).
  187. .. rst-class:: classref-item-separator
  188. ----
  189. .. _class_MultiplayerAPI_method_get_network_connected_peers:
  190. .. rst-class:: classref-method
  191. :ref:`PoolIntArray<class_PoolIntArray>` **get_network_connected_peers** **(** **)** |const|
  192. Returns the peer IDs of all connected peers of this MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_property_network_peer>`.
  193. .. rst-class:: classref-item-separator
  194. ----
  195. .. _class_MultiplayerAPI_method_get_network_unique_id:
  196. .. rst-class:: classref-method
  197. :ref:`int<class_int>` **get_network_unique_id** **(** **)** |const|
  198. Returns the unique peer ID of this MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_property_network_peer>`.
  199. .. rst-class:: classref-item-separator
  200. ----
  201. .. _class_MultiplayerAPI_method_get_rpc_sender_id:
  202. .. rst-class:: classref-method
  203. :ref:`int<class_int>` **get_rpc_sender_id** **(** **)** |const|
  204. Returns the sender's peer ID for the RPC currently being executed.
  205. \ **Note:** If not inside an RPC this method will return 0.
  206. .. rst-class:: classref-item-separator
  207. ----
  208. .. _class_MultiplayerAPI_method_has_network_peer:
  209. .. rst-class:: classref-method
  210. :ref:`bool<class_bool>` **has_network_peer** **(** **)** |const|
  211. Returns ``true`` if there is a :ref:`network_peer<class_MultiplayerAPI_property_network_peer>` set.
  212. .. rst-class:: classref-item-separator
  213. ----
  214. .. _class_MultiplayerAPI_method_is_network_server:
  215. .. rst-class:: classref-method
  216. :ref:`bool<class_bool>` **is_network_server** **(** **)** |const|
  217. Returns ``true`` if this MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_property_network_peer>` is in server mode (listening for connections).
  218. .. rst-class:: classref-item-separator
  219. ----
  220. .. _class_MultiplayerAPI_method_poll:
  221. .. rst-class:: classref-method
  222. void **poll** **(** **)**
  223. Method used for polling the MultiplayerAPI. You only need to worry about this if you are using :ref:`Node.custom_multiplayer<class_Node_property_custom_multiplayer>` override or you set :ref:`SceneTree.multiplayer_poll<class_SceneTree_property_multiplayer_poll>` to ``false``. By default, :ref:`SceneTree<class_SceneTree>` will poll its MultiplayerAPI for you.
  224. \ **Note:** This method results in RPCs and RSETs being called, so they will be executed in the same context of this function (e.g. ``_process``, ``physics``, :ref:`Thread<class_Thread>`).
  225. .. rst-class:: classref-item-separator
  226. ----
  227. .. _class_MultiplayerAPI_method_send_bytes:
  228. .. rst-class:: classref-method
  229. :ref:`Error<enum_@GlobalScope_Error>` **send_bytes** **(** :ref:`PoolByteArray<class_PoolByteArray>` bytes, :ref:`int<class_int>` id=0, :ref:`TransferMode<enum_NetworkedMultiplayerPeer_TransferMode>` mode=2 **)**
  230. Sends the given raw ``bytes`` to a specific peer identified by ``id`` (see :ref:`NetworkedMultiplayerPeer.set_target_peer<class_NetworkedMultiplayerPeer_method_set_target_peer>`). Default ID is ``0``, i.e. broadcast to all peers.
  231. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  232. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  233. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  234. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`