Up to date
This page is up to date for Godot 4.3.
If you still find outdated information, please open an issue.
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 本身便容易得多,因為使用 Variant 就能輕鬆地完成 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.