Table of Contents

Class MultiplayerPeer

Namespace
Godot
Assembly
GodotSharp.dll

Manages the connection with one or more remote peers acting as server or client and assigning unique IDs to each of them. See also MultiplayerApi.

Note: The 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.

public class MultiplayerPeer : PacketPeer, IDisposable
Inheritance
MultiplayerPeer
Implements
Derived
Inherited Members

Fields

TargetPeerBroadcast

Packets are sent to all connected peers.

public const long TargetPeerBroadcast = 0

Field Value

long

TargetPeerServer

Packets are sent to the remote peer acting as server.

public const long TargetPeerServer = 1

Field Value

long

Properties

RefuseNewConnections

If true, this MultiplayerPeer refuses new connections.

public bool RefuseNewConnections { get; set; }

Property Value

bool

TransferChannel

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 MultiplayerPeer.TransferModeEnum) so that Reliable and UnreliableOrdered 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.

public int TransferChannel { get; set; }

Property Value

int

TransferMode

The manner in which to send packets to the target peer. See MultiplayerPeer.TransferModeEnum, and the SetTargetPeer(int) method.

public MultiplayerPeer.TransferModeEnum TransferMode { get; set; }

Property Value

MultiplayerPeer.TransferModeEnum

Methods

Close()

Immediately close the multiplayer peer returning to the state Disconnected. Connected peers will be dropped without emitting PeerDisconnected.

public void Close()

DisconnectPeer(int, bool)

Disconnects the given peer from this host. If force is true the PeerDisconnected signal will not be emitted for this peer.

public void DisconnectPeer(int peer, bool force = false)

Parameters

peer int
force bool

GenerateUniqueId()

Returns a randomly generated integer that can be used as a network unique ID.

public uint GenerateUniqueId()

Returns

uint

GetConnectionStatus()

Returns the current state of the connection. See MultiplayerPeer.ConnectionStatus.

public MultiplayerPeer.ConnectionStatus GetConnectionStatus()

Returns

MultiplayerPeer.ConnectionStatus

GetPacketChannel()

Returns the channel over which the next available packet was received. See GetAvailablePacketCount().

public int GetPacketChannel()

Returns

int

GetPacketMode()

Returns the MultiplayerPeer.TransferModeEnum the remote peer used to send the next available packet. See GetAvailablePacketCount().

public MultiplayerPeer.TransferModeEnum GetPacketMode()

Returns

MultiplayerPeer.TransferModeEnum

GetPacketPeer()

Returns the ID of the MultiplayerPeer who sent the next available packet. See GetAvailablePacketCount().

public int GetPacketPeer()

Returns

int

GetUniqueId()

Returns the ID of this MultiplayerPeer.

public int GetUniqueId()

Returns

int

HasGodotClassMethod(in godot_string_name)

Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.

protected override bool HasGodotClassMethod(in godot_string_name method)

Parameters

method godot_string_name

Name of the method to check for.

Returns

bool

HasGodotClassSignal(in godot_string_name)

Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.

protected override bool HasGodotClassSignal(in godot_string_name signal)

Parameters

signal godot_string_name

Name of the signal to check for.

Returns

bool

InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)

Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.

protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)

Parameters

method godot_string_name

Name of the method to invoke.

args NativeVariantPtrArgs

Arguments to use with the invoked method.

ret godot_variant

Value returned by the invoked method.

Returns

bool

IsServerRelaySupported()

Returns true if the server can act as a relay in the current configuration (i.e. if the higher level MultiplayerApi should notify connected clients of other peers, and implement a relay protocol to allow communication between them).

public bool IsServerRelaySupported()

Returns

bool

Poll()

Waits up to 1 second to receive a new network event.

public void Poll()

SetTargetPeer(int)

Sets the peer to which packets will be sent.

The id can be one of: TargetPeerBroadcast to send to all connected peers, TargetPeerServer 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 TargetPeerBroadcast.

public void SetTargetPeer(int id)

Parameters

id int

Events

PeerConnected

Emitted when a remote peer connects.

public event MultiplayerPeer.PeerConnectedEventHandler PeerConnected

Event Type

MultiplayerPeer.PeerConnectedEventHandler

PeerDisconnected

Emitted when a remote peer has disconnected.

public event MultiplayerPeer.PeerDisconnectedEventHandler PeerDisconnected

Event Type

MultiplayerPeer.PeerDisconnectedEventHandler