class_multiplayerreplicator.rst 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 MultiplayerReplicator.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_MultiplayerReplicator:
  6. MultiplayerReplicator
  7. =====================
  8. **Inherits:** :ref:`Object<class_Object>`
  9. Methods
  10. -------
  11. +-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  12. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`decode_state<class_MultiplayerReplicator_method_decode_state>` **(** :ref:`int<class_int>` scene_id, :ref:`Object<class_Object>` object, :ref:`PackedByteArray<class_PackedByteArray>` data, :ref:`bool<class_bool>` initial=true **)** |
  13. +-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  14. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`despawn<class_MultiplayerReplicator_method_despawn>` **(** :ref:`int<class_int>` scene_id, :ref:`Object<class_Object>` object, :ref:`int<class_int>` peer_id=0 **)** |
  15. +-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  16. | :ref:`PackedByteArray<class_PackedByteArray>` | :ref:`encode_state<class_MultiplayerReplicator_method_encode_state>` **(** :ref:`int<class_int>` scene_id, :ref:`Object<class_Object>` object, :ref:`bool<class_bool>` initial=true **)** |
  17. +-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  18. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`send_despawn<class_MultiplayerReplicator_method_send_despawn>` **(** :ref:`int<class_int>` peer_id, :ref:`int<class_int>` scene_id, :ref:`Variant<class_Variant>` data=null, :ref:`NodePath<class_NodePath>` path=NodePath("") **)** |
  19. +-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  20. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`send_spawn<class_MultiplayerReplicator_method_send_spawn>` **(** :ref:`int<class_int>` peer_id, :ref:`int<class_int>` scene_id, :ref:`Variant<class_Variant>` data=null, :ref:`NodePath<class_NodePath>` path=NodePath("") **)** |
  21. +-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  22. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`send_sync<class_MultiplayerReplicator_method_send_sync>` **(** :ref:`int<class_int>` peer_id, :ref:`int<class_int>` scene_id, :ref:`PackedByteArray<class_PackedByteArray>` data, :ref:`TransferMode<enum_@GlobalScope_TransferMode>` transfer_mode=2, :ref:`int<class_int>` channel=0 **)** |
  23. +-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  24. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`spawn<class_MultiplayerReplicator_method_spawn>` **(** :ref:`int<class_int>` scene_id, :ref:`Object<class_Object>` object, :ref:`int<class_int>` peer_id=0 **)** |
  25. +-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  26. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`spawn_config<class_MultiplayerReplicator_method_spawn_config>` **(** :ref:`int<class_int>` scene_id, :ref:`ReplicationMode<enum_MultiplayerReplicator_ReplicationMode>` spawn_mode, :ref:`StringName[]<class_StringName>` properties=[], :ref:`Callable<class_Callable>` custom_send, :ref:`Callable<class_Callable>` custom_receive **)** |
  27. +-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  28. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`sync_all<class_MultiplayerReplicator_method_sync_all>` **(** :ref:`int<class_int>` scene_id, :ref:`int<class_int>` peer_id=0 **)** |
  29. +-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  30. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`sync_config<class_MultiplayerReplicator_method_sync_config>` **(** :ref:`int<class_int>` scene_id, :ref:`int<class_int>` interval, :ref:`StringName[]<class_StringName>` properties=[], :ref:`Callable<class_Callable>` custom_send, :ref:`Callable<class_Callable>` custom_receive **)** |
  31. +-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  32. | void | :ref:`track<class_MultiplayerReplicator_method_track>` **(** :ref:`int<class_int>` scene_id, :ref:`Object<class_Object>` object **)** |
  33. +-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  34. | void | :ref:`untrack<class_MultiplayerReplicator_method_untrack>` **(** :ref:`int<class_int>` scene_id, :ref:`Object<class_Object>` object **)** |
  35. +-----------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  36. Signals
  37. -------
  38. .. _class_MultiplayerReplicator_signal_despawn_requested:
  39. - **despawn_requested** **(** :ref:`int<class_int>` id, :ref:`int<class_int>` scene_id, :ref:`Node<class_Node>` parent, :ref:`String<class_String>` name, :ref:`PackedByteArray<class_PackedByteArray>` data **)**
  40. Emitted when a network despawn request has been received from a client, or for a :ref:`PackedScene<class_PackedScene>` that has been configured as :ref:`REPLICATION_MODE_CUSTOM<class_MultiplayerReplicator_constant_REPLICATION_MODE_CUSTOM>`.
  41. ----
  42. .. _class_MultiplayerReplicator_signal_despawned:
  43. - **despawned** **(** :ref:`int<class_int>` scene_id, :ref:`Node<class_Node>` node **)**
  44. Emitted on a client before deleting a local Node upon receiving a despawn request from the server.
  45. ----
  46. .. _class_MultiplayerReplicator_signal_replicated_instance_added:
  47. - **replicated_instance_added** **(** :ref:`int<class_int>` scene_id, :ref:`Node<class_Node>` node **)**
  48. Emitted when an instance of a :ref:`PackedScene<class_PackedScene>` that has been configured for networking enters the :ref:`SceneTree<class_SceneTree>`. See :ref:`spawn_config<class_MultiplayerReplicator_method_spawn_config>`.
  49. ----
  50. .. _class_MultiplayerReplicator_signal_replicated_instance_removed:
  51. - **replicated_instance_removed** **(** :ref:`int<class_int>` scene_id, :ref:`Node<class_Node>` node **)**
  52. Emitted when an instance of a :ref:`PackedScene<class_PackedScene>` that has been configured for networking leaves the :ref:`SceneTree<class_SceneTree>`. See :ref:`spawn_config<class_MultiplayerReplicator_method_spawn_config>`.
  53. ----
  54. .. _class_MultiplayerReplicator_signal_spawn_requested:
  55. - **spawn_requested** **(** :ref:`int<class_int>` id, :ref:`int<class_int>` scene_id, :ref:`Node<class_Node>` parent, :ref:`String<class_String>` name, :ref:`PackedByteArray<class_PackedByteArray>` data **)**
  56. Emitted when a network spawn request has been received from a client, or for a :ref:`PackedScene<class_PackedScene>` that has been configured as :ref:`REPLICATION_MODE_CUSTOM<class_MultiplayerReplicator_constant_REPLICATION_MODE_CUSTOM>`.
  57. ----
  58. .. _class_MultiplayerReplicator_signal_spawned:
  59. - **spawned** **(** :ref:`int<class_int>` scene_id, :ref:`Node<class_Node>` node **)**
  60. Emitted on a client after a new Node is instantiated locally and added to the SceneTree upon receiving a spawn request from the server.
  61. Enumerations
  62. ------------
  63. .. _enum_MultiplayerReplicator_ReplicationMode:
  64. .. _class_MultiplayerReplicator_constant_REPLICATION_MODE_NONE:
  65. .. _class_MultiplayerReplicator_constant_REPLICATION_MODE_SERVER:
  66. .. _class_MultiplayerReplicator_constant_REPLICATION_MODE_CUSTOM:
  67. enum **ReplicationMode**:
  68. - **REPLICATION_MODE_NONE** = **0** --- Used with :ref:`spawn_config<class_MultiplayerReplicator_method_spawn_config>` to identify a :ref:`PackedScene<class_PackedScene>` that should not be replicated.
  69. - **REPLICATION_MODE_SERVER** = **1** --- Used with :ref:`spawn_config<class_MultiplayerReplicator_method_spawn_config>` to identify a :ref:`PackedScene<class_PackedScene>` that should be automatically replicated from server to clients.
  70. - **REPLICATION_MODE_CUSTOM** = **2** --- Used with :ref:`spawn_config<class_MultiplayerReplicator_method_spawn_config>` to identify a :ref:`PackedScene<class_PackedScene>` that can be manually replicated among peers.
  71. Method Descriptions
  72. -------------------
  73. .. _class_MultiplayerReplicator_method_decode_state:
  74. - :ref:`Error<enum_@GlobalScope_Error>` **decode_state** **(** :ref:`int<class_int>` scene_id, :ref:`Object<class_Object>` object, :ref:`PackedByteArray<class_PackedByteArray>` data, :ref:`bool<class_bool>` initial=true **)**
  75. Decode the given ``data`` representing a spawnable state into ``object`` using the configuration associated with the provided ``scene_id``. This function is called automatically when a client receives a server spawn for a scene with :ref:`REPLICATION_MODE_SERVER<class_MultiplayerReplicator_constant_REPLICATION_MODE_SERVER>`. See :ref:`spawn_config<class_MultiplayerReplicator_method_spawn_config>`.
  76. Tip: You may find this function useful in servers when parsing spawn requests from clients, or when implementing your own logic with :ref:`REPLICATION_MODE_CUSTOM<class_MultiplayerReplicator_constant_REPLICATION_MODE_CUSTOM>`.
  77. ----
  78. .. _class_MultiplayerReplicator_method_despawn:
  79. - :ref:`Error<enum_@GlobalScope_Error>` **despawn** **(** :ref:`int<class_int>` scene_id, :ref:`Object<class_Object>` object, :ref:`int<class_int>` peer_id=0 **)**
  80. Request a despawn for the scene identified by ``scene_id`` to the given ``peer_id``. This will either trigger the default behaviour, or invoke the custom spawn/despawn callables specified in :ref:`spawn_config<class_MultiplayerReplicator_method_spawn_config>`. See :ref:`send_despawn<class_MultiplayerReplicator_method_send_despawn>` for the default behavior.
  81. ----
  82. .. _class_MultiplayerReplicator_method_encode_state:
  83. - :ref:`PackedByteArray<class_PackedByteArray>` **encode_state** **(** :ref:`int<class_int>` scene_id, :ref:`Object<class_Object>` object, :ref:`bool<class_bool>` initial=true **)**
  84. Encode the given ``object`` using the configuration associated with the provided ``scene_id``. This function is called automatically when the server spawns scenes with :ref:`REPLICATION_MODE_SERVER<class_MultiplayerReplicator_constant_REPLICATION_MODE_SERVER>`. See :ref:`spawn_config<class_MultiplayerReplicator_method_spawn_config>`.
  85. Tip: You may find this function useful when requesting spawns from clients to server, or when implementing your own logic with :ref:`REPLICATION_MODE_CUSTOM<class_MultiplayerReplicator_constant_REPLICATION_MODE_CUSTOM>`.
  86. ----
  87. .. _class_MultiplayerReplicator_method_send_despawn:
  88. - :ref:`Error<enum_@GlobalScope_Error>` **send_despawn** **(** :ref:`int<class_int>` peer_id, :ref:`int<class_int>` scene_id, :ref:`Variant<class_Variant>` data=null, :ref:`NodePath<class_NodePath>` path=NodePath("") **)**
  89. Sends a despawn request for the scene identified by ``scene_id`` to the given ``peer_id`` (see :ref:`MultiplayerPeer.set_target_peer<class_MultiplayerPeer_method_set_target_peer>`). If the scene is configured as :ref:`REPLICATION_MODE_SERVER<class_MultiplayerReplicator_constant_REPLICATION_MODE_SERVER>` (see :ref:`spawn_config<class_MultiplayerReplicator_method_spawn_config>`) and the request is sent by the server (see :ref:`MultiplayerAPI.is_server<class_MultiplayerAPI_method_is_server>`), the receiving peer(s) will automatically queue for deletion the node at ``path`` and emit the signal :ref:`despawned<class_MultiplayerReplicator_signal_despawned>`. In all other cases no deletion happens, and the signal :ref:`despawn_requested<class_MultiplayerReplicator_signal_despawn_requested>` is emitted instead.
  90. ----
  91. .. _class_MultiplayerReplicator_method_send_spawn:
  92. - :ref:`Error<enum_@GlobalScope_Error>` **send_spawn** **(** :ref:`int<class_int>` peer_id, :ref:`int<class_int>` scene_id, :ref:`Variant<class_Variant>` data=null, :ref:`NodePath<class_NodePath>` path=NodePath("") **)**
  93. Sends a spawn request for the scene identified by ``scene_id`` to the given ``peer_id`` (see :ref:`MultiplayerPeer.set_target_peer<class_MultiplayerPeer_method_set_target_peer>`). If the scene is configured as :ref:`REPLICATION_MODE_SERVER<class_MultiplayerReplicator_constant_REPLICATION_MODE_SERVER>` (see :ref:`spawn_config<class_MultiplayerReplicator_method_spawn_config>`) and the request is sent by the server (see :ref:`MultiplayerAPI.is_server<class_MultiplayerAPI_method_is_server>`), the receiving peer(s) will automatically instantiate that scene, add it to the :ref:`SceneTree<class_SceneTree>` at the given ``path`` and emit the signal :ref:`spawned<class_MultiplayerReplicator_signal_spawned>`. In all other cases no instantiation happens, and the signal :ref:`spawn_requested<class_MultiplayerReplicator_signal_spawn_requested>` is emitted instead.
  94. ----
  95. .. _class_MultiplayerReplicator_method_send_sync:
  96. - :ref:`Error<enum_@GlobalScope_Error>` **send_sync** **(** :ref:`int<class_int>` peer_id, :ref:`int<class_int>` scene_id, :ref:`PackedByteArray<class_PackedByteArray>` data, :ref:`TransferMode<enum_@GlobalScope_TransferMode>` transfer_mode=2, :ref:`int<class_int>` channel=0 **)**
  97. Sends a sync request for the instances of the scene identified by ``scene_id`` to the given ``peer_id`` (see :ref:`MultiplayerPeer.set_target_peer<class_MultiplayerPeer_method_set_target_peer>`). This function can only be called manually when overriding the send and receive sync functions (see :ref:`sync_config<class_MultiplayerReplicator_method_sync_config>`).
  98. ----
  99. .. _class_MultiplayerReplicator_method_spawn:
  100. - :ref:`Error<enum_@GlobalScope_Error>` **spawn** **(** :ref:`int<class_int>` scene_id, :ref:`Object<class_Object>` object, :ref:`int<class_int>` peer_id=0 **)**
  101. Request a spawn for the scene identified by ``scene_id`` to the given ``peer_id``. This will either trigger the default behaviour, or invoke the custom spawn/despawn callables specified in :ref:`spawn_config<class_MultiplayerReplicator_method_spawn_config>`. See :ref:`send_spawn<class_MultiplayerReplicator_method_send_spawn>` for the default behavior.
  102. ----
  103. .. _class_MultiplayerReplicator_method_spawn_config:
  104. - :ref:`Error<enum_@GlobalScope_Error>` **spawn_config** **(** :ref:`int<class_int>` scene_id, :ref:`ReplicationMode<enum_MultiplayerReplicator_ReplicationMode>` spawn_mode, :ref:`StringName[]<class_StringName>` properties=[], :ref:`Callable<class_Callable>` custom_send, :ref:`Callable<class_Callable>` custom_receive **)**
  105. Configures the MultiplayerReplicator to track instances of the :ref:`PackedScene<class_PackedScene>` identified by ``scene_id`` (see :ref:`ResourceLoader.get_resource_uid<class_ResourceLoader_method_get_resource_uid>`) for the purpose of network replication. When ``mode`` is :ref:`REPLICATION_MODE_SERVER<class_MultiplayerReplicator_constant_REPLICATION_MODE_SERVER>`, the specified ``properties`` will also be replicated to clients during the initial spawn. You can optionally specify a ``custom_send`` and a ``custom_receive`` to override the default behaviour and customize the spawn/despawn proecess.
  106. Tip: You can use a custom property in the scene main script to return a customly optimized state representation.
  107. ----
  108. .. _class_MultiplayerReplicator_method_sync_all:
  109. - :ref:`Error<enum_@GlobalScope_Error>` **sync_all** **(** :ref:`int<class_int>` scene_id, :ref:`int<class_int>` peer_id=0 **)**
  110. Manually request a sync for all the instances of the scene identified by ``scene_id``. This function will trigger the default sync behaviour, or call your send custom send callable if specified in :ref:`sync_config<class_MultiplayerReplicator_method_sync_config>`.
  111. **Note:** The default implementation only allow syncing from server to clients.
  112. ----
  113. .. _class_MultiplayerReplicator_method_sync_config:
  114. - :ref:`Error<enum_@GlobalScope_Error>` **sync_config** **(** :ref:`int<class_int>` scene_id, :ref:`int<class_int>` interval, :ref:`StringName[]<class_StringName>` properties=[], :ref:`Callable<class_Callable>` custom_send, :ref:`Callable<class_Callable>` custom_receive **)**
  115. Configures the MultiplayerReplicator to sync instances of the :ref:`PackedScene<class_PackedScene>` identified by ``scene_id`` (see :ref:`ResourceLoader.get_resource_uid<class_ResourceLoader_method_get_resource_uid>`) for the purpose of network replication at the desired ``interval`` (in milliseconds). The specified ``properties`` will be part of the state sync. You can optionally specify a ``custom_send`` and a ``custom_receive`` to override the default behaviour and customize the syncronization proecess.
  116. Tip: You can use a custom property in the scene main script to return a customly optimized state representation (having a single property that returns a PackedByteArray is higly recommended when dealing with many instances).
  117. ----
  118. .. _class_MultiplayerReplicator_method_track:
  119. - void **track** **(** :ref:`int<class_int>` scene_id, :ref:`Object<class_Object>` object **)**
  120. Track the given ``object`` as an instance of the scene identified by ``scene_id``. This object will be passed to your custom sync callables (see :ref:`sync_config<class_MultiplayerReplicator_method_sync_config>`). Tracking and untracking is automatic in :ref:`REPLICATION_MODE_SERVER<class_MultiplayerReplicator_constant_REPLICATION_MODE_SERVER>`.
  121. ----
  122. .. _class_MultiplayerReplicator_method_untrack:
  123. - void **untrack** **(** :ref:`int<class_int>` scene_id, :ref:`Object<class_Object>` object **)**
  124. Untrack the given ``object``. This object will no longer be passed to your custom sync callables (see :ref:`sync_config<class_MultiplayerReplicator_method_sync_config>`). Tracking and untracking is automatic in :ref:`REPLICATION_MODE_SERVER<class_MultiplayerReplicator_constant_REPLICATION_MODE_SERVER>`.
  125. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  126. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  127. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  128. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  129. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  130. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`