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
TargetPeerServer
Packets are sent to the remote peer acting as server.
public const long TargetPeerServer = 1
Field Value
Properties
RefuseNewConnections
If true
, this MultiplayerPeer refuses new connections.
public bool RefuseNewConnections { get; set; }
Property Value
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
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
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
GenerateUniqueId()
Returns a randomly generated integer that can be used as a network unique ID.
public uint GenerateUniqueId()
Returns
GetConnectionStatus()
Returns the current state of the connection. See MultiplayerPeer.ConnectionStatus.
public MultiplayerPeer.ConnectionStatus GetConnectionStatus()
Returns
GetPacketChannel()
Returns the channel over which the next available packet was received. See GetAvailablePacketCount().
public int GetPacketChannel()
Returns
GetPacketMode()
Returns the MultiplayerPeer.TransferModeEnum the remote peer used to send the next available packet. See GetAvailablePacketCount().
public MultiplayerPeer.TransferModeEnum GetPacketMode()
Returns
GetPacketPeer()
Returns the ID of the MultiplayerPeer who sent the next available packet. See GetAvailablePacketCount().
public int GetPacketPeer()
Returns
GetUniqueId()
Returns the ID of this MultiplayerPeer.
public int GetUniqueId()
Returns
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_nameName of the method to check for.
Returns
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_nameName of the signal to check for.
Returns
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_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
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
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
PeerDisconnected
Emitted when a remote peer has disconnected.
public event MultiplayerPeer.PeerDisconnectedEventHandler PeerDisconnected