.. _doc_binary_serialization_api: Binary serialization API ======================== Introduction ------------ Godot has a simple serialization API based on Variant. It's used for converting data types to an array of bytes efficiently. This API is used in the functions `get_var` and `store_var` of `File` as well as the packet APIs for `PacketPeer`. This format is *not* used for binary scenes and resources. 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; +--------+--------------------------+ | Type | Value | +========+==========================+ | 0 | null | +--------+--------------------------+ | 1 | bool | +--------+--------------------------+ | 2 | integer | +--------+--------------------------+ | 3 | float | +--------+--------------------------+ | 4 | string | +--------+--------------------------+ | 5 | vector2 | +--------+--------------------------+ | 6 | rect2 | +--------+--------------------------+ | 7 | vector3 | +--------+--------------------------+ | 8 | transform2d | +--------+--------------------------+ | 9 | plane | +--------+--------------------------+ | 10 | quat | +--------+--------------------------+ | 11 | aabb | +--------+--------------------------+ | 12 | basis | +--------+--------------------------+ | 13 | transform | +--------+--------------------------+ | 14 | color | +--------+--------------------------+ | 15 | node path | +--------+--------------------------+ | 16 | rid | +--------+--------------------------+ | 17 | object | +--------+--------------------------+ | 18 | dictionary | +--------+--------------------------+ | 19 | array | +--------+--------------------------+ | 20 | raw array | +--------+--------------------------+ | 21 | int array | +--------+--------------------------+ | 22 | real array | +--------+--------------------------+ | 23 | string array | +--------+--------------------------+ | 24 | vector2 array | +--------+--------------------------+ | 25 | vector3 array | +--------+--------------------------+ | 26 | color array | +--------+--------------------------+ | 27 | max | +--------+--------------------------+ 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: `bool