class_multiplayerpeer.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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/MultiplayerPeer.xml.
  8. .. _class_MultiplayerPeer:
  9. MultiplayerPeer
  10. ===============
  11. **Inherits:** :ref:`PacketPeer<class_PacketPeer>` **<** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  12. **Inherited By:** :ref:`ENetMultiplayerPeer<class_ENetMultiplayerPeer>`, :ref:`MultiplayerPeerExtension<class_MultiplayerPeerExtension>`, :ref:`OfflineMultiplayerPeer<class_OfflineMultiplayerPeer>`, :ref:`WebRTCMultiplayerPeer<class_WebRTCMultiplayerPeer>`, :ref:`WebSocketMultiplayerPeer<class_WebSocketMultiplayerPeer>`
  13. Abstract class for specialized :ref:`PacketPeer<class_PacketPeer>`\ s used by the :ref:`MultiplayerAPI<class_MultiplayerAPI>`.
  14. .. rst-class:: classref-introduction-group
  15. Description
  16. -----------
  17. Manages the connection with one or more remote peers acting as server or client and assigning unique IDs to each of them. See also :ref:`MultiplayerAPI<class_MultiplayerAPI>`.
  18. \ **Note:** The :ref:`MultiplayerAPI<class_MultiplayerAPI>` protocol is an implementation detail and isn't meant to be used by non-Godot servers. It may change without notice.
  19. \ **Note:** When exporting to Android, make sure to enable the ``INTERNET`` permission in the Android export preset before exporting the project or using one-click deploy. Otherwise, network communication of any kind will be blocked by Android.
  20. .. rst-class:: classref-introduction-group
  21. Tutorials
  22. ---------
  23. - :doc:`High-level multiplayer <../tutorials/networking/high_level_multiplayer>`
  24. .. rst-class:: classref-reftable-group
  25. Properties
  26. ----------
  27. .. table::
  28. :widths: auto
  29. +--------------------------------------------------------+--------------------------------------------------------------------------------------+-----------+
  30. | :ref:`bool<class_bool>` | :ref:`refuse_new_connections<class_MultiplayerPeer_property_refuse_new_connections>` | ``false`` |
  31. +--------------------------------------------------------+--------------------------------------------------------------------------------------+-----------+
  32. | :ref:`int<class_int>` | :ref:`transfer_channel<class_MultiplayerPeer_property_transfer_channel>` | ``0`` |
  33. +--------------------------------------------------------+--------------------------------------------------------------------------------------+-----------+
  34. | :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>` | :ref:`transfer_mode<class_MultiplayerPeer_property_transfer_mode>` | ``2`` |
  35. +--------------------------------------------------------+--------------------------------------------------------------------------------------+-----------+
  36. .. rst-class:: classref-reftable-group
  37. Methods
  38. -------
  39. .. table::
  40. :widths: auto
  41. +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
  42. | |void| | :ref:`close<class_MultiplayerPeer_method_close>`\ (\ ) |
  43. +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
  44. | |void| | :ref:`disconnect_peer<class_MultiplayerPeer_method_disconnect_peer>`\ (\ peer\: :ref:`int<class_int>`, force\: :ref:`bool<class_bool>` = false\ ) |
  45. +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
  46. | :ref:`int<class_int>` | :ref:`generate_unique_id<class_MultiplayerPeer_method_generate_unique_id>`\ (\ ) |const| |
  47. +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
  48. | :ref:`ConnectionStatus<enum_MultiplayerPeer_ConnectionStatus>` | :ref:`get_connection_status<class_MultiplayerPeer_method_get_connection_status>`\ (\ ) |const| |
  49. +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | :ref:`int<class_int>` | :ref:`get_packet_channel<class_MultiplayerPeer_method_get_packet_channel>`\ (\ ) |const| |
  51. +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
  52. | :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>` | :ref:`get_packet_mode<class_MultiplayerPeer_method_get_packet_mode>`\ (\ ) |const| |
  53. +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
  54. | :ref:`int<class_int>` | :ref:`get_packet_peer<class_MultiplayerPeer_method_get_packet_peer>`\ (\ ) |const| |
  55. +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
  56. | :ref:`int<class_int>` | :ref:`get_unique_id<class_MultiplayerPeer_method_get_unique_id>`\ (\ ) |const| |
  57. +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
  58. | :ref:`bool<class_bool>` | :ref:`is_server_relay_supported<class_MultiplayerPeer_method_is_server_relay_supported>`\ (\ ) |const| |
  59. +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | |void| | :ref:`poll<class_MultiplayerPeer_method_poll>`\ (\ ) |
  61. +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | |void| | :ref:`set_target_peer<class_MultiplayerPeer_method_set_target_peer>`\ (\ id\: :ref:`int<class_int>`\ ) |
  63. +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
  64. .. rst-class:: classref-section-separator
  65. ----
  66. .. rst-class:: classref-descriptions-group
  67. Signals
  68. -------
  69. .. _class_MultiplayerPeer_signal_peer_connected:
  70. .. rst-class:: classref-signal
  71. **peer_connected**\ (\ id\: :ref:`int<class_int>`\ ) :ref:`🔗<class_MultiplayerPeer_signal_peer_connected>`
  72. Emitted when a remote peer connects.
  73. .. rst-class:: classref-item-separator
  74. ----
  75. .. _class_MultiplayerPeer_signal_peer_disconnected:
  76. .. rst-class:: classref-signal
  77. **peer_disconnected**\ (\ id\: :ref:`int<class_int>`\ ) :ref:`🔗<class_MultiplayerPeer_signal_peer_disconnected>`
  78. Emitted when a remote peer has disconnected.
  79. .. rst-class:: classref-section-separator
  80. ----
  81. .. rst-class:: classref-descriptions-group
  82. Enumerations
  83. ------------
  84. .. _enum_MultiplayerPeer_ConnectionStatus:
  85. .. rst-class:: classref-enumeration
  86. enum **ConnectionStatus**: :ref:`🔗<enum_MultiplayerPeer_ConnectionStatus>`
  87. .. _class_MultiplayerPeer_constant_CONNECTION_DISCONNECTED:
  88. .. rst-class:: classref-enumeration-constant
  89. :ref:`ConnectionStatus<enum_MultiplayerPeer_ConnectionStatus>` **CONNECTION_DISCONNECTED** = ``0``
  90. The MultiplayerPeer is disconnected.
  91. .. _class_MultiplayerPeer_constant_CONNECTION_CONNECTING:
  92. .. rst-class:: classref-enumeration-constant
  93. :ref:`ConnectionStatus<enum_MultiplayerPeer_ConnectionStatus>` **CONNECTION_CONNECTING** = ``1``
  94. The MultiplayerPeer is currently connecting to a server.
  95. .. _class_MultiplayerPeer_constant_CONNECTION_CONNECTED:
  96. .. rst-class:: classref-enumeration-constant
  97. :ref:`ConnectionStatus<enum_MultiplayerPeer_ConnectionStatus>` **CONNECTION_CONNECTED** = ``2``
  98. This MultiplayerPeer is connected.
  99. .. rst-class:: classref-item-separator
  100. ----
  101. .. _enum_MultiplayerPeer_TransferMode:
  102. .. rst-class:: classref-enumeration
  103. enum **TransferMode**: :ref:`🔗<enum_MultiplayerPeer_TransferMode>`
  104. .. _class_MultiplayerPeer_constant_TRANSFER_MODE_UNRELIABLE:
  105. .. rst-class:: classref-enumeration-constant
  106. :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>` **TRANSFER_MODE_UNRELIABLE** = ``0``
  107. Packets are not acknowledged, no resend attempts are made for lost packets. Packets may arrive in any order. Potentially faster than :ref:`TRANSFER_MODE_UNRELIABLE_ORDERED<class_MultiplayerPeer_constant_TRANSFER_MODE_UNRELIABLE_ORDERED>`. Use for non-critical data, and always consider whether the order matters.
  108. .. _class_MultiplayerPeer_constant_TRANSFER_MODE_UNRELIABLE_ORDERED:
  109. .. rst-class:: classref-enumeration-constant
  110. :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>` **TRANSFER_MODE_UNRELIABLE_ORDERED** = ``1``
  111. Packets are not acknowledged, no resend attempts are made for lost packets. Packets are received in the order they were sent in. Potentially faster than :ref:`TRANSFER_MODE_RELIABLE<class_MultiplayerPeer_constant_TRANSFER_MODE_RELIABLE>`. Use for non-critical data or data that would be outdated if received late due to resend attempt(s) anyway, for example movement and positional data.
  112. .. _class_MultiplayerPeer_constant_TRANSFER_MODE_RELIABLE:
  113. .. rst-class:: classref-enumeration-constant
  114. :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>` **TRANSFER_MODE_RELIABLE** = ``2``
  115. Packets must be received and resend attempts should be made until the packets are acknowledged. Packets must be received in the order they were sent in. Most reliable transfer mode, but potentially the slowest due to the overhead. Use for critical data that must be transmitted and arrive in order, for example an ability being triggered or a chat message. Consider carefully if the information really is critical, and use sparingly.
  116. .. rst-class:: classref-section-separator
  117. ----
  118. .. rst-class:: classref-descriptions-group
  119. Constants
  120. ---------
  121. .. _class_MultiplayerPeer_constant_TARGET_PEER_BROADCAST:
  122. .. rst-class:: classref-constant
  123. **TARGET_PEER_BROADCAST** = ``0`` :ref:`🔗<class_MultiplayerPeer_constant_TARGET_PEER_BROADCAST>`
  124. Packets are sent to all connected peers.
  125. .. _class_MultiplayerPeer_constant_TARGET_PEER_SERVER:
  126. .. rst-class:: classref-constant
  127. **TARGET_PEER_SERVER** = ``1`` :ref:`🔗<class_MultiplayerPeer_constant_TARGET_PEER_SERVER>`
  128. Packets are sent to the remote peer acting as server.
  129. .. rst-class:: classref-section-separator
  130. ----
  131. .. rst-class:: classref-descriptions-group
  132. Property Descriptions
  133. ---------------------
  134. .. _class_MultiplayerPeer_property_refuse_new_connections:
  135. .. rst-class:: classref-property
  136. :ref:`bool<class_bool>` **refuse_new_connections** = ``false`` :ref:`🔗<class_MultiplayerPeer_property_refuse_new_connections>`
  137. .. rst-class:: classref-property-setget
  138. - |void| **set_refuse_new_connections**\ (\ value\: :ref:`bool<class_bool>`\ )
  139. - :ref:`bool<class_bool>` **is_refusing_new_connections**\ (\ )
  140. If ``true``, this **MultiplayerPeer** refuses new connections.
  141. .. rst-class:: classref-item-separator
  142. ----
  143. .. _class_MultiplayerPeer_property_transfer_channel:
  144. .. rst-class:: classref-property
  145. :ref:`int<class_int>` **transfer_channel** = ``0`` :ref:`🔗<class_MultiplayerPeer_property_transfer_channel>`
  146. .. rst-class:: classref-property-setget
  147. - |void| **set_transfer_channel**\ (\ value\: :ref:`int<class_int>`\ )
  148. - :ref:`int<class_int>` **get_transfer_channel**\ (\ )
  149. The channel to use to send packets. Many network APIs such as ENet and WebRTC allow the creation of multiple independent channels which behaves, in a way, like separate connections. This means that reliable data will only block delivery of other packets on that channel, and ordering will only be in respect to the channel the packet is being sent on. Using different channels to send **different and independent** state updates is a common way to optimize network usage and decrease latency in fast-paced games.
  150. \ **Note:** The default channel (``0``) actually works as 3 separate channels (one for each :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>`) so that :ref:`TRANSFER_MODE_RELIABLE<class_MultiplayerPeer_constant_TRANSFER_MODE_RELIABLE>` and :ref:`TRANSFER_MODE_UNRELIABLE_ORDERED<class_MultiplayerPeer_constant_TRANSFER_MODE_UNRELIABLE_ORDERED>` does not interact with each other by default. Refer to the specific network API documentation (e.g. ENet or WebRTC) to learn how to set up channels correctly.
  151. .. rst-class:: classref-item-separator
  152. ----
  153. .. _class_MultiplayerPeer_property_transfer_mode:
  154. .. rst-class:: classref-property
  155. :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>` **transfer_mode** = ``2`` :ref:`🔗<class_MultiplayerPeer_property_transfer_mode>`
  156. .. rst-class:: classref-property-setget
  157. - |void| **set_transfer_mode**\ (\ value\: :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>`\ )
  158. - :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>` **get_transfer_mode**\ (\ )
  159. The manner in which to send packets to the target peer. See :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>`, and the :ref:`set_target_peer<class_MultiplayerPeer_method_set_target_peer>` method.
  160. .. rst-class:: classref-section-separator
  161. ----
  162. .. rst-class:: classref-descriptions-group
  163. Method Descriptions
  164. -------------------
  165. .. _class_MultiplayerPeer_method_close:
  166. .. rst-class:: classref-method
  167. |void| **close**\ (\ ) :ref:`🔗<class_MultiplayerPeer_method_close>`
  168. Immediately close the multiplayer peer returning to the state :ref:`CONNECTION_DISCONNECTED<class_MultiplayerPeer_constant_CONNECTION_DISCONNECTED>`. Connected peers will be dropped without emitting :ref:`peer_disconnected<class_MultiplayerPeer_signal_peer_disconnected>`.
  169. .. rst-class:: classref-item-separator
  170. ----
  171. .. _class_MultiplayerPeer_method_disconnect_peer:
  172. .. rst-class:: classref-method
  173. |void| **disconnect_peer**\ (\ peer\: :ref:`int<class_int>`, force\: :ref:`bool<class_bool>` = false\ ) :ref:`🔗<class_MultiplayerPeer_method_disconnect_peer>`
  174. Disconnects the given ``peer`` from this host. If ``force`` is ``true`` the :ref:`peer_disconnected<class_MultiplayerPeer_signal_peer_disconnected>` signal will not be emitted for this peer.
  175. .. rst-class:: classref-item-separator
  176. ----
  177. .. _class_MultiplayerPeer_method_generate_unique_id:
  178. .. rst-class:: classref-method
  179. :ref:`int<class_int>` **generate_unique_id**\ (\ ) |const| :ref:`🔗<class_MultiplayerPeer_method_generate_unique_id>`
  180. Returns a randomly generated integer that can be used as a network unique ID.
  181. .. rst-class:: classref-item-separator
  182. ----
  183. .. _class_MultiplayerPeer_method_get_connection_status:
  184. .. rst-class:: classref-method
  185. :ref:`ConnectionStatus<enum_MultiplayerPeer_ConnectionStatus>` **get_connection_status**\ (\ ) |const| :ref:`🔗<class_MultiplayerPeer_method_get_connection_status>`
  186. Returns the current state of the connection. See :ref:`ConnectionStatus<enum_MultiplayerPeer_ConnectionStatus>`.
  187. .. rst-class:: classref-item-separator
  188. ----
  189. .. _class_MultiplayerPeer_method_get_packet_channel:
  190. .. rst-class:: classref-method
  191. :ref:`int<class_int>` **get_packet_channel**\ (\ ) |const| :ref:`🔗<class_MultiplayerPeer_method_get_packet_channel>`
  192. Returns the channel over which the next available packet was received. See :ref:`PacketPeer.get_available_packet_count<class_PacketPeer_method_get_available_packet_count>`.
  193. .. rst-class:: classref-item-separator
  194. ----
  195. .. _class_MultiplayerPeer_method_get_packet_mode:
  196. .. rst-class:: classref-method
  197. :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>` **get_packet_mode**\ (\ ) |const| :ref:`🔗<class_MultiplayerPeer_method_get_packet_mode>`
  198. Returns the transfer mode the remote peer used to send the next available packet. See :ref:`PacketPeer.get_available_packet_count<class_PacketPeer_method_get_available_packet_count>`.
  199. .. rst-class:: classref-item-separator
  200. ----
  201. .. _class_MultiplayerPeer_method_get_packet_peer:
  202. .. rst-class:: classref-method
  203. :ref:`int<class_int>` **get_packet_peer**\ (\ ) |const| :ref:`🔗<class_MultiplayerPeer_method_get_packet_peer>`
  204. Returns the ID of the **MultiplayerPeer** who sent the next available packet. See :ref:`PacketPeer.get_available_packet_count<class_PacketPeer_method_get_available_packet_count>`.
  205. .. rst-class:: classref-item-separator
  206. ----
  207. .. _class_MultiplayerPeer_method_get_unique_id:
  208. .. rst-class:: classref-method
  209. :ref:`int<class_int>` **get_unique_id**\ (\ ) |const| :ref:`🔗<class_MultiplayerPeer_method_get_unique_id>`
  210. Returns the ID of this **MultiplayerPeer**.
  211. .. rst-class:: classref-item-separator
  212. ----
  213. .. _class_MultiplayerPeer_method_is_server_relay_supported:
  214. .. rst-class:: classref-method
  215. :ref:`bool<class_bool>` **is_server_relay_supported**\ (\ ) |const| :ref:`🔗<class_MultiplayerPeer_method_is_server_relay_supported>`
  216. Returns ``true`` if the server can act as a relay in the current configuration. That is, if the higher level :ref:`MultiplayerAPI<class_MultiplayerAPI>` should notify connected clients of other peers, and implement a relay protocol to allow communication between them.
  217. .. rst-class:: classref-item-separator
  218. ----
  219. .. _class_MultiplayerPeer_method_poll:
  220. .. rst-class:: classref-method
  221. |void| **poll**\ (\ ) :ref:`🔗<class_MultiplayerPeer_method_poll>`
  222. Waits up to 1 second to receive a new network event.
  223. .. rst-class:: classref-item-separator
  224. ----
  225. .. _class_MultiplayerPeer_method_set_target_peer:
  226. .. rst-class:: classref-method
  227. |void| **set_target_peer**\ (\ id\: :ref:`int<class_int>`\ ) :ref:`🔗<class_MultiplayerPeer_method_set_target_peer>`
  228. Sets the peer to which packets will be sent.
  229. The ``id`` can be one of: :ref:`TARGET_PEER_BROADCAST<class_MultiplayerPeer_constant_TARGET_PEER_BROADCAST>` to send to all connected peers, :ref:`TARGET_PEER_SERVER<class_MultiplayerPeer_constant_TARGET_PEER_SERVER>` to send to the peer acting as server, a valid peer ID to send to that specific peer, a negative peer ID to send to all peers except that one. By default, the target peer is :ref:`TARGET_PEER_BROADCAST<class_MultiplayerPeer_constant_TARGET_PEER_BROADCAST>`.
  230. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  231. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  232. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  233. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  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.)`
  235. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  236. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  237. .. |void| replace:: :abbr:`void (No return value.)`