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]
属性注解的泛型类型参数的 API 通常有此要求,因为值将被传递给引擎,如果该类型无法被编组,则会导致运行时错误。