Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

SceneMultiplayer

繼承: MultiplayerAPI < RefCounted < Object

高階多人遊戲 API 實作。

說明

這個類是 MultiplayerAPI 的預設實作,用於在 Godot 引擎中提供多人遊戲功能。

該實作通過 Node.rpc()Node.rpc_id() 來支援 RPC,需要向 MultiplayerAPI.rpc() 傳遞一個 Node(傳入其他物件型別會導致失敗)。

該實作還提供了 SceneTree 複製功能,使用的是 MultiplayerSpawnerMultiplayerSynchronizer 節點,以及 SceneReplicationConfig 資源,。

注意:高階多人遊戲 API 協議屬於實作細節,並不打算提供給非 Godot 伺服器使用。對協議的更改可能不會進行提前通知。

注意:匯出到 Android 時,在匯出專案或使用一鍵部署之前,請務必在安卓導出預設中開啟 INTERNET 許可權。否則,任何型別的網路通信都將被 Android 阻止。

屬性

bool

allow_object_decoding

false

Callable

auth_callback

Callable()

float

auth_timeout

3.0

int

max_delta_packet_size

65535

int

max_sync_packet_size

1350

bool

refuse_new_connections

false

NodePath

root_path

NodePath("")

bool

server_relay

true

方法

void

clear()

Error

complete_auth(id: int)

void

disconnect_peer(id: int)

PackedInt32Array

get_authenticating_peers()

Error

send_auth(id: int, data: PackedByteArray)

Error

send_bytes(bytes: PackedByteArray, id: int = 0, mode: TransferMode = 2, channel: int = 0)


訊號

peer_authenticating(id: int) 🔗

當此 MultiplayerAPI 的 MultiplayerAPI.multiplayer_peer 連接到一個新對等體並設定有效的 auth_callback 時,將發出此訊號。在這種情況下,只有在呼叫 complete_auth() 並傳遞給定的對等體 id 時,才會發出 MultiplayerAPI.peer_connected。在此狀態下,該對等體不會包含在 MultiplayerAPI.get_peers() 返回的列表中(但會包含在 get_authenticating_peers() 返回的列表中),且只會發送和接收身份驗證資料。有關發送身份驗證資料的資訊,請參閱 send_auth()


peer_authentication_failed(id: int) 🔗

當這個 MultiplayerAPI 的 MultiplayerAPI.multiplayer_peer 與另一個尚未完成授權的對等體斷開連接時觸發。見 peer_authenticating


peer_packet(id: int, packet: PackedByteArray) 🔗

當這個 MultiplayerAPI 的 MultiplayerAPI.multiplayer_peer 收到帶有自定義資料(見 send_bytes())的 packet 時發出。ID 是發送封包的對等體的對等體 ID。


屬性說明

bool allow_object_decoding = false 🔗

  • void set_allow_object_decoding(value: bool)

  • bool is_object_decoding_allowed()

如果為 true,則 MultiplayerAPI 將允許在遠端程式呼叫期間對對象進行編碼和解碼。

警告:反序列化的物件可能包含要執行的程式碼。如果序列化的物件來自不受信任的來源,請勿使用此選項,以避免潛在的安全威脅,例如遠端程式碼執行。


Callable auth_callback = Callable() 🔗

The callback to execute when receiving authentication data sent via send_auth(). If the Callable is empty (default), peers will be automatically accepted as soon as they connect.


float auth_timeout = 3.0 🔗

  • void set_auth_timeout(value: float)

  • float get_auth_timeout()

If set to a value greater than 0.0, the maximum duration in seconds peers can stay in the authenticating state, after which the authentication will automatically fail. See the peer_authenticating and peer_authentication_failed signals.


int max_delta_packet_size = 65535 🔗

  • void set_max_delta_packet_size(value: int)

  • int get_max_delta_packet_size()

差異量封包的最大大小。值越大,單影格能夠收到完整更新的幾率就越大,但造成網路擁堵的幾率也越大(網路延遲、斷線等)。見 MultiplayerSynchronizer


int max_sync_packet_size = 1350 🔗

  • void set_max_sync_packet_size(value: int)

  • int get_max_sync_packet_size()

同步封包的最大大小。值越大,單影格能夠收到完整更新的幾率就越大,但丟包的幾率也越大。見 MultiplayerSynchronizer


bool refuse_new_connections = false 🔗

  • void set_refuse_new_connections(value: bool)

  • bool is_refusing_new_connections()

如果為 true,則 MultiplayerAPI 的 MultiplayerAPI.multiplayer_peer 將拒絕新的傳入連接。


NodePath root_path = NodePath("") 🔗

用於 RPC 和複製的根路徑。將使用相對路徑,而不是絕對路徑,來搜尋 RPC 應該在其上被執行的節點。

這有效地允許場景樹的不同分支,由不同的 MultiplayerAPI 管理,例如允許在同一場景中,同時運作使用者端和伺服器。


bool server_relay = true 🔗

  • void set_server_relay_enabled(value: bool)

  • bool is_server_relay_enabled()

啟用或禁用伺服器功能,該功能通知使用者端其他對等體的連接/斷開,並在它們之間轉發消息。當此選項為 false 時,使用者端將不會自動收到其他對等體的通知,也無法通過伺服器向它們發送封包。

注意:在其他對等體連接時更改此選項可能會導致意外行為。

注意:對該功能的支援可能取決於目前的 MultiplayerPeer 配置。請參閱 MultiplayerPeer.is_server_relay_supported()


方法說明

void clear() 🔗

清除目前 SceneMultiplayer 的網路狀態(除非你知道你在做什麼,否則不應該呼叫這個函式)。


Error complete_auth(id: int) 🔗

完成對由 id 標識的遠端對等體的身份驗證步驟。遠端端也完成身份驗證之後,將會為這個對等體發出 MultiplayerAPI.peer_connected 訊號。不會再從此對等體接收到身份驗證消息。

如果對等體在完成身份驗證之前斷開連接,無論是由於網路問題、auth_timeout 過期還是手動呼叫 disconnect_peer(),都會發出 peer_authentication_failed 訊號,而不是 MultiplayerAPI.peer_disconnected 訊號。


void disconnect_peer(id: int) 🔗

斷開由 id 標識的對等體的連接,將其從連接的對等體列表中移除,並關閉與它的底層連接。


PackedInt32Array get_authenticating_peers() 🔗

返回目前嘗試使用此 MultiplayerAPI 進行身份驗證的對等方的 ID。


Error send_auth(id: int, data: PackedByteArray) 🔗

將指定的 data 作為身份驗證消息的一部分發送到由 id 標識的遠端對等方。這可用於驗證對等方,並控制何時發出 MultiplayerAPI.peer_connected(並接受遠端對等方作為連接的對等方之一)。


Error send_bytes(bytes: PackedByteArray, id: int = 0, mode: TransferMode = 2, channel: int = 0) 🔗

向由 id 標識的特定對等體發送給定的原始位元組 bytes(見 MultiplayerPeer.set_target_peer())。預設 ID 為 0,即廣播到所有對等體。