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...
Object¶
Inherited By: AudioServer, CameraServer, ClassDB, DisplayServer, EditorFileSystemDirectory, EditorInterface, EditorPaths, EditorSelection, EditorUndoRedoManager, EditorVCSInterface, Engine, EngineDebugger, GDExtensionManager, Geometry2D, Geometry3D, GodotSharp, Input, InputMap, IP, JavaClassWrapper, JavaScriptBridge, JNISingleton, JSONRPC, MainLoop, Marshalls, MovieWriter, NavigationMeshGenerator, NavigationServer2D, NavigationServer3D, Node, OpenXRExtensionWrapperExtension, OpenXRInteractionProfileMetadata, OS, Performance, PhysicsDirectBodyState2D, PhysicsDirectBodyState3D, PhysicsDirectSpaceState2D, PhysicsDirectSpaceState3D, PhysicsServer2D, PhysicsServer2DManager, PhysicsServer3D, PhysicsServer3DManager, PhysicsServer3DRenderingServerHandler, ProjectSettings, RefCounted, RenderingDevice, RenderingServer, ResourceLoader, ResourceSaver, ResourceUID, ScriptLanguage, TextServerManager, ThemeDB, TileData, Time, TranslationServer, TreeItem, UndoRedo, WorkerThreadPool, XRServer
引擎中所有其他类的基类。
Description¶
一种高级的 Variant 类型。引擎中的所有类都继承自 Object。每个类都可以定义新的属性、方法或信号,并且这些对所有继承的类都可用。例如,一个 Sprite2D 实例能够调用 Node.add_child 因为它继承自 Node。
可以使用 GDScript 中的 Object.new()
或 C# 中的 new Object
来创建新实例。
要删除一个 Object 实例,请调用 free。这对于大多数继承 Object 的类来说是必须的,因为它们本身并不管理内存,如果不调用该方法的话,在不再使用时会造成内存泄漏。有几个类会执行内存管理。例如,RefCounted(以及扩展的 Resource)在不再被引用时删除自身,而 Node 在释放时会删除其子节点。
对象可以附加一个 Script。一旦该 Script 被实例化,它就有效地充当了基类的扩展,允许它定义和继承新的属性、方法和信号。
在 Script 中,_get_property_list 可以被可以重写,以通过多种方式自定义属性。这允许它们对编辑器可用,显示为选项列表,细分为组,保存在磁盘上,等等。脚本语言提供更简单的方式来自定义属性,例如使用 @GDScript.@export 注解。
Godot 是非常动态的。一个对象的脚本,以及它的属性、方法和信号,都可以在运行时改变。正因为如此,可能会出现这样的情况,例如,一个方法所需的属性可能不存在。为了防止运行时出错,可以参考 set、get、call、has_method、has_signal 等方法。请注意,这些方法比直接引用慢得多。
在 GDScript 中,还可以使用 in
运算符来检查对象中是否存在给定的属性、方法或信号名称:
var node = Node.new()
print("name" in node) # 输出 true
print("get_parent" in node) # 输出 true
print("tree_entered" in node) # 输出 true
print("unknown" in node) # 输出 false
通知是 int 常量,通常由对象发送和接收。例如,在每个渲染帧上,SceneTree 使用 Node.NOTIFICATION_PROCESS 通知树内的节点。节点收到它后,可以调用 Node._process 进行更新。要使用通知,请参阅 notification 和 _notification。
最后,每个对象还可以包含元数据(关于数据的数据)。set_meta 可用于存储对象本身不依赖的信息。为了保持代码整洁,不鼓励过度使用元数据。
注意:与对 RefCounted 的引用不同,对存储在变量中的对象的引用,可能会在未被设置为 null
的情况下变得无效。要检查对象是否已被删除,请不要将其与 null
进行比较。而是使用 @GlobalScope.is_instance_valid。存储数据的类,建议从 RefCounted 继承而不是 Object。
注意:script
不像大多数属性那样公开。要在代码中设置或获取一个对象的 Script,请分别使用 set_script 和 get_script。
Tutorials¶
Methods¶
_get ( StringName property ) virtual |
|
_get_property_list ( ) virtual |
|
void |
_init ( ) virtual |
void |
_notification ( int what ) virtual |
_property_can_revert ( StringName property ) virtual |
|
_property_get_revert ( StringName property ) virtual |
|