MultiplayerAPI

package
v0.0.0-...-357ca8a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2025 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

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 SceneTree.SetMultiplayer 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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDefaultInterface

func GetDefaultInterface() string

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

func SetDefaultInterface

func SetDefaultInterface(interface_name string)

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.

Types

type Advanced

type Advanced = class

Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.

type Any

type Any interface {
	gd.IsClass
	AsMultiplayerAPI() Instance
}

type Expanded

type Expanded = MoreArgs

type Extension

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension

func (*Extension[T]) AsMultiplayerAPI

func (self *Extension[T]) AsMultiplayerAPI() Instance

func (*Extension[T]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

func (*Extension[T]) AsRefCounted

func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted

type ID

type ID Object.ID

ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

type Instance

type Instance [1]gdclass.MultiplayerAPI

Instance of the class with convieniently typed arguments and results.

var Nil Instance

Nil is a nil/null instance of the class. Equivalent to the zero value.

func CreateDefaultInterface

func CreateDefaultInterface() Instance

Returns a new instance of the default MultiplayerAPI.

func New

func New() Instance

func (Instance) AsMultiplayerAPI

func (self Instance) AsMultiplayerAPI() Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsRefCounted

func (self Instance) AsRefCounted() [1]gd.RefCounted

func (Instance) GetPeers

func (self Instance) GetPeers() []int32

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

func (Instance) GetRemoteSenderId

func (self Instance) GetRemoteSenderId() int

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

Note: This method returns 0 when called outside of an RPC. As such, the original peer ID may be lost when code execution is delayed (such as with GDScript's await keyword).

func (Instance) GetUniqueId

func (self Instance) GetUniqueId() int

Returns the unique peer ID of this MultiplayerAPI's MultiplayerPeer.

func (Instance) HasMultiplayerPeer

func (self Instance) HasMultiplayerPeer() bool

Returns true if there is a MultiplayerPeer set.

func (Instance) ID

func (self Instance) ID() ID

func (Instance) IsServer

func (self Instance) IsServer() bool

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

func (Instance) MoreArgs

func (self Instance) MoreArgs() MoreArgs

MoreArgs enables certain functions to be called with additional 'optional' arguments.

func (Instance) MultiplayerPeer

func (self Instance) MultiplayerPeer() MultiplayerPeer.Instance

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.

func (Instance) ObjectConfigurationAdd

func (self Instance) ObjectConfigurationAdd(obj Object.Instance, configuration any) error

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 node path 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.

func (Instance) ObjectConfigurationRemove

func (self Instance) ObjectConfigurationRemove(obj Object.Instance, configuration any) error

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 node path 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.

func (Instance) OnConnectedToServer

func (self Instance) OnConnectedToServer(cb func(), flags ...Signal.Flags)

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

func (Instance) OnConnectionFailed

func (self Instance) OnConnectionFailed(cb func(), flags ...Signal.Flags)

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

func (Instance) OnPeerConnected

func (self Instance) OnPeerConnected(cb func(id int), flags ...Signal.Flags)

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).

func (Instance) OnPeerDisconnected

func (self Instance) OnPeerDisconnected(cb func(id int), flags ...Signal.Flags)

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

func (Instance) OnServerDisconnected

func (self Instance) OnServerDisconnected(cb func(), flags ...Signal.Flags)

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

func (Instance) Poll

func (self Instance) Poll() error

Method used for polling the MultiplayerAPI. You only need to worry about this if you set SceneTree.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, Thread).

func (Instance) Rpc

func (self Instance) Rpc(peer int, obj Object.Instance, method string) error

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 Node.Rpc and Node.RpcConfig.

Note: Prefer using Node.Rpc, Node.RpcId, or my_method.rpc(peer, arg1, arg2, ...) (in GDScript), since they are faster. This method is mostly useful in conjunction with MultiplayerAPIExtension when extending or replacing the multiplayer capabilities.

func (Instance) SetMultiplayerPeer

func (self Instance) SetMultiplayerPeer(value MultiplayerPeer.Instance)

SetMultiplayerPeer sets the property returned by [GetMultiplayerPeer].

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

type MoreArgs

type MoreArgs [1]gdclass.MultiplayerAPI

MoreArgs is a container for Instance functions with additional 'optional' arguments.

func (MoreArgs) Rpc

func (self MoreArgs) Rpc(peer int, obj Object.Instance, method string, arguments []any) error

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 Node.Rpc and Node.RpcConfig.

Note: Prefer using Node.Rpc, Node.RpcId, or my_method.rpc(peer, arg1, arg2, ...) (in GDScript), since they are faster. This method is mostly useful in conjunction with MultiplayerAPIExtension when extending or replacing the multiplayer capabilities.

type RPCMode

type RPCMode int //gd:MultiplayerAPI.RPCMode
const (
	// Used with [Node.RpcConfig] to disable a method or property for all RPC calls, making it unavailable. Default for all methods.
	//
	// [Node.RpcConfig]: https://pkg.go.dev/graphics.gd/classdb/Node#Instance.RpcConfig
	RpcModeDisabled RPCMode = 0
	// Used with [Node.RpcConfig] to set a method to be callable remotely by any peer. Analogous to the @rpc("any_peer") annotation. Calls are accepted from all remote peers, no matter if they are node's authority or not.
	//
	// [Node.RpcConfig]: https://pkg.go.dev/graphics.gd/classdb/Node#Instance.RpcConfig
	RpcModeAnyPeer RPCMode = 1
	// Used with [Node.RpcConfig] to set a method to be callable remotely only by the current multiplayer authority (which is the server by default). Analogous to the @rpc("authority") annotation. See [Node.SetMultiplayerAuthority].
	//
	// [Node.RpcConfig]: https://pkg.go.dev/graphics.gd/classdb/Node#Instance.RpcConfig
	// [Node.SetMultiplayerAuthority]: https://pkg.go.dev/graphics.gd/classdb/Node#Instance.SetMultiplayerAuthority
	RpcModeAuthority RPCMode = 2
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL