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.

GD0401:该类必须继承自 Godot.GodotObject 或其派生类

规则 ID

GD0401

类别

用法

修复是破坏性的还是非破坏性的

更改继承链会导致中断性变更

非中断性变更——如果移除 [GlobalClass] 属性

默认启用

原因

一个被加上了 [GlobalClass] 属性的类,并没有继承 GodotObject

规则说明

[GlobalClass] 对于那些没有继承自 GodotObject 的类型是没有任何效果的。每一个 global class 最终都必须继承自 GodotObject ,这样它才能被正确地进行编组(marshalled,即在 C# 和 Godot 引擎之间进行数据传递和转换)。

// This type is not registered as a global class because it doesn't derive from GodotObject.
[GlobalClass]
class SomeType { }

// This type is a global class because it derives from Godot.Node
// which ultimately derives from GodotObject.
[GlobalClass]
class MyNode : Node { }

// This type is a global class because it derives from Godot.Resource
// which ultimately derives from GodotObject.
[GlobalClass]
class MyResource : Resource { }

如何解决违规情况

欲解决该规则带来的冲突,请避免在并未继承自 GodotObject 或移除 [GlobalClass] 属性。

何时抑制警告

不要抑制此规则的警告。在不继承 GodotObject 类型中添加 [GlobalClass] 是一个常见错误,而此警告有助于提醒用户,这可能会导致意外错误。