123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- :github_url: hide
- .. meta::
- :keywords: network
- .. DO NOT EDIT THIS FILE!!!
- .. Generated automatically from Godot engine sources.
- .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
- .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/MultiplayerPeer.xml.
- .. _class_MultiplayerPeer:
- MultiplayerPeer
- ===============
- **Inherits:** :ref:`PacketPeer<class_PacketPeer>` **<** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
- **Inherited By:** :ref:`ENetMultiplayerPeer<class_ENetMultiplayerPeer>`, :ref:`MultiplayerPeerExtension<class_MultiplayerPeerExtension>`, :ref:`OfflineMultiplayerPeer<class_OfflineMultiplayerPeer>`, :ref:`WebRTCMultiplayerPeer<class_WebRTCMultiplayerPeer>`, :ref:`WebSocketMultiplayerPeer<class_WebSocketMultiplayerPeer>`
- Abstract class for specialized :ref:`PacketPeer<class_PacketPeer>`\ s used by the :ref:`MultiplayerAPI<class_MultiplayerAPI>`.
- .. rst-class:: classref-introduction-group
- Description
- -----------
- 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>`.
- \ **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.
- \ **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.
- .. rst-class:: classref-introduction-group
- Tutorials
- ---------
- - :doc:`High-level multiplayer <../tutorials/networking/high_level_multiplayer>`
- .. rst-class:: classref-reftable-group
- Properties
- ----------
- .. table::
- :widths: auto
- +--------------------------------------------------------+--------------------------------------------------------------------------------------+-----------+
- | :ref:`bool<class_bool>` | :ref:`refuse_new_connections<class_MultiplayerPeer_property_refuse_new_connections>` | ``false`` |
- +--------------------------------------------------------+--------------------------------------------------------------------------------------+-----------+
- | :ref:`int<class_int>` | :ref:`transfer_channel<class_MultiplayerPeer_property_transfer_channel>` | ``0`` |
- +--------------------------------------------------------+--------------------------------------------------------------------------------------+-----------+
- | :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>` | :ref:`transfer_mode<class_MultiplayerPeer_property_transfer_mode>` | ``2`` |
- +--------------------------------------------------------+--------------------------------------------------------------------------------------+-----------+
- .. rst-class:: classref-reftable-group
- Methods
- -------
- .. table::
- :widths: auto
- +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
- | |void| | :ref:`close<class_MultiplayerPeer_method_close>`\ (\ ) |
- +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
- | |void| | :ref:`disconnect_peer<class_MultiplayerPeer_method_disconnect_peer>`\ (\ peer\: :ref:`int<class_int>`, force\: :ref:`bool<class_bool>` = false\ ) |
- +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`int<class_int>` | :ref:`generate_unique_id<class_MultiplayerPeer_method_generate_unique_id>`\ (\ ) |const| |
- +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`ConnectionStatus<enum_MultiplayerPeer_ConnectionStatus>` | :ref:`get_connection_status<class_MultiplayerPeer_method_get_connection_status>`\ (\ ) |const| |
- +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`int<class_int>` | :ref:`get_packet_channel<class_MultiplayerPeer_method_get_packet_channel>`\ (\ ) |const| |
- +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>` | :ref:`get_packet_mode<class_MultiplayerPeer_method_get_packet_mode>`\ (\ ) |const| |
- +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`int<class_int>` | :ref:`get_packet_peer<class_MultiplayerPeer_method_get_packet_peer>`\ (\ ) |const| |
- +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`int<class_int>` | :ref:`get_unique_id<class_MultiplayerPeer_method_get_unique_id>`\ (\ ) |const| |
- +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`bool<class_bool>` | :ref:`is_server_relay_supported<class_MultiplayerPeer_method_is_server_relay_supported>`\ (\ ) |const| |
- +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
- | |void| | :ref:`poll<class_MultiplayerPeer_method_poll>`\ (\ ) |
- +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
- | |void| | :ref:`set_target_peer<class_MultiplayerPeer_method_set_target_peer>`\ (\ id\: :ref:`int<class_int>`\ ) |
- +----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
- .. rst-class:: classref-section-separator
- ----
- .. rst-class:: classref-descriptions-group
- Signals
- -------
- .. _class_MultiplayerPeer_signal_peer_connected:
- .. rst-class:: classref-signal
- **peer_connected**\ (\ id\: :ref:`int<class_int>`\ ) :ref:`🔗<class_MultiplayerPeer_signal_peer_connected>`
- Emitted when a remote peer connects.
- .. rst-class:: classref-item-separator
- ----
- .. _class_MultiplayerPeer_signal_peer_disconnected:
- .. rst-class:: classref-signal
- **peer_disconnected**\ (\ id\: :ref:`int<class_int>`\ ) :ref:`🔗<class_MultiplayerPeer_signal_peer_disconnected>`
- Emitted when a remote peer has disconnected.
- .. rst-class:: classref-section-separator
- ----
- .. rst-class:: classref-descriptions-group
- Enumerations
- ------------
- .. _enum_MultiplayerPeer_ConnectionStatus:
- .. rst-class:: classref-enumeration
- enum **ConnectionStatus**: :ref:`🔗<enum_MultiplayerPeer_ConnectionStatus>`
- .. _class_MultiplayerPeer_constant_CONNECTION_DISCONNECTED:
- .. rst-class:: classref-enumeration-constant
- :ref:`ConnectionStatus<enum_MultiplayerPeer_ConnectionStatus>` **CONNECTION_DISCONNECTED** = ``0``
- The MultiplayerPeer is disconnected.
- .. _class_MultiplayerPeer_constant_CONNECTION_CONNECTING:
- .. rst-class:: classref-enumeration-constant
- :ref:`ConnectionStatus<enum_MultiplayerPeer_ConnectionStatus>` **CONNECTION_CONNECTING** = ``1``
- The MultiplayerPeer is currently connecting to a server.
- .. _class_MultiplayerPeer_constant_CONNECTION_CONNECTED:
- .. rst-class:: classref-enumeration-constant
- :ref:`ConnectionStatus<enum_MultiplayerPeer_ConnectionStatus>` **CONNECTION_CONNECTED** = ``2``
- This MultiplayerPeer is connected.
- .. rst-class:: classref-item-separator
- ----
- .. _enum_MultiplayerPeer_TransferMode:
- .. rst-class:: classref-enumeration
- enum **TransferMode**: :ref:`🔗<enum_MultiplayerPeer_TransferMode>`
- .. _class_MultiplayerPeer_constant_TRANSFER_MODE_UNRELIABLE:
- .. rst-class:: classref-enumeration-constant
- :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>` **TRANSFER_MODE_UNRELIABLE** = ``0``
- 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.
- .. _class_MultiplayerPeer_constant_TRANSFER_MODE_UNRELIABLE_ORDERED:
- .. rst-class:: classref-enumeration-constant
- :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>` **TRANSFER_MODE_UNRELIABLE_ORDERED** = ``1``
- 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.
- .. _class_MultiplayerPeer_constant_TRANSFER_MODE_RELIABLE:
- .. rst-class:: classref-enumeration-constant
- :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>` **TRANSFER_MODE_RELIABLE** = ``2``
- 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.
- .. rst-class:: classref-section-separator
- ----
- .. rst-class:: classref-descriptions-group
- Constants
- ---------
- .. _class_MultiplayerPeer_constant_TARGET_PEER_BROADCAST:
- .. rst-class:: classref-constant
- **TARGET_PEER_BROADCAST** = ``0`` :ref:`🔗<class_MultiplayerPeer_constant_TARGET_PEER_BROADCAST>`
- Packets are sent to all connected peers.
- .. _class_MultiplayerPeer_constant_TARGET_PEER_SERVER:
- .. rst-class:: classref-constant
- **TARGET_PEER_SERVER** = ``1`` :ref:`🔗<class_MultiplayerPeer_constant_TARGET_PEER_SERVER>`
- Packets are sent to the remote peer acting as server.
- .. rst-class:: classref-section-separator
- ----
- .. rst-class:: classref-descriptions-group
- Property Descriptions
- ---------------------
- .. _class_MultiplayerPeer_property_refuse_new_connections:
- .. rst-class:: classref-property
- :ref:`bool<class_bool>` **refuse_new_connections** = ``false`` :ref:`🔗<class_MultiplayerPeer_property_refuse_new_connections>`
- .. rst-class:: classref-property-setget
- - |void| **set_refuse_new_connections**\ (\ value\: :ref:`bool<class_bool>`\ )
- - :ref:`bool<class_bool>` **is_refusing_new_connections**\ (\ )
- If ``true``, this **MultiplayerPeer** refuses new connections.
- .. rst-class:: classref-item-separator
- ----
- .. _class_MultiplayerPeer_property_transfer_channel:
- .. rst-class:: classref-property
- :ref:`int<class_int>` **transfer_channel** = ``0`` :ref:`🔗<class_MultiplayerPeer_property_transfer_channel>`
- .. rst-class:: classref-property-setget
- - |void| **set_transfer_channel**\ (\ value\: :ref:`int<class_int>`\ )
- - :ref:`int<class_int>` **get_transfer_channel**\ (\ )
- 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.
- \ **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.
- .. rst-class:: classref-item-separator
- ----
- .. _class_MultiplayerPeer_property_transfer_mode:
- .. rst-class:: classref-property
- :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>` **transfer_mode** = ``2`` :ref:`🔗<class_MultiplayerPeer_property_transfer_mode>`
- .. rst-class:: classref-property-setget
- - |void| **set_transfer_mode**\ (\ value\: :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>`\ )
- - :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>` **get_transfer_mode**\ (\ )
- 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.
- .. rst-class:: classref-section-separator
- ----
- .. rst-class:: classref-descriptions-group
- Method Descriptions
- -------------------
- .. _class_MultiplayerPeer_method_close:
- .. rst-class:: classref-method
- |void| **close**\ (\ ) :ref:`🔗<class_MultiplayerPeer_method_close>`
- 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>`.
- .. rst-class:: classref-item-separator
- ----
- .. _class_MultiplayerPeer_method_disconnect_peer:
- .. rst-class:: classref-method
- |void| **disconnect_peer**\ (\ peer\: :ref:`int<class_int>`, force\: :ref:`bool<class_bool>` = false\ ) :ref:`🔗<class_MultiplayerPeer_method_disconnect_peer>`
- 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.
- .. rst-class:: classref-item-separator
- ----
- .. _class_MultiplayerPeer_method_generate_unique_id:
- .. rst-class:: classref-method
- :ref:`int<class_int>` **generate_unique_id**\ (\ ) |const| :ref:`🔗<class_MultiplayerPeer_method_generate_unique_id>`
- Returns a randomly generated integer that can be used as a network unique ID.
- .. rst-class:: classref-item-separator
- ----
- .. _class_MultiplayerPeer_method_get_connection_status:
- .. rst-class:: classref-method
- :ref:`ConnectionStatus<enum_MultiplayerPeer_ConnectionStatus>` **get_connection_status**\ (\ ) |const| :ref:`🔗<class_MultiplayerPeer_method_get_connection_status>`
- Returns the current state of the connection. See :ref:`ConnectionStatus<enum_MultiplayerPeer_ConnectionStatus>`.
- .. rst-class:: classref-item-separator
- ----
- .. _class_MultiplayerPeer_method_get_packet_channel:
- .. rst-class:: classref-method
- :ref:`int<class_int>` **get_packet_channel**\ (\ ) |const| :ref:`🔗<class_MultiplayerPeer_method_get_packet_channel>`
- 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>`.
- .. rst-class:: classref-item-separator
- ----
- .. _class_MultiplayerPeer_method_get_packet_mode:
- .. rst-class:: classref-method
- :ref:`TransferMode<enum_MultiplayerPeer_TransferMode>` **get_packet_mode**\ (\ ) |const| :ref:`🔗<class_MultiplayerPeer_method_get_packet_mode>`
- 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>`.
- .. rst-class:: classref-item-separator
- ----
- .. _class_MultiplayerPeer_method_get_packet_peer:
- .. rst-class:: classref-method
- :ref:`int<class_int>` **get_packet_peer**\ (\ ) |const| :ref:`🔗<class_MultiplayerPeer_method_get_packet_peer>`
- 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>`.
- .. rst-class:: classref-item-separator
- ----
- .. _class_MultiplayerPeer_method_get_unique_id:
- .. rst-class:: classref-method
- :ref:`int<class_int>` **get_unique_id**\ (\ ) |const| :ref:`🔗<class_MultiplayerPeer_method_get_unique_id>`
- Returns the ID of this **MultiplayerPeer**.
- .. rst-class:: classref-item-separator
- ----
- .. _class_MultiplayerPeer_method_is_server_relay_supported:
- .. rst-class:: classref-method
- :ref:`bool<class_bool>` **is_server_relay_supported**\ (\ ) |const| :ref:`🔗<class_MultiplayerPeer_method_is_server_relay_supported>`
- 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.
- .. rst-class:: classref-item-separator
- ----
- .. _class_MultiplayerPeer_method_poll:
- .. rst-class:: classref-method
- |void| **poll**\ (\ ) :ref:`🔗<class_MultiplayerPeer_method_poll>`
- Waits up to 1 second to receive a new network event.
- .. rst-class:: classref-item-separator
- ----
- .. _class_MultiplayerPeer_method_set_target_peer:
- .. rst-class:: classref-method
- |void| **set_target_peer**\ (\ id\: :ref:`int<class_int>`\ ) :ref:`🔗<class_MultiplayerPeer_method_set_target_peer>`
- Sets the peer to which packets will be sent.
- 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>`.
- .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
- .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
- .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
- .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
- .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
- .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
- .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
- .. |void| replace:: :abbr:`void (No return value.)`
|