Skip to main content

:article_outdated: True

Binary serialization API

Introduction

Godot has a serialization API based on Variant. It's used for converting data types to an array of bytes efficiently. This API is exposed via the global bytes_to_var() and var_to_bytes() functions, but it is also used in the get_var and store_var methods of class_FileAccess as well as the packet APIs for class_PacketPeer. This format is not used for binary scenes and resources.

Full Objects vs Object instance IDs

If a variable is serialized with full_objects = true, then any Objects contained in the variable will be serialized and included in the result. This is recursive.

If full_objects = false, then only the instance IDs will be serialized for any Objects contained in the variable.

Packet specification

The packet is designed to be always padded to 4 bytes. All values are little-endian-encoded. All packets have a 4-byte header representing an integer, specifying the type of data.

The lowest value two bytes are used to determine the type, while the highest value two bytes contain flags

base_type = val & 0xFFFF;
flags = val >> 16;

TypeValue
0null
1bool
2integer
3float
4string
5vector2
6rect2
7vector3
8transform2d
9plane
10quaternion
11aabb
12basis
13transform3d
14color
15node path
16rid
17object
18dictionary
19array
20raw array
21int32 array
22int64 array
23float32 array
24float64 array
25string array
26vector2 array
27vector3 array
28color array
29max

Following this is the actual packet contents, which varies for each type of packet. Note that this assumes Godot is compiled with single-precision floats, which is the default. If Godot was compiled with double-precision floats, the length of "Float" fields within data structures should be 8, and the offset should be (offset - 4) * 2 + 4. The "float" type itself always uses double precision.

0: null

1: :ref:bool<class_bool>

OffsetLenTypeDescription
44Integer0 for False, 1 for True

2: :ref:int<class_int>

If no flags are set (flags == 0), the integer is sent as a 32 bit integer:

OffsetLenTypeDescription
44Integer32-bit signed integer

If flag ENCODE_FLAG_64 is set (flags & 1 == 1), the integer is sent as a 64-bit integer:

OffsetLenTypeDescription
48Integer64-bit signed integer

3: :ref:float<class_float>

If no flags are set (flags == 0), the float is sent as a 32 bit single precision:

OffsetLenTypeDescription
44FloatIEEE 754 single-precision float

If flag ENCODE_FLAG_64 is set (flags & 1 == 1), the float is sent as a 64-bit double precision number:

OffsetLenTypeDescription
48FloatIEEE 754 double-precision float

4: :ref:String<class_string>

OffsetLenTypeDescription
44IntegerString length (in bytes)
8XBytesUTF-8 encoded string

This field is padded to 4 bytes.

5: :ref:Vector2<class_vector2>

OffsetLenTypeDescription
44FloatX coordinate
84FloatY coordinate

6: :ref:Rect2<class_rect2>

OffsetLenTypeDescription
44FloatX coordinate
84FloatY coordinate
124FloatX size
164FloatY size

7: :ref:Vector3<class_vector3>

OffsetLenTypeDescription
44FloatX coordinate
84FloatY coordinate
124FloatZ coordinate

8: :ref:Transform2D<class_transform2d>

OffsetLenTypeDescription
44FloatThe X component of the X column vector, accessed via [0][0]
84FloatThe Y component of the X column vector, accessed via [0][1]
124FloatThe X component of the Y column vector, accessed via [1][0]
164FloatThe Y component of the Y column vector, accessed via [1][1]
204FloatThe X component of the origin vector, accessed via [2][0]
244FloatThe Y component of the origin vector, accessed via [2][1]

9: :ref:Plane<class_plane>

OffsetLenTypeDescription
44FloatNormal X
84FloatNormal Y
124FloatNormal Z
164FloatDistance

10: :ref:Quaternion<class_quaternion>

OffsetLenTypeDescription
44FloatImaginary X
84FloatImaginary Y
124FloatImaginary Z
164FloatReal W

11: :ref:AABB<class_aabb>

OffsetLenTypeDescription
44FloatX coordinate
84FloatY coordinate
124FloatZ coordinate
164FloatX size
204FloatY size
244FloatZ size

12: :ref:Basis<class_basis>

OffsetLenTypeDescription
44FloatThe X component of the X column vector, accessed via [0][0]
84FloatThe Y component of the X column vector, accessed via [0][1]
124FloatThe Z component of the X column vector, accessed via [0][2]
164FloatThe X component of the Y column vector, accessed via [1][0]
204FloatThe Y component of the Y column vector, accessed via [1][1]
244FloatThe Z component of the Y column vector, accessed via [1][2]
284FloatThe X component of the Z column vector, accessed via [2][0]
324FloatThe Y component of the Z column vector, accessed via [2][1]
364FloatThe Z component of the Z column vector, accessed via [2][2]

13: :ref:Transform3D<class_transform3d>

OffsetLenTypeDescription
44FloatThe X component of the X column vector, accessed via [0][0]
84FloatThe Y component of the X column vector, accessed via [0][1]
124FloatThe Z component of the X column vector, accessed via [0][2]
164FloatThe X component of the Y column vector, accessed via [1][0]
204FloatThe Y component of the Y column vector, accessed via [1][1]
244FloatThe Z component of the Y column vector, accessed via [1][2]
284FloatThe X component of the Z column vector, accessed via [2][0]
324FloatThe Y component of the Z column vector, accessed via [2][1]
364FloatThe Z component of the Z column vector, accessed via [2][2]
404FloatThe X component of the origin vector, accessed via [3][0]
444FloatThe Y component of the origin vector, accessed via [3][1]
484FloatThe Z component of the origin vector, accessed via [3][2]

14: :ref:Color<class_color>

OffsetLenTypeDescription
44FloatRed (typically 0..1, can be above 1 for overbright colors)
84FloatGreen (typically 0..1, can be above 1 for overbright colors)
124FloatBlue (typically 0..1, can be above 1 for overbright colors)
164FloatAlpha (0..1)

15: :ref:NodePath<class_nodepath>

OffsetLenTypeDescription
44IntegerString length, or new format (val&0x80000000!=0 and NameCount=val&0x7FFFFFFF)

For old format:

OffsetLenTypeDescription
8XBytesUTF-8 encoded string

Padded to 4 bytes.

For new format:

OffsetLenTypeDescription
44IntegerSub-name count
84IntegerFlags (absolute: val&1 != 0 )

For each Name and Sub-Name

OffsetLenTypeDescription
X+04IntegerString length
X+4XBytesUTF-8 encoded string

Every name string is padded to 4 bytes.

16: :ref:RID<class_rid> (unsupported)

17: :ref:Object<class_object>

An Object could be serialized in three different ways: as a null value, with full_objects = false, or with full_objects = true.

A null value

OffsetLenTypeDescription
44IntegerZero (32-bit signed integer)

full_objects disabled

OffsetLenTypeDescription
48IntegerThe Object instance ID (64-bit signed integer)

full_objects enabled

OffsetLenTypeDescription
44IntegerClass name (String length)
8XBytesClass name (UTF-8 encoded string)
X+84IntegerThe number of properties that are serialized

For each property:

OffsetLenTypeDescription
Y4IntegerProperty name (String length)
Y+4ZBytesProperty name (UTF-8 encoded string)
Y+4+ZW<variable>Property value, using this same format
note

Not all properties are included. Only properties that are configured with the PROPERTY_USAGE_STORAGE flag set will be serialized. You can add a new usage flag to a property by overriding the _get_property_list method in your class. You can also check how property usage is configured by calling Object._get_property_list See PropertyUsageFlags for the possible usage flags.

18: :ref:Dictionary<class_dictionary>

OffsetLenTypeDescription
44Integerval&0x7FFFFFFF = elements, val&0x80000000 = shared (bool)

Then what follows is, for amount of "elements", pairs of key and value, one after the other, using this same format.

19: :ref:Array<class_array>

OffsetLenTypeDescription
44Integerval&0x7FFFFFFF = elements, val&0x80000000 = shared (bool)

Then what follows is, for amount of "elements", values one after the other, using this same format.

20: :ref:PackedByteArray<class_PackedByteArray>

OffsetLenTypeDescription
44IntegerArray length (Bytes)
8..8+length1ByteByte (0..255)

The array data is padded to 4 bytes.

21: :ref:PackedInt32Array<class_PackedInt32Array>

OffsetLenTypeDescription
44IntegerArray length (Integers)
8..8+length*44Integer32-bit signed integer

22: :ref:PackedInt64Array<class_PackedInt64Array>

OffsetLenTypeDescription
48IntegerArray length (Integers)
8..8+length*88Integer64-bit signed integer

23: :ref:PackedFloat32Array<class_PackedFloat32Array>

OffsetLenTypeDescription
44IntegerArray length (Floats)
8..8+length*44Integer32-bit IEEE 754 single-precision float

24: :ref:PackedFloat64Array<class_PackedFloat64Array>

OffsetLenTypeDescription
44IntegerArray length (Floats)
8..8+length*88Integer64-bit IEEE 754 double-precision float

25: :ref:PackedStringArray<class_PackedStringArray>

OffsetLenTypeDescription
44IntegerArray length (Strings)

For each String:

OffsetLenTypeDescription
X+04IntegerString length
X+4XBytesUTF-8 encoded string

Every string is padded to 4 bytes.

26: :ref:PackedVector2Array<class_PackedVector2Array>

OffsetLenTypeDescription
44IntegerArray length
8..8+length*84FloatX coordinate
8..12+length*84FloatY coordinate

27: :ref:PackedVector3Array<class_PackedVector3Array>

OffsetLenTypeDescription
44IntegerArray length
8..8+length*124FloatX coordinate
8..12+length*124FloatY coordinate
8..16+length*124FloatZ coordinate

28: :ref:PackedColorArray<class_PackedColorArray>

OffsetLenTypeDescription
44IntegerArray length
8..8+length*164FloatRed (typically 0..1, can be above 1 for overbright colors)
8..12+length*164FloatGreen (typically 0..1, can be above 1 for overbright colors)
8..16+length*164FloatBlue (typically 0..1, can be above 1 for overbright colors)
8..20+length*164FloatAlpha (0..1)