ENetPacketUtils

Inherits: Object

Utility singleton for encoding and decoding network packets in multiple formats.

Description

ENetPacketUtils provides utilities for encoding and decoding data in various formats for network transmission. It supports automatic type detection, Dictionary serialization, JSON, raw bytes, Variant encoding, and Node serialization with multiple modes.

This singleton is used internally by ENetServer and ENetClient but can also be used directly for custom packet handling.

Tutorials

Methods

Variant

decode_auto(data: PackedByteArray)

Dictionary

decode_dictionary(data: PackedByteArray)

String

decode_json(data: PackedByteArray)

Dictionary

decode_node_metadata(data: PackedByteArray)

String

decode_string(data: PackedByteArray)

Variant

decode_variant(data: PackedByteArray)

PackedByteArray

encode_auto(variant: Variant)

PackedByteArray

encode_dictionary(dictionary: Dictionary)

PackedByteArray

encode_json(json: String)

PackedByteArray

encode_node(node: Node, flags: int)

PackedByteArray

encode_string(string: String)

PackedByteArray

encode_variant(variant: Variant)


Enumerations

enum PacketType: 🔗

PacketType PACKET_TYPE_RAW = 0

Raw byte data without additional encoding.

PacketType PACKET_TYPE_VARIANT = 1

Variant data encoded using Godot's serialization.

PacketType PACKET_TYPE_DICTIONARY = 2

Dictionary data.

PacketType PACKET_TYPE_JSON = 3

JSON string data.

PacketType PACKET_TYPE_STRING = 4

UTF-8 string data.

PacketType PACKET_TYPE_NODE = 5

Serialized node data.


enum NodeSyncFlags: 🔗

NodeSyncFlags SYNC_PROPERTIES = 1

Serialize all exported properties of the node.

NodeSyncFlags SYNC_METADATA_ONLY = 2

Only serialize node metadata (path, type, name).

NodeSyncFlags SYNC_TRANSFORM_ONLY = 4

Only serialize transform data (for Node2D/Node3D).

NodeSyncFlags SYNC_CUSTOM = 8

Use custom serialization via the node's _serialize_for_network() method.


Method Descriptions

Variant decode_auto(data: PackedByteArray) 🔗

Automatically detects the packet type from the header and decodes it to the appropriate Variant type. This is the recommended method for most use cases.

var decoded_packet = ENetPacketUtils.decode_auto(raw_byte_array)

Dictionary decode_dictionary(data: PackedByteArray) 🔗

Decodes a PackedByteArray that was encoded with encode_dictionary() back into a Dictionary.


String decode_json(data: PackedByteArray) 🔗

Decodes a PackedByteArray that was encoded with encode_json() back into a JSON String.


Dictionary decode_node_metadata(data: PackedByteArray) 🔗

Decodes a PackedByteArray containing serialized node data back into a Dictionary with node metadata.


String decode_string(data: PackedByteArray) 🔗

Decodes a PackedByteArray that was encoded with encode_string() back into a String.


Variant decode_variant(data: PackedByteArray) 🔗

Decodes a PackedByteArray that was encoded with encode_variant() back into a Variant.


PackedByteArray encode_auto(variant: Variant) 🔗

Automatically detects the best encoding method for the given Variant and returns encoded data with a type header. This is the recommended method for most use cases.

var array_to_send = PackedByteArray()
array_to_send = ENetPacketUtils.encode_auto({"type": "chat", "msg": "hello"})

PackedByteArray encode_dictionary(dictionary: Dictionary) 🔗

Encodes a Dictionary into a PackedByteArray for network transmission.


PackedByteArray encode_json(json: String) 🔗

Encodes a JSON String into a PackedByteArray for network transmission.


PackedByteArray encode_node(node: Node, flags: int) 🔗

Encodes a Node into a PackedByteArray using the specified flags. See NodeSyncFlags for available options.


PackedByteArray encode_string(string: String) 🔗

Encodes a String into a PackedByteArray for network transmission.


PackedByteArray encode_variant(variant: Variant) 🔗

Encodes a Variant into a PackedByteArray for network transmission using Godot's built-in serialization.