Clase variant
Acerca de
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.
Una variante puede:
Almacenar casi cualquier tipo de datos.
Realiza operaciones entre muchas variantes (GDScript utiliza Variant como su tipo de dato atómico/nativo).
Ser hasheado, de modo que pueda compararse rápidamente con otras variantes.
Es utilizado para convertir de forma segura entre tipos de datos.
Es utilizado para abstraer métodos de llamada y sus argumentos (Godot exporta todas sus funciones a través de variantes).
Se usa para aplazar llamadas o mover datos entre hilos.
Es serializado como binario y almacenado en disco, o transferido a través de la red.
Puede ser serializado a texto y utilizado para imprimir valores y configuraciones editables.
Funciona como una propiedad exportada, por lo que el editor puede editarla universalmente.
Puede ser utilizado para diccionarios, arreglos, analizadores, etc.
Básicamente, gracias a la clase Variant, escribir Godot en sí fue una tarea mucho, mucho más fácil, ya que permite construcciones altamente dinámicas que no son comunes en C++ con poco esfuerzo. Conviértete en amigo de Variant hoy mismo.
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.
Referencias
List of variant types
These types are available in Variant:
Tipo |
Notas |
|---|---|
Nil (can only store |
Nullable type |
2D counterpart of AABB |
|
3D counterpart of Rect2 |
|
Nullable type |
|
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.