Docs: Variant — Godot Engine (stable) documentation in English

A Variant:

  • Can store almost any datatype.
  • Can perform operations between many variants. GDScript uses Variant as its atomic/native datatype.
  • Can be hashed, so it can be compared quickly to other variants.
  • Can be used to convert safely between datatypes.
  • Can be used to abstract calling methods and their arguments. Godot exports all its functions through variants.
  • Can be used to defer calls or move data between threads.
  • Can be serialized as binary and stored to disk, or transferred via network.
  • Can be serialized to text and use it for printing values and editable settings.
  • Can work as an exported property, so the editor can edit it universally.
  • Can be used for dictionaries, arrays, parsers, etc.

Containers (Array and Dictionary): Both are implemented using variants. A Dictionary can match any datatype used as key to any other datatype. An Array just holds an array of Variants. Of course, a Variant can also hold a Dictionary and an Array inside, making it even more flexible.

Deep Dives

Size Considerations

According to the docs, a Variant is “only 20 bytes”. Whereas an int is 8 bytes (64 bits). So if you don’t statically type your int, then it will take up nearly twice as much space in memory.

var num1 = 0 # variant, 20 bytes
var num2: int = 0 # int, 8 bytes