Variant 类
关于
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.
Variant 可以:
Store almost any datatype.
针对若干 variant 执行操作(GDScript 使用 Variant 作为原子/原生数据类型)。
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).
用于推迟调用或在线程之间交换数据.
被序列化为二进制并存储到磁盘, 或通过网络传输.
被序列化为文本, 并将其用于打印值和可编辑的设置.
用作导出的属性, 因此编辑器可以对其进行普通地编辑.
用于字典, 数组, 解析器等.
基本上, 由于有了Variant类, 编写Godot本身就容易得多, 因为它可以轻松完成C++不常见的高度动态的结构. 今天就成为Variant的朋友吧.
备注
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.
参考
List of variant types
These types are available in Variant:
类型 |
注意 |
---|---|
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.