class_webrtcmultiplayerpeer.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the WebRTCMultiplayerPeer.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_WebRTCMultiplayerPeer:
  6. WebRTCMultiplayerPeer
  7. =====================
  8. **Inherits:** :ref:`MultiplayerPeer<class_MultiplayerPeer>` **<** :ref:`PacketPeer<class_PacketPeer>` **<** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  9. A simple interface to create a peer-to-peer mesh network composed of :ref:`WebRTCPeerConnection<class_WebRTCPeerConnection>` that is compatible with the :ref:`MultiplayerAPI<class_MultiplayerAPI>`.
  10. Description
  11. -----------
  12. This class constructs a full mesh of :ref:`WebRTCPeerConnection<class_WebRTCPeerConnection>` (one connection for each peer) that can be used as a :ref:`MultiplayerAPI.multiplayer_peer<class_MultiplayerAPI_property_multiplayer_peer>`.
  13. You can add each :ref:`WebRTCPeerConnection<class_WebRTCPeerConnection>` via :ref:`add_peer<class_WebRTCMultiplayerPeer_method_add_peer>` or remove them via :ref:`remove_peer<class_WebRTCMultiplayerPeer_method_remove_peer>`. Peers must be added in :ref:`WebRTCPeerConnection.STATE_NEW<class_WebRTCPeerConnection_constant_STATE_NEW>` state to allow it to create the appropriate channels. This class will not create offers nor set descriptions, it will only poll them, and notify connections and disconnections.
  14. \ :ref:`MultiplayerPeer.connection_succeeded<class_MultiplayerPeer_signal_connection_succeeded>` and :ref:`MultiplayerPeer.server_disconnected<class_MultiplayerPeer_signal_server_disconnected>` will not be emitted unless ``server_compatibility`` is ``true`` in :ref:`initialize<class_WebRTCMultiplayerPeer_method_initialize>`. Beside that data transfer works like in a :ref:`MultiplayerPeer<class_MultiplayerPeer>`.
  15. \ **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.
  16. Methods
  17. -------
  18. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  19. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`add_peer<class_WebRTCMultiplayerPeer_method_add_peer>` **(** :ref:`WebRTCPeerConnection<class_WebRTCPeerConnection>` peer, :ref:`int<class_int>` peer_id, :ref:`int<class_int>` unreliable_lifetime=1 **)** |
  20. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  21. | void | :ref:`close<class_WebRTCMultiplayerPeer_method_close>` **(** **)** |
  22. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  23. | :ref:`Dictionary<class_Dictionary>` | :ref:`get_peer<class_WebRTCMultiplayerPeer_method_get_peer>` **(** :ref:`int<class_int>` peer_id **)** |
  24. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  25. | :ref:`Dictionary<class_Dictionary>` | :ref:`get_peers<class_WebRTCMultiplayerPeer_method_get_peers>` **(** **)** |
  26. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  27. | :ref:`bool<class_bool>` | :ref:`has_peer<class_WebRTCMultiplayerPeer_method_has_peer>` **(** :ref:`int<class_int>` peer_id **)** |
  28. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  29. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`initialize<class_WebRTCMultiplayerPeer_method_initialize>` **(** :ref:`int<class_int>` peer_id, :ref:`bool<class_bool>` server_compatibility=false, :ref:`Array<class_Array>` channels_config=[] **)** |
  30. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  31. | void | :ref:`remove_peer<class_WebRTCMultiplayerPeer_method_remove_peer>` **(** :ref:`int<class_int>` peer_id **)** |
  32. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  33. Method Descriptions
  34. -------------------
  35. .. _class_WebRTCMultiplayerPeer_method_add_peer:
  36. - :ref:`Error<enum_@GlobalScope_Error>` **add_peer** **(** :ref:`WebRTCPeerConnection<class_WebRTCPeerConnection>` peer, :ref:`int<class_int>` peer_id, :ref:`int<class_int>` unreliable_lifetime=1 **)**
  37. Add a new peer to the mesh with the given ``peer_id``. The :ref:`WebRTCPeerConnection<class_WebRTCPeerConnection>` must be in state :ref:`WebRTCPeerConnection.STATE_NEW<class_WebRTCPeerConnection_constant_STATE_NEW>`.
  38. Three channels will be created for reliable, unreliable, and ordered transport. The value of ``unreliable_lifetime`` will be passed to the ``maxPacketLifetime`` option when creating unreliable and ordered channels (see :ref:`WebRTCPeerConnection.create_data_channel<class_WebRTCPeerConnection_method_create_data_channel>`).
  39. ----
  40. .. _class_WebRTCMultiplayerPeer_method_close:
  41. - void **close** **(** **)**
  42. Close all the add peer connections and channels, freeing all resources.
  43. ----
  44. .. _class_WebRTCMultiplayerPeer_method_get_peer:
  45. - :ref:`Dictionary<class_Dictionary>` **get_peer** **(** :ref:`int<class_int>` peer_id **)**
  46. Returns a dictionary representation of the peer with given ``peer_id`` with three keys. ``connection`` containing the :ref:`WebRTCPeerConnection<class_WebRTCPeerConnection>` to this peer, ``channels`` an array of three :ref:`WebRTCDataChannel<class_WebRTCDataChannel>`, and ``connected`` a boolean representing if the peer connection is currently connected (all three channels are open).
  47. ----
  48. .. _class_WebRTCMultiplayerPeer_method_get_peers:
  49. - :ref:`Dictionary<class_Dictionary>` **get_peers** **(** **)**
  50. Returns a dictionary which keys are the peer ids and values the peer representation as in :ref:`get_peer<class_WebRTCMultiplayerPeer_method_get_peer>`.
  51. ----
  52. .. _class_WebRTCMultiplayerPeer_method_has_peer:
  53. - :ref:`bool<class_bool>` **has_peer** **(** :ref:`int<class_int>` peer_id **)**
  54. Returns ``true`` if the given ``peer_id`` is in the peers map (it might not be connected though).
  55. ----
  56. .. _class_WebRTCMultiplayerPeer_method_initialize:
  57. - :ref:`Error<enum_@GlobalScope_Error>` **initialize** **(** :ref:`int<class_int>` peer_id, :ref:`bool<class_bool>` server_compatibility=false, :ref:`Array<class_Array>` channels_config=[] **)**
  58. Initialize the multiplayer peer with the given ``peer_id`` (must be between 1 and 2147483647).
  59. If ``server_compatibilty`` is ``false`` (default), the multiplayer peer will be immediately in state :ref:`MultiplayerPeer.CONNECTION_CONNECTED<class_MultiplayerPeer_constant_CONNECTION_CONNECTED>` and :ref:`MultiplayerPeer.connection_succeeded<class_MultiplayerPeer_signal_connection_succeeded>` will not be emitted.
  60. If ``server_compatibilty`` is ``true`` the peer will suppress all :ref:`MultiplayerPeer.peer_connected<class_MultiplayerPeer_signal_peer_connected>` signals until a peer with id :ref:`MultiplayerPeer.TARGET_PEER_SERVER<class_MultiplayerPeer_constant_TARGET_PEER_SERVER>` connects and then emit :ref:`MultiplayerPeer.connection_succeeded<class_MultiplayerPeer_signal_connection_succeeded>`. After that the signal :ref:`MultiplayerPeer.peer_connected<class_MultiplayerPeer_signal_peer_connected>` will be emitted for every already connected peer, and any new peer that might connect. If the server peer disconnects after that, signal :ref:`MultiplayerPeer.server_disconnected<class_MultiplayerPeer_signal_server_disconnected>` will be emitted and state will become :ref:`MultiplayerPeer.CONNECTION_CONNECTED<class_MultiplayerPeer_constant_CONNECTION_CONNECTED>`.
  61. You can optionally specify a ``channels_config`` array of :ref:`TransferMode<enum_@GlobalScope_TransferMode>` which will be used to create extra channels (WebRTC only supports one transfer mode per channel).
  62. ----
  63. .. _class_WebRTCMultiplayerPeer_method_remove_peer:
  64. - void **remove_peer** **(** :ref:`int<class_int>` peer_id **)**
  65. Remove the peer with given ``peer_id`` from the mesh. If the peer was connected, and :ref:`MultiplayerPeer.peer_connected<class_MultiplayerPeer_signal_peer_connected>` was emitted for it, then :ref:`MultiplayerPeer.peer_disconnected<class_MultiplayerPeer_signal_peer_disconnected>` will be emitted.
  66. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  67. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  68. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  69. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  70. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  71. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`