Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
GD0301:泛型型別參數必須為 Variant 相容型別
規則 ID |
GD0301 |
分類 |
用法 |
修正是否會破壞相容性 |
破壞性變更 |
預設啟用 |
是 |
原因
指定的泛型型別參數不是預期的 Variant 相容型別。
規則說明
當泛型型別參數被標註為 [MustBeVariant] 屬性時,該型別必須是 Variant 相容型別。例如,泛型 Godot.Collections.Array<T> 只支援可轉換為 Variant 的型別項目。
class SomeType { }
// SomeType is not a valid type because it doesn't derive from GodotObject,
// so it's not compatible with Variant.
var invalidArray = new Godot.Collections.Array<SomeType>();
// System.Int32 is a valid type because it's compatible with Variant.
var validArray = new Godot.Collections.Array<int>();
修正方式
要修正此規則的違規情形,請將泛型型別參數改為 Variant 相容型別,或改用不要求 Variant 相容型別的 API。
什麼時候應該忽略警告
請勿忽略此規則的警告。標註 [MustBeVariant] 屬性的泛型參數通常會傳遞至引擎,若型別無法被正確處理,將導致執行階段錯誤。