Click or drag to resize

NetworkedMultiplayerENet Class

A PacketPeer implementation that should be passed to NetworkPeer after being initialized as either a client or server. Events can then be handled by connecting to SceneTree signals.

ENet's purpose is to provide a relatively thin, simple and robust network communication layer on top of UDP (User Datagram Protocol).

Note: ENet only uses UDP, not TCP. When forwarding the server port to make your server accessible on the public Internet, you only need to forward the server port in UDP. You can use the UPNP class to try to forward the server port automatically when starting the server.

Inheritance Hierarchy

Namespace:  Godot
Assembly:  GodotSharp (in GodotSharp.dll) Version: 1.0.0
Syntax
C#
public class NetworkedMultiplayerENet : NetworkedMultiplayerPeer

The NetworkedMultiplayerENet type exposes the following members.

Constructors
  NameDescription
Public methodNetworkedMultiplayerENet
Initializes a new instance of the NetworkedMultiplayerENet class
Top
Properties
  NameDescription
Public propertyAllowObjectDecoding

Deprecated. Use get_var and put_var parameters instead.

If true, the PacketPeer will allow encoding and decoding of object via GetVar(Boolean) and PutVar(Object, Boolean).

Warning: Deserialized objects can contain code which gets executed. Do not use this option if the serialized object comes from untrusted sources to avoid potential security threats such as remote code execution.

(Inherited from PacketPeer.)
Public propertyAlwaysOrdered

Enforce ordered packets when using (thus behaving similarly to ). This is the only way to use ordering with the RPC system.

Public propertyChannelCount

The number of channels to be used by ENet. Channels are used to separate different kinds of data. In reliable or ordered mode, for example, the packet delivery order is ensured on a per-channel basis. This is done to combat latency and reduces ordering restrictions on packets. The delivery status of a packet in one channel won't stall the delivery of other packets in another channel.

Public propertyCompressionMode

The compression method used for network packets. These have different tradeoffs of compression speed versus bandwidth, you may need to test which one works best for your use case if you use compression at all.

Public propertyDtlsVerify

Enable or disable certificate verification when UseDtlstrue.

Public propertyDynamicObject
Gets a new DynamicGodotObject associated with this instance.
(Inherited from Object.)
Public propertyEncodeBufferMaxSize

Maximum buffer size allowed when encoding Variants. Raise this value to support heavier memory allocations.

The PutVar(Object, Boolean) method allocates memory on the stack, and the buffer used will grow automatically to the closest power of two to match the size of the Variant. If the Variant is bigger than encode_buffer_max_size, the method will error out with .

(Inherited from PacketPeer.)
Public propertyNativeInstance (Inherited from Object.)
Public propertyRefuseNewConnections

If true, this NetworkedMultiplayerPeer refuses new connections.

(Inherited from NetworkedMultiplayerPeer.)
Public propertyServerRelay

Enable or disable the server feature that notifies clients of other peers' connection/disconnection, and relays messages between them. When this option is false, clients won't be automatically notified of other peers and won't be able to send them packets through the server.

Public propertyTransferChannel

Set the default channel to be used to transfer data. By default, this value is -1 which means that ENet will only use 2 channels: one for reliable packets, and one for unreliable packets. The channel 0 is reserved and cannot be used. Setting this member to any value between 0 and ChannelCount (excluded) will force ENet to use that channel for sending data. See ChannelCount for more information about ENet channels.

Public propertyTransferMode (Inherited from NetworkedMultiplayerPeer.)
Public propertyUseDtls

When enabled, the client or server created by this peer, will use PacketPeerDTLS instead of raw UDP sockets for communicating with the remote peer. This will make the communication encrypted with DTLS at the cost of higher resource usage and potentially larger packet size.

Note: When creating a DTLS server, make sure you setup the key/certificate pair via SetDtlsKey(CryptoKey) and SetDtlsCertificate(X509Certificate). For DTLS clients, have a look at the DtlsVerify option, and configure the certificate accordingly via SetDtlsCertificate(X509Certificate).

Top
Methods
  NameDescription
Public method_Get

Virtual method which can be overridden to customize the return value of Get(String).

Returns the given property. Returns null if the property does not exist.

(Inherited from Object.)
Public method_GetPropertyList

Virtual method which can be overridden to customize the return value of GetPropertyList.

Returns the object's property list as an Array of dictionaries.

Each property's Dictionary must contain at least name: String and type: int (see VariantType) entries. Optionally, it can also include hint: int (see PropertyHint), hint_string: String, and usage: int (see PropertyUsageFlags).

(Inherited from Object.)
Public method_Notification

Called whenever the object receives a notification, which is identified in what by a constant. The base Object has two constants and , but subclasses such as Node define a lot more notifications which are also received by this method.

(Inherited from Object.)
Public method_Set

Virtual method which can be overridden to customize the return value of Set(String, Object).

Sets a property. Returns true if the property exists.

(Inherited from Object.)
Public methodAddUserSignal

Adds a user-defined signal. Arguments are optional, but can be added as an Array of dictionaries, each containing name: String and type: int (see VariantType) entries.

(Inherited from Object.)
Public methodCall

Calls the method on the object and returns the result. This method supports a variable number of arguments, so parameters are passed as a comma separated list. Example:

call("set", "position", Vector2(42.0, 0.0))

Note: In C#, the method name must be specified as snake_case if it is defined by a built-in Godot node. This doesn't apply to user-defined methods where you should use the same convention as in the C# source (typically PascalCase).

(Inherited from Object.)
Public methodCallDeferred

Calls the method on the object during idle time. This method supports a variable number of arguments, so parameters are passed as a comma separated list. Example:

call_deferred("set", "position", Vector2(42.0, 0.0))

Note: In C#, the method name must be specified as snake_case if it is defined by a built-in Godot node. This doesn't apply to user-defined methods where you should use the same convention as in the C# source (typically PascalCase).

(Inherited from Object.)
Public methodCallv

Calls the method on the object and returns the result. Contrarily to Call(String, Object), this method does not support a variable number of arguments but expects all parameters to be via a single Array.

callv("set", [ "position", Vector2(42.0, 0.0) ])

(Inherited from Object.)
Public methodCanTranslateMessages

Returns true if the object can translate strings. See SetMessageTranslation(Boolean) and Tr(String).

(Inherited from Object.)
Public methodCloseConnection

Closes the connection. Ignored if no connection is currently established. If this is a server it tries to notify all clients before forcibly disconnecting them. If this is a client it simply closes the connection to the server.

Public methodConnect

Connects a signal to a method on a target object. Pass optional binds to the call as an Array of parameters. These parameters will be passed to the method after any parameter used in the call to EmitSignal(String, Object). Use flags to set deferred or one-shot connections. See ObjectConnectFlags constants.

A signal can only be connected once to a method. It will throw an error if already connected, unless the signal was connected with . To avoid this, first, use IsConnected(String, Object, String) to check for existing connections.

If the target is destroyed in the game's lifecycle, the connection will be lost.

Examples:

connect("pressed", self, "_on_Button_pressed") # BaseButton signal
connect("text_entered", self, "_on_LineEdit_text_entered") # LineEdit signal
connect("hit", self, "_on_Player_hit", [ weapon_type, damage ]) # User-defined signal

An example of the relationship between binds passed to Connect(String, Object, String, Array, UInt32) and parameters used when calling EmitSignal(String, Object):

connect("hit", self, "_on_Player_hit", [ weapon_type, damage ]) # weapon_type and damage are passed last
emit_signal("hit", "Dark lord", 5) # "Dark lord" and 5 are passed first
func _on_Player_hit(hit_by, level, weapon_type, damage):
    print("Hit by %s (lvl %d) with weapon %s for %d damage" % [hit_by, level, weapon_type, damage])

(Inherited from Object.)
Public methodCreateClient

Create client that connects to a server at address using specified port. The given address needs to be either a fully qualified domain name (e.g. "www.example.com") or an IP address in IPv4 or IPv6 format (e.g. "192.168.1.1"). The port is the port the server is listening on. The in_bandwidth and out_bandwidth parameters can be used to limit the incoming and outgoing bandwidth to the given number of bytes per second. The default of 0 means unlimited bandwidth. Note that ENet will strategically drop packets on specific sides of a connection between peers to ensure the peer's bandwidth is not overwhelmed. The bandwidth parameters also determine the window size of a connection which limits the amount of reliable packets that may be in transit at any given time. Returns if a client was created, if this NetworkedMultiplayerENet instance already has an open connection (in which case you need to call CloseConnection(UInt32) first) or if the client could not be created. If client_port is specified, the client will also listen to the given port; this is useful for some NAT traversal techniques.

Public methodCreateServer

Create server that listens to connections via port. The port needs to be an available, unused port between 0 and 65535. Note that ports below 1024 are privileged and may require elevated permissions depending on the platform. To change the interface the server listens on, use SetBindIp(String). The default IP is the wildcard "*", which listens on all available interfaces. max_clients is the maximum number of clients that are allowed at once, any number up to 4095 may be used, although the achievable number of simultaneous clients may be far lower and depends on the application. For additional details on the bandwidth parameters, see CreateClient(String, Int32, Int32, Int32, Int32). Returns if a server was created, if this NetworkedMultiplayerENet instance already has an open connection (in which case you need to call CloseConnection(UInt32) first) or if the server could not be created.

Public methodDisconnect

Disconnects a signal from a method on the given target.

If you try to disconnect a connection that does not exist, the method will throw an error. Use IsConnected(String, Object, String) to ensure that the connection exists.

(Inherited from Object.)
Public methodDisconnectPeer

Disconnect the given peer. If "now" is set to true, the connection will be closed immediately without flushing queued messages.

Public methodDispose (Inherited from Object.)
Protected methodDispose(Boolean) (Inherited from Object.)
Public methodEmitSignal

Emits the given signal. The signal must exist, so it should be a built-in signal of this class or one of its parent classes, or a user-defined signal. This method supports a variable number of arguments, so parameters are passed as a comma separated list. Example:

emit_signal("hit", weapon_type, damage)
emit_signal("game_over")

(Inherited from Object.)
Public methodEquals (Inherited from Object.)
Protected methodFinalize (Inherited from Object.)
Public methodFree

Deletes the object from memory immediately. For Nodes, you may want to use QueueFree to queue the node for safe deletion at the end of the current frame.

Important: If you have a variable pointing to an object, it will not be assigned to null once the object is freed. Instead, it will point to a previously freed instance and you should validate it with @GDScript.is_instance_valid before attempting to call its methods or access its properties.

(Inherited from Object.)
Public methodGet

Returns the Variant value of the given property. If the property doesn't exist, this will return null.

Note: In C#, the property name must be specified as snake_case if it is defined by a built-in Godot node. This doesn't apply to user-defined properties where you should use the same convention as in the C# source (typically PascalCase).

(Inherited from Object.)
Public methodGetAvailablePacketCount

Returns the number of packets currently available in the ring-buffer.

(Inherited from PacketPeer.)
Public methodGetChannelCount Obsolete.
Public methodGetClass

Returns the object's class as a String.

(Inherited from Object.)
Public methodGetCompressionMode Obsolete.
Public methodGetConnectionStatus (Inherited from NetworkedMultiplayerPeer.)
Public methodGetEncodeBufferMaxSize Obsolete. (Inherited from PacketPeer.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetIncomingConnections

Returns an Array of dictionaries with information about signals that are connected to the object.

Each Dictionary contains three String entries:

- source is a reference to the signal emitter.

- signal_name is the name of the connected signal.

- method_name is the name of the method to which the signal is connected.

(Inherited from Object.)
Public methodGetIndexed

Gets the object's property indexed by the given NodePath. The node path should be relative to the current object and can use the colon character (:) to access nested properties. Examples: "position:x" or "material:next_pass:blend_mode".

(Inherited from Object.)
Public methodGetInstanceId

Returns the object's unique instance ID.

This ID can be saved in EncodedObjectAsID, and can be used to retrieve the object instance with @GDScript.instance_from_id.

(Inherited from Object.)
Public methodGetLastPacketChannel
Public methodGetMeta

Returns the object's metadata entry for the given name.

(Inherited from Object.)
Public methodGetMetaList

Returns the object's metadata as a String.

(Inherited from Object.)
Public methodGetMethodList

Returns the object's methods and their signatures as an Array.

(Inherited from Object.)
Public methodGetPacket

Gets a raw packet.

(Inherited from PacketPeer.)
Public methodGetPacketChannel

Returns the channel of the next packet that will be retrieved via GetPacket.

Public methodGetPacketError (Inherited from PacketPeer.)
Public methodGetPacketPeer

Returns the ID of the NetworkedMultiplayerPeer who sent the most recent packet.

(Inherited from NetworkedMultiplayerPeer.)
Public methodGetPeerAddress

Returns the IP address of the given peer.

Public methodGetPeerPort

Returns the remote port of the given peer.

Public methodGetPropertyList

Returns the object's property list as an Array of dictionaries.

Each property's Dictionary contain at least name: String and type: int (see VariantType) entries. Optionally, it can also include hint: int (see PropertyHint), hint_string: String, and usage: int (see PropertyUsageFlags).

(Inherited from Object.)
Public methodGetScript

Returns the object's Script instance, or null if none is assigned.

(Inherited from Object.)
Public methodGetSignalConnectionList

Returns an Array of connections for the given signal.

(Inherited from Object.)
Public methodGetSignalList

Returns the list of signals as an Array of dictionaries.

(Inherited from Object.)
Public methodGetTransferChannel Obsolete.
Public methodGetTransferMode Obsolete. (Inherited from NetworkedMultiplayerPeer.)
Public methodGetType (Inherited from Object.)
Public methodGetUniqueId

Returns the ID of this NetworkedMultiplayerPeer.

(Inherited from NetworkedMultiplayerPeer.)
Public methodGetVar

Gets a Variant. If allow_objects (or AllowObjectDecoding) is true, decoding objects is allowed.

Warning: Deserialized objects can contain code which gets executed. Do not use this option if the serialized object comes from untrusted sources to avoid potential security threats such as remote code execution.

(Inherited from PacketPeer.)
Public methodHasMeta

Returns true if a metadata entry is found with the given name.

(Inherited from Object.)
Public methodHasMethod

Returns true if the object contains the given method.

(Inherited from Object.)
Public methodHasSignal

Returns true if the given signal exists.

(Inherited from Object.)
Public methodHasUserSignal

Returns true if the given user-defined signal exists. Only signals added using AddUserSignal(String, Array) are taken into account.

(Inherited from Object.)
Public methodInitRef

Initializes the internal reference counter. Use this only if you really know what you are doing.

Returns whether the initialization was successful.

(Inherited from Reference.)
Public methodIsAlwaysOrdered Obsolete.
Public methodIsBlockingSignals

Returns true if signal emission blocking is enabled.

(Inherited from Object.)
Public methodIsClass

Returns true if the object inherits from the given class.

(Inherited from Object.)
Public methodIsConnected

Returns true if a connection exists for a given signal, target, and method.

(Inherited from Object.)
Public methodIsDtlsEnabled Obsolete.
Public methodIsDtlsVerifyEnabled Obsolete.
Public methodIsObjectDecodingAllowed Obsolete. (Inherited from PacketPeer.)
Public methodIsQueuedForDeletion (Inherited from Object.)
Public methodIsRefusingNewConnections Obsolete. (Inherited from NetworkedMultiplayerPeer.)
Public methodIsServerRelayEnabled Obsolete.
Protected methodMemberwiseClone (Inherited from Object.)
Public methodNotification

Send a given notification to the object, which will also trigger a call to the _Notification(Int32) method of all classes that the object inherits from.

If reversed is true, _Notification(Int32) is called first on the object's own class, and then up to its successive parent classes. If reversed is false, _Notification(Int32) is called first on the highest ancestor (Object itself), and then down to its successive inheriting classes.

(Inherited from Object.)
Public methodPoll

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

(Inherited from NetworkedMultiplayerPeer.)
Public methodPropertyListChangedNotify

Notify the editor that the property list has changed, so that editor plugins can take the new values into account. Does nothing on export builds.

(Inherited from Object.)
Public methodPutPacket

Sends a raw packet.

(Inherited from PacketPeer.)
Public methodPutVar

Sends a Variant as a packet. If full_objects (or AllowObjectDecoding) is true, encoding objects is allowed (and can potentially include code).

(Inherited from PacketPeer.)
Public methodReference_

Increments the internal reference counter. Use this only if you really know what you are doing.

Returns true if the increment was successful, false otherwise.

(Inherited from Reference.)
Public methodRemoveMeta

Removes a given entry from the object's metadata. See also SetMeta(String, Object).

(Inherited from Object.)
Public methodSet

Assigns a new value to the given property. If the property does not exist, nothing will happen.

Note: In C#, the property name must be specified as snake_case if it is defined by a built-in Godot node. This doesn't apply to user-defined properties where you should use the same convention as in the C# source (typically PascalCase).

(Inherited from Object.)
Public methodSetAllowObjectDecoding Obsolete. (Inherited from PacketPeer.)
Public methodSetAlwaysOrdered Obsolete.
Public methodSetBindIp

The IP used when creating a server. This is set to the wildcard "*" by default, which binds to all available interfaces. The given IP needs to be in IPv4 or IPv6 address format, for example: "192.168.1.1".

Public methodSetBlockSignals

If set to true, signal emission is blocked.

(Inherited from Object.)
Public methodSetChannelCount Obsolete.
Public methodSetCompressionMode Obsolete.
Public methodSetDeferred

Assigns a new value to the given property, after the current frame's physics step. This is equivalent to calling Set(String, Object) via CallDeferred(String, Object), i.e. call_deferred("set", property, value).

Note: In C#, the property name must be specified as snake_case if it is defined by a built-in Godot node. This doesn't apply to user-defined properties where you should use the same convention as in the C# source (typically PascalCase).

(Inherited from Object.)
Public methodSetDtlsCertificate

Configure the X509Certificate to use when UseDtls is true. For servers, you must also setup the CryptoKey via SetDtlsKey(CryptoKey).

Public methodSetDtlsEnabled Obsolete.
Public methodSetDtlsKey

Configure the CryptoKey to use when UseDtls is true. Remember to also call SetDtlsCertificate(X509Certificate) to setup your X509Certificate.

Public methodSetDtlsVerifyEnabled Obsolete.
Public methodSetEncodeBufferMaxSize Obsolete. (Inherited from PacketPeer.)
Public methodSetIndexed

Assigns a new value to the property identified by the NodePath. The node path should be relative to the current object and can use the colon character (:) to access nested properties. Example:

set_indexed("position", Vector2(42, 0))
set_indexed("position:y", -10)
print(position) # (42, -10)

(Inherited from Object.)
Public methodSetMessageTranslation

Defines whether the object can translate strings (with calls to Tr(String)). Enabled by default.

(Inherited from Object.)
Public methodSetMeta

Adds, changes or removes a given entry in the object's metadata. Metadata are serialized and can take any Variant value.

To remove a given entry from the object's metadata, use RemoveMeta(String). Metadata is also removed if its value is set to null. This means you can also use set_meta("name", null) to remove metadata for "name".

(Inherited from Object.)
Public methodSetPeerTimeout

Sets the timeout parameters for a peer.The timeout parameters control how and when a peer will timeout from a failure to acknowledge reliable traffic. Timeout values are expressed in milliseconds.

The timeout_limit is a factor that, multiplied by a value based on the avarage round trip time, will determine the timeout limit for a reliable packet. When that limit is reached, the timeout will be doubled, and the peer will be disconnected if that limit has reached timeout_min. The timeout_max parameter, on the other hand, defines a fixed timeout for which any packet must be acknowledged or the peer will be dropped.

Public methodSetRefuseNewConnections Obsolete. (Inherited from NetworkedMultiplayerPeer.)
Public methodSetScript

Assigns a script to the object. Each object can have a single script assigned to it, which are used to extend its functionality.

If the object already had a script, the previous script instance will be freed and its variables and state will be lost. The new script's method will be called.

(Inherited from Object.)
Public methodSetServerRelayEnabled Obsolete.
Public methodSetTargetPeer

Sets the peer to which packets will be sent.

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

(Inherited from NetworkedMultiplayerPeer.)
Public methodSetTransferChannel Obsolete.
Public methodSetTransferMode Obsolete. (Inherited from NetworkedMultiplayerPeer.)
Public methodCode exampleToSignal
Returns a new SignalAwaiter awaiter configured to complete when the instance source emits the signal specified by the signal parameter.
(Inherited from Object.)
Public methodToString (Inherited from Object.)
Public methodTr

Translates a message using translation catalogs configured in the Project Settings.

Only works if message translation is enabled (which it is by default), otherwise it returns the message unchanged. See SetMessageTranslation(Boolean).

(Inherited from Object.)
Public methodUnreference

Decrements the internal reference counter. Use this only if you really know what you are doing.

Returns true if the decrement was successful, false otherwise.

(Inherited from Reference.)
Top
See Also