class_multiplayerapi.rst 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. .. Generated automatically by doc/tools/makerst.py in Godot's source tree.
  2. .. DO NOT EDIT THIS FILE, but the MultiplayerAPI.xml source instead.
  3. .. The source is found in doc/classes or modules/<name>/doc_classes.
  4. .. _class_MultiplayerAPI:
  5. MultiplayerAPI
  6. ==============
  7. **Inherits:** :ref:`Reference<class_reference>` **<** :ref:`Object<class_object>`
  8. **Category:** Core
  9. Brief Description
  10. -----------------
  11. High Level Multiplayer API.
  12. Member Functions
  13. ----------------
  14. +------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
  15. | void | :ref:`clear<class_MultiplayerAPI_clear>` **(** **)** |
  16. +------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
  17. | :ref:`PoolIntArray<class_poolintarray>` | :ref:`get_network_connected_peers<class_MultiplayerAPI_get_network_connected_peers>` **(** **)** const |
  18. +------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
  19. | :ref:`int<class_int>` | :ref:`get_network_unique_id<class_MultiplayerAPI_get_network_unique_id>` **(** **)** const |
  20. +------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
  21. | :ref:`int<class_int>` | :ref:`get_rpc_sender_id<class_MultiplayerAPI_get_rpc_sender_id>` **(** **)** const |
  22. +------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
  23. | :ref:`bool<class_bool>` | :ref:`has_network_peer<class_MultiplayerAPI_has_network_peer>` **(** **)** const |
  24. +------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
  25. | :ref:`bool<class_bool>` | :ref:`is_network_server<class_MultiplayerAPI_is_network_server>` **(** **)** const |
  26. +------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
  27. | void | :ref:`poll<class_MultiplayerAPI_poll>` **(** **)** |
  28. +------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
  29. | :ref:`Error<enum_@globalscope_error>` | :ref:`send_bytes<class_MultiplayerAPI_send_bytes>` **(** :ref:`PoolByteArray<class_poolbytearray>` bytes, :ref:`int<class_int>` id=0 **)** |
  30. +------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
  31. | void | :ref:`set_root_node<class_MultiplayerAPI_set_root_node>` **(** :ref:`Node<class_node>` node **)** |
  32. +------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
  33. Signals
  34. -------
  35. .. _class_MultiplayerAPI_connected_to_server:
  36. - **connected_to_server** **(** **)**
  37. Emitted whenever this MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_network_peer>` successfully connected to a server. Only emitted on clients.
  38. .. _class_MultiplayerAPI_connection_failed:
  39. - **connection_failed** **(** **)**
  40. Emitted whenever this MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_network_peer>` fails to establish a connection to a server. Only emitted on clients.
  41. .. _class_MultiplayerAPI_network_peer_connected:
  42. - **network_peer_connected** **(** :ref:`int<class_int>` id **)**
  43. Emitted whenever this MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_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).
  44. .. _class_MultiplayerAPI_network_peer_disconnected:
  45. - **network_peer_disconnected** **(** :ref:`int<class_int>` id **)**
  46. Emitted whenever this MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_network_peer>` disconnects from a peer. Clients get notified when other clients disconnect from the same server.
  47. .. _class_MultiplayerAPI_network_peer_packet:
  48. - **network_peer_packet** **(** :ref:`int<class_int>` id, :ref:`PoolByteArray<class_poolbytearray>` packet **)**
  49. Emitted whenever this MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_network_peer>` receive a ``packet`` with custom data (see :ref:`send_bytes<class_MultiplayerAPI_send_bytes>`). ID is the peer ID of the peer that sent the packet.
  50. .. _class_MultiplayerAPI_server_disconnected:
  51. - **server_disconnected** **(** **)**
  52. Emitted whenever this MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_network_peer>` disconnects from server. Only emitted on clients.
  53. Member Variables
  54. ----------------
  55. .. _class_MultiplayerAPI_network_peer:
  56. - :ref:`NetworkedMultiplayerPeer<class_networkedmultiplayerpeer>` **network_peer** - 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_is_network_server>`) and will set root node's network mode to master (see NETWORK_MODE\_\* constants in :ref:`Node<class_node>`), or it will become a regular peer with root node set to slave. 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.
  57. .. _class_MultiplayerAPI_refuse_new_network_connections:
  58. - :ref:`bool<class_bool>` **refuse_new_network_connections** - If ``true`` the MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_network_peer>` refuses new incoming connections.
  59. Description
  60. -----------
  61. This class implements most of the logic behind the high level multiplayer API.
  62. 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.
  63. It is possible to override the MultiplayerAPI instance used by specific Nodes by setting the :ref:`Node.custom_multiplayer<class_Node_custom_multiplayer>` property, effectively allowing to run both client and server in the same scene.
  64. Member Function Description
  65. ---------------------------
  66. .. _class_MultiplayerAPI_clear:
  67. - void **clear** **(** **)**
  68. Clears the current MultiplayerAPI network state (you shouldn't call this unless you know what you are doing).
  69. .. _class_MultiplayerAPI_get_network_connected_peers:
  70. - :ref:`PoolIntArray<class_poolintarray>` **get_network_connected_peers** **(** **)** const
  71. Returns the peer IDs of all connected peers of this MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_network_peer>`.
  72. .. _class_MultiplayerAPI_get_network_unique_id:
  73. - :ref:`int<class_int>` **get_network_unique_id** **(** **)** const
  74. Returns the unique peer ID of this MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_network_peer>`.
  75. .. _class_MultiplayerAPI_get_rpc_sender_id:
  76. - :ref:`int<class_int>` **get_rpc_sender_id** **(** **)** const
  77. Returns the sender's peer ID for the RPC currently being executed.
  78. NOTE: If not inside an RPC this method will return 0.
  79. .. _class_MultiplayerAPI_has_network_peer:
  80. - :ref:`bool<class_bool>` **has_network_peer** **(** **)** const
  81. Returns ``true`` if there is a :ref:`network_peer<class_MultiplayerAPI_network_peer>` set.
  82. .. _class_MultiplayerAPI_is_network_server:
  83. - :ref:`bool<class_bool>` **is_network_server** **(** **)** const
  84. Returns ``true`` if this MultiplayerAPI's :ref:`network_peer<class_MultiplayerAPI_network_peer>` is in server mode (listening for connections).
  85. .. _class_MultiplayerAPI_poll:
  86. - void **poll** **(** **)**
  87. Method used for polling the MultiplayerAPI.
  88. You only need to worry about this if you are using :ref:`Node.custom_multiplayer<class_Node_custom_multiplayer>` override.
  89. SceneTree will poll the default MultiplayerAPI for you.
  90. .. _class_MultiplayerAPI_send_bytes:
  91. - :ref:`Error<enum_@globalscope_error>` **send_bytes** **(** :ref:`PoolByteArray<class_poolbytearray>` bytes, :ref:`int<class_int>` id=0 **)**
  92. Sends the given raw ``bytes`` to a specific peer identified by ``id`` (see :ref:`NetworkedMultiplayerPeer.set_target_peer<class_NetworkedMultiplayerPeer_set_target_peer>`). Default ID is ``0``, i.e. broadcast to all peers.
  93. .. _class_MultiplayerAPI_set_root_node:
  94. - void **set_root_node** **(** :ref:`Node<class_node>` node **)**
  95. Sets the base 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.
  96. 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.