Table of Contents

Class MultiplayerApi

Namespace
Godot
Assembly
GodotSharp.dll

Base class for high-level multiplayer API implementations. See also MultiplayerPeer.

By default, SceneTree has a reference to an implementation of this class and uses it to provide multiplayer capabilities (i.e. RPCs) across the whole scene.

It is possible to override the MultiplayerAPI instance used by specific tree branches by calling the SetMultiplayer(MultiplayerApi, NodePath) method, effectively allowing to run both client and server in the same scene.

It is also possible to extend or replace the default implementation via scripting or native extensions. See MultiplayerApiExtension for details about extensions, SceneMultiplayer for the details about the default implementation.

[GodotClassName("MultiplayerAPI")]
public class MultiplayerApi : RefCounted, IDisposable
Inheritance
MultiplayerApi
Implements
Derived
Inherited Members

Properties

MultiplayerPeer

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 IsServer()) and will set root node's network mode to authority, or it will become a regular client peer. 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.

public MultiplayerPeer MultiplayerPeer { get; set; }

Property Value

MultiplayerPeer

Methods

CreateDefaultInterface()

Returns a new instance of the default MultiplayerAPI.

public static MultiplayerApi CreateDefaultInterface()

Returns

MultiplayerApi

GetDefaultInterface()

Returns the default MultiplayerAPI implementation class name. This is usually "SceneMultiplayer" when SceneMultiplayer is available. See SetDefaultInterface(StringName).

public static StringName GetDefaultInterface()

Returns

StringName

GetPeers()

Returns the peer IDs of all connected peers of this MultiplayerAPI's MultiplayerPeer.

public int[] GetPeers()

Returns

int[]

GetRemoteSenderId()

Returns the sender's peer ID for the RPC currently being executed.

Note: If not inside an RPC this method will return 0.

public int GetRemoteSenderId()

Returns

int

GetUniqueId()

Returns the unique peer ID of this MultiplayerAPI's 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

HasMultiplayerPeer()

Returns true if there is a MultiplayerPeer set.

public bool HasMultiplayerPeer()

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

IsServer()

Returns true if this MultiplayerAPI's MultiplayerPeer is valid and in server mode (listening for connections).

public bool IsServer()

Returns

bool

ObjectConfigurationAdd(GodotObject, Variant)

Notifies the MultiplayerAPI of a new configuration for the given object. This method is used internally by SceneTree to configure the root path for this MultiplayerAPI (passing null and a valid NodePath as configuration). This method can be further used by MultiplayerAPI implementations to provide additional features, refer to specific implementation (e.g. SceneMultiplayer) for details on how they use it.

Note: This method is mostly relevant when extending or overriding the MultiplayerAPI behavior via MultiplayerApiExtension.

public Error ObjectConfigurationAdd(GodotObject @object, Variant configuration)

Parameters

object GodotObject
configuration Variant

Returns

Error

ObjectConfigurationRemove(GodotObject, Variant)

Notifies the MultiplayerAPI to remove a configuration for the given object. This method is used internally by SceneTree to configure the root path for this MultiplayerAPI (passing null and an empty NodePath as configuration). This method can be further used by MultiplayerAPI implementations to provide additional features, refer to specific implementation (e.g. SceneMultiplayer) for details on how they use it.

Note: This method is mostly relevant when extending or overriding the MultiplayerAPI behavior via MultiplayerApiExtension.

public Error ObjectConfigurationRemove(GodotObject @object, Variant configuration)

Parameters

object GodotObject
configuration Variant

Returns

Error

Poll()

Method used for polling the MultiplayerAPI. You only need to worry about this if you set MultiplayerPoll to false. By default, SceneTree will poll its MultiplayerAPI(s) for you.

Note: This method results in RPCs being called, so they will be executed in the same context of this function (e.g. _process, physics, GodotThread).

public Error Poll()

Returns

Error

Rpc(int, GodotObject, StringName, Array)

Sends an RPC to the target peer. The given method will be called on the remote object with the provided arguments. The RPC may also be called locally depending on the implementation and RPC configuration. See Rpc(StringName, params Variant[]) and RpcConfig(StringName, Variant).

Note: Prefer using Rpc(StringName, params Variant[]), RpcId(long, StringName, params Variant[]), or my_method.rpc(peer, arg1, arg2, ...) (in GDScript), since they are faster. This method is mostly useful in conjunction with MultiplayerApiExtension when augmenting or replacing the multiplayer capabilities.

public Error Rpc(int peer, GodotObject @object, StringName method, Array arguments = null)

Parameters

peer int
object GodotObject
method StringName
arguments Array

Returns

Error

SetDefaultInterface(StringName)

Sets the default MultiplayerAPI implementation class. This method can be used by modules and extensions to configure which implementation will be used by SceneTree when the engine starts.

public static void SetDefaultInterface(StringName interfaceName)

Parameters

interfaceName StringName

Events

ConnectedToServer

Emitted when this MultiplayerAPI's MultiplayerPeer successfully connected to a server. Only emitted on clients.

public event Action ConnectedToServer

Event Type

Action

ConnectionFailed

Emitted when this MultiplayerAPI's MultiplayerPeer fails to establish a connection to a server. Only emitted on clients.

public event Action ConnectionFailed

Event Type

Action

PeerConnected

Emitted when this MultiplayerAPI's MultiplayerPeer 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).

public event MultiplayerApi.PeerConnectedEventHandler PeerConnected

Event Type

MultiplayerApi.PeerConnectedEventHandler

PeerDisconnected

Emitted when this MultiplayerAPI's MultiplayerPeer disconnects from a peer. Clients get notified when other clients disconnect from the same server.

public event MultiplayerApi.PeerDisconnectedEventHandler PeerDisconnected

Event Type

MultiplayerApi.PeerDisconnectedEventHandler

ServerDisconnected

Emitted when this MultiplayerAPI's MultiplayerPeer disconnects from server. Only emitted on clients.

public event Action ServerDisconnected

Event Type

Action