Classe Variant

Sobre

Variant is the most important datatype in Godot. A Variant takes up only 24 bytes on 64-bit platforms (20 bytes on 32-bit platforms) and can store almost any engine datatype inside of it. Variants are rarely used to hold information for long periods of time, instead they are used mainly for communication, editing, serialization and generally moving data around.

Uma Variant pode:

  • Store almost any datatype.

  • Executar operações entre muitas Variant (o GDScript usa Variant como seu tipo de dado atômico/nativo).

  • Be hashed, so it can be compared quickly to other variants.

  • Be used to convert safely between datatypes.

  • Be used to abstract calling methods and their arguments (Godot exports all its functions through variants).

  • Ser usado para adiar chamadas ou mover dados entre threads.

  • Ser serializado como binário e armazenado em disco ou ser transferido pela rede.

  • Ser serializado em texto e usado para imprimir valores e configurações editáveis.

  • Trabalhar como uma propriedade exportada, para que o editor possa editá-la universalmente.

  • Ser usada para dicionários, matrizes, analisadores (parsers), etc.

Basicamente, graças à classe Variant, escrever a Godot foi uma tarefa muito, mas muito mais fácil, pois esta permite construções altamente dinâmicas não comuns em C++ com menos esforço. Torne-se um amigo da Variant hoje.

Nota

All types within Variant except Nil and Object cannot be null and must always store a valid value. These types within Variant are therefore called non-nullable types.

One of the Variant types is Nil which can only store the value null. Therefore, it is possible for a Variant to contain the value null, even though all Variant types excluding Nil and Object are non-nullable.

Referências

List of variant types

These types are available in Variant:

Tipo

Notas

Nil (can only store null)

Nullable type

bool

int

float

String

Vector2

Vector2i

Rect2

2D counterpart of AABB

Rect2i

Vector3

Vector3i

Transform2D

Vector4

Vector4i

Plane

Quaternion

AABB

3D counterpart of Rect2

Basis

Transform3D

Projection

Color

StringName

NodePath

RID

Object

Nullable type

Callable

Signal

Dictionary

Array

PackedByteArray

PackedInt32Array

PackedInt64Array

PackedFloat32Array

PackedFloat64Array

PackedStringArray

PackedVector2Array

PackedVector3Array

PackedColorArray

PackedVector4Array

Containers: Array and Dictionary

Both Array and Dictionary 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 or an Array inside, making it even more flexible.

Modifications to a container will modify all references to it. A Mutex should be created to lock it if multi-threaded access is desired.

Referências