Object

派生: AudioServer, CameraServer, ClassDB, DisplayServer, EditorFileSystemDirectory, EditorInterface, EditorPaths, EditorSelection, EditorUndoRedoManager, EditorVCSInterface, Engine, EngineDebugger, FramebufferCacheRD, GDExtensionManager, Geometry2D, Geometry3D, GodotInstance, Input, InputMap, IP, JavaClassWrapper, JavaScriptBridge, JNISingleton, JSONRPC, MainLoop, Marshalls, MovieWriter, NativeMenu, NavigationMeshGenerator, NavigationServer2D, NavigationServer2DManager, NavigationServer3D, NavigationServer3DManager, Node, OpenXRExtensionWrapper, OpenXRInteractionProfileMetadata, OS, Performance, PhysicsDirectBodyState2D, PhysicsDirectBodyState3D, PhysicsDirectSpaceState2D, PhysicsDirectSpaceState3D, PhysicsServer2D, PhysicsServer2DManager, PhysicsServer3D, PhysicsServer3DManager, PhysicsServer3DRenderingServerHandler, ProjectSettings, RefCounted, RenderData, RenderingDevice, RenderingServer, RenderSceneData, ResourceLoader, ResourceSaver, ResourceUID, ScriptLanguage, ShaderIncludeDB, TextServerManager, ThemeDB, TileData, Time, TranslationServer, TreeItem, UndoRedo, UniformSetCacheRD, WorkerThreadPool, XRServer, XRVRS

引擎中所有其他类的基类。

描述

一种高级的 Variant 类型。引擎中的所有类都继承自 Object。每个类都可以定义新的属性、方法或信号,并且这些对所有继承的类都可用。例如,一个 Sprite2D 实例能够调用 Node.add_child() 因为它继承自 Node

可以使用 GDScript 中的 Object.new() 或 C# 中的 new GodotObject 来创建新实例。

要删除一个 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()

注意:在布尔值上下文中,Object 等于 null 或已释放时会求值为 false。否则 Object 始终求值为 true。 另见 @GlobalScope.is_instance_valid()

教程

方法

Variant

_get(property: StringName) virtual

Array[Dictionary]

_get_property_list() virtual

void

_init() virtual

Variant

_iter_get(iter: Variant) virtual

bool

_iter_init(iter: Array) virtual

bool

_iter_next(iter: Array) virtual

void

_notification(what: int) virtual

bool

_property_can_revert(property: StringName) virtual

Variant

_property_get_revert(property: StringName) virtual

bool

_set(property: StringName, value: Variant) virtual

String

_to_string() virtual

void

_validate_property(property: Dictionary) virtual

void

add_user_signal(signal: String, arguments: Array = [])

Variant

call(method: StringName, ...) vararg

Variant

call_deferred(method: StringName, ...) vararg

Variant

callv(method: StringName, arg_array: Array)

bool

can_translate_messages() const

void

cancel_free()

Error

connect(signal: StringName, callable: Callable, flags: int = 0)

void

disconnect(signal: StringName, callable: Callable)

Error

emit_signal(signal: StringName, ...) vararg

void

free()

Variant

get(property: StringName) const

String

get_class() const

Array[Dictionary]

get_incoming_connections() const

Variant

get_indexed(property_path: NodePath) const

int

get_instance_id() const

Variant

get_meta(name: StringName, default: Variant = null) const

Array[StringName]

get_meta_list() const

int

get_method_argument_count(method: StringName) const

Array[Dictionary]

get_method_list() const

Array[Dictionary]

get_property_list() const

Variant

get_script() const

Array[Dictionary]

get_signal_connection_list(signal: StringName) const

Array[Dictionary]

get_signal_list() const

StringName

get_translation_domain() const

bool

has_connections(signal: StringName) const

bool

has_meta(name: StringName) const

bool

has_method(method: StringName) const

bool

has_signal(signal: StringName) const

bool

has_user_signal(signal: StringName) const

bool

is_blocking_signals() const

bool

is_class(class: String) const

bool

is_connected(signal: StringName, callable: Callable) const

bool

is_queued_for_deletion() const

void

notification(what: int, reversed: bool = false)

void

notify_property_list_changed()

bool

property_can_revert(property: StringName) const

Variant

property_get_revert(property: StringName) const

void

remove_meta(name: StringName)

void

remove_user_signal(signal: StringName)

void

set(property: StringName, value: Variant)

void

set_block_signals(enable: bool)

void

set_deferred(property: StringName, value: Variant)

void

set_indexed(property_path: NodePath, value: Variant)

void

set_message_translation(enable: bool)

void

set_meta(name: StringName, value: Variant)

void

set_script(script: Variant)

void

set_translation_domain(domain: StringName)

String

to_string()

String

tr(message: StringName, context: StringName = &"") const

String

tr_n(message: StringName, plural_message: StringName, n: int, context: StringName = &"") const


信号

property_list_changed() 🔗

调用 notify_property_list_changed() 时发出。


script_changed() 🔗

该对象的脚本发生改变时发出。

注意:发出这个信号时,新脚本还没有初始化。如果你需要访问新脚本,请用 CONNECT_DEFERRED 推迟与这个信号的连接。


枚举

enum ConnectFlags: 🔗

ConnectFlags CONNECT_DEFERRED = 1

延迟连接会在空闲时触发 Callable(当前帧的末尾),不会立即触发。

ConnectFlags CONNECT_PERSIST = 2

Persisting connections are stored when the object is serialized (such as when using PackedScene.pack()). In the editor, connections created through the Signals dock are always persisting.

Note: Connections to lambda functions (that is, when the function code is embedded in the connect() call) cannot be made persistent.

ConnectFlags CONNECT_ONE_SHOT = 4

一次性连接,会在触发后自行断开。

ConnectFlags CONNECT_REFERENCE_COUNTED = 8

引用计数连接可以多次分配给同一个 Callable。每断开一次连接会让内部计数器减一。信号会在计数器变为 0 时完全断开连接。

ConnectFlags CONNECT_APPEND_SOURCE_OBJECT = 16

On signal emission, the source object is automatically appended after the original arguments of the signal, regardless of the connected Callable's unbinds which affect only the original arguments of the signal (see Callable.unbind(), Callable.get_unbound_arguments_count()).

extends Object

signal test_signal

func test():
    print(self) # Prints e.g. <Object#35332818393>
    test_signal.connect(prints.unbind(1), CONNECT_APPEND_SOURCE_OBJECT)
    test_signal.emit("emit_arg_1", "emit_arg_2") # Prints emit_arg_1 <Object#35332818393>

常量

NOTIFICATION_POSTINITIALIZE = 0 🔗

该对象初始化时收到的通知,发生在附加脚本之前。内部使用。

NOTIFICATION_PREDELETE = 1 🔗

该对象即将被删除时收到的通知。可以用作面向对象编程语言中的析构函数。

该通知会以反向顺序发送。

NOTIFICATION_EXTENSION_RELOADED = 2 🔗

当对象完成热重加载时收到的通知。该通知仅针对扩展类和派生类发送。


方法说明

Variant _get(property: StringName) virtual 🔗

覆盖该方法以自定义 get() 的行为。应该返回给定的 property 的值,或者 property 应该被正常处理时返回 null

结合 _set()_get_property_list(),该方法允许定义自定义属性,这对编辑器插件特别有用。

注意:获取对象的内置属性时不会调用该方法,包括使用 @GDScript.@export 定义的属性。

func _get(property):
    if property == "fake_property":
        print("正在获取我的属性!")
        return 4

func _get_property_list():
    return [
        { "name": "fake_property", "type": TYPE_INT }
    ]

注意: 和其他虚拟方法不同,每一个被脚本覆盖了的该方法都会被自动调用。这意味着基础实现不应该用 GDScript 中的 super 或者其他语言中的同等构造来调用。最底层子类的该方法会首先被调用,接着是沿类层次结构向上依次调用。调用链会在第一个返回了非 null 值的类停止。


Array[Dictionary] _get_property_list() virtual 🔗

Override this method to provide a custom list of additional properties to handle by the engine.

Should return a property list, as an Array of dictionaries. The result is added to the array of get_property_list(), and should be formatted in the same way. Each Dictionary must at least contain the name and type entries.

You can use _property_can_revert() and _property_get_revert() to customize the default values of the properties added by this method.

The example below displays a list of numbers shown as words going from ZERO to FIVE, with number_count controlling the size of the list:

@tool
extends Node

@export var number_count = 3:
    set(nc):
        number_count = nc
        numbers.resize(number_count)
        notify_property_list_changed()

var numbers = PackedInt32Array([0, 0, 0])

func _get_property_list():
    var properties = []

    for i in range(number_count):
        properties.append({
            "name": "number_%d" % i,
            "type": TYPE_INT,
            "hint": PROPERTY_HINT_ENUM,
            "hint_string": "ZERO,ONE,TWO,THREE,FOUR,FIVE",
        })

    return properties

func _get(property):
    if property.begins_with("number_"):
        var index = property.get_slice("_", 1).to_int()
        return numbers[index]

func _set(property, value):
    if property.begins_with("number_"):
        var index = property.get_slice("_", 1).to_int()
        numbers[index] = value
        return true
    return false

Note: This method is intended for advanced purposes. For most common use cases, the scripting languages offer easier ways to handle properties. See @GDScript.@export, @GDScript.@export_enum, @GDScript.@export_group, etc. If you want to customize exported properties, use _validate_property().

Note: If the object's script is not @GDScript.@tool, this method will not be called in the editor.

Note: Unlike other virtual methods, this method is called automatically for every script that overrides it. This means that the base implementation should not be called via super in GDScript or its equivalents in other languages. The bottom-most sub-class will be called first, with subsequent calls ascending the class hierarchy.


void _init() virtual 🔗

实例化对象的脚本时调用,通常是在对象在内存中初始化之后(通过 GDScript 中的 Object.new() 或 C# 中的 new GodotObject)。也可以将其定义为接受参数的形式。该方法类似于大多数编程语言中的构造函数。

注意:如果为 _init() 定义了必填的参数,则带脚本的 Object 只能直接创建。使用任何其他方式(例如 PackedScene.instantiate()Node.duplicate())创建时,该脚本的初始化都将失败。


Variant _iter_get(iter: Variant) virtual 🔗

返回当前的可迭代值。iter 存储迭代状态,但与 _iter_init()_iter_next() 不同,此处的状态只读,因此不使用 Array 封装。

贴士:在 GDScript 中,你可以使用 Variant 子类型作为 _iter_get() 的返回类型。该类型会用来设置 for 循环中迭代器变量的类型,提升类型安全。


bool _iter_init(iter: Array) virtual 🔗

初始化迭代器。iter 存储迭代状态。由于 GDScript 不支持按引用传递参数,这里使用单个元素的数组作为包装器。只要迭代器尚未到达末尾就会返回 true

class MyRange:
    var _from
    var _to

    func _init(from, to):
        assert(from <= to)
        _from = from
        _to = to

    func _iter_init(iter):
        iter[0] = _from
        return iter[0] < _to

    func _iter_next(iter):
        iter[0] += 1
        return iter[0] < _to

    func _iter_get(iter):
        return iter

func _ready():
    var my_range = MyRange.new(2, 5)
    for x in my_range:
        print(x) # 输出 2, 3, 4.

注意:你也可以忽略 iter,直接使用对象的状态,示例见在线文档。请注意,这种情况下无法在嵌套循环中重用相同的迭代器实例。此外,如果想要多次重用相同的实例,请确保在该方法中重置迭代器状态。


bool _iter_next(iter: Array) virtual 🔗

将迭代器移动到下一次迭代。iter 存储迭代状态。由于 GDScript 不支持按引用传递参数,这里使用单元素数组作为包装器。只要迭代器尚未到达末尾就会返回 true


void _notification(what: int) virtual 🔗

Called when the object receives a notification, which can be identified in what by comparing it with a constant. See also notification().

func _notification(what):
    if what == NOTIFICATION_PREDELETE:
        print("Goodbye!")

Note: The base Object defines a few notifications (NOTIFICATION_POSTINITIALIZE and NOTIFICATION_PREDELETE). Inheriting classes such as Node define a lot more notifications, which are also received by this method.

Note: Unlike other virtual methods, this method is called automatically for every script that overrides it. This means that the base implementation should not be called via super in GDScript or its equivalents in other languages. Call order depends on the reversed argument of notification() and varies between different notifications. Most notifications are sent in the forward order (i.e. Object class first, most derived class last).


bool _property_can_revert(property: StringName) virtual 🔗

Override this method to customize the given property's revert behavior. Should return true if the property has a custom default value and is revertible in the Inspector dock. Use _property_get_revert() to specify the property's default value.

Note: This method must return consistently, regardless of the current value of the property.

Note: Unlike other virtual methods, this method is called automatically for every script that overrides it. This means that the base implementation should not be called via super in GDScript or its equivalents in other languages. The bottom-most sub-class will be called first, with subsequent calls ascending the class hierarchy. The call chain will stop on the first class that returns true.


Variant _property_get_revert(property: StringName) virtual 🔗

Override this method to customize the given property's revert behavior. Should return the default value for the property. If the default value differs from the property's current value, a revert icon is displayed in the Inspector dock.

Note: _property_can_revert() must also be overridden for this method to be called.

Note: Unlike other virtual methods, this method is called automatically for every script that overrides it. This means that the base implementation should not be called via super in GDScript or its equivalents in other languages. The bottom-most sub-class will be called first, with subsequent calls ascending the class hierarchy. The call chain will stop on the first class that returns a non-null value.


bool _set(property: StringName, value: Variant) virtual 🔗

覆盖该方法以自定义 set() 的行为。应将 property 设置为 value 并返回 true,如果 property 正常处理则返回 false。设置 property确切方式取决于该方法的实现。

结合 _get()_get_property_list(),该方法允许定义自定义属性,这对编辑器插件特别有用。

注意:设置对象的内置属性时不会调用该方法,包括使用 @GDScript.@export 定义的属性。

var internal_data = {}

func _set(property, value):
    if property == "fake_property":
        # 在冒牌属性中存值。
        internal_data["fake_property"] = value
        return true
    return false

func _get_property_list():
    return [
        { "name": "fake_property", "type": TYPE_INT }
    ]

注意: 和其他虚拟方法不同,每一个被脚本覆盖了的该方法都会被自动调用。这意味着基础实现不应该用 GDScript 中的 super 或者其他语言中的同等构造来调用。最底层子类的该方法会首先被调用,接着是沿类层次结构向上依次调用。调用链会在第一个返回了 true 的类停止。


String _to_string() virtual 🔗

覆盖该方法以自定义 to_string() 的返回值,将对象表示为 String

func _to_string():
    return "欢迎来到 Godot 4!"

func _init():
    print(self)       # 输出“欢迎来到 Godot 4!”
    var a = str(self) # a 是“欢迎来到 Godot 4!”

void _validate_property(property: Dictionary) virtual 🔗

覆盖该方法以自定义已有属性。除了使用 _get_property_list() 添加的属性之外,每个属性信息都经过该方法。字典内容与 _get_property_list() 中的相同。

@tool
extends Node

@export var is_number_editable: bool:
    set(value):
        is_number_editable = value
        notify_property_list_changed()
@export var number: int

func _validate_property(property: Dictionary):
    if property.name == "number" and not is_number_editable:
        property.usage |= PROPERTY_USAGE_READ_ONLY

void add_user_signal(signal: String, arguments: Array = []) 🔗

添加名为 signal 的用户定义的信号。信号的参数是可选的,以字典的 Array 形式添加,字典中定义名称 name String,类型 type int(见 Variant.Type)。另见 has_user_signal()remove_user_signal()

add_user_signal("hurt", [
    { "name": "damage", "type": TYPE_INT },
    { "name": "source", "type": TYPE_OBJECT }
])

Variant call(method: StringName, ...) vararg 🔗

在对象上调用 method 并返回结果。该方法支持可变数量的参数,因此参数可以作为逗号分隔的列表传递。

var node = Node3D.new()
node.call("rotate", Vector3(1.0, 0.0, 0.0), 1.571)

注意:在 C# 中,在引用 Godot 内置方法时,method 必须为 snake_case 格式。最好使用 MethodName 类中公开的名称,以避免在每次调用时分配新的 StringName


Variant call_deferred(method: StringName, ...) vararg 🔗

Calls the method on the object during idle time. Always returns null, not the method's result.

Idle time happens mainly at the end of process and physics frames. In it, deferred calls will be run until there are none left, which means you can defer calls from other deferred calls and they'll still be run in the current idle time cycle. This means you should not call a method deferred from itself (or from a method called by it), as this causes infinite recursion the same way as if you had called the method directly.

This method supports a variable number of arguments, so parameters can be passed as a comma separated list.

var node = Node3D.new()
node.call_deferred("rotate", Vector3(1.0, 0.0, 0.0), 1.571)

For methods that are deferred from the same thread, the order of execution at idle time is identical to the order in which call_deferred was called.

See also Callable.call_deferred().

Note: In C#, method must be in snake_case when referring to built-in Godot methods. Prefer using the names exposed in the MethodName class to avoid allocating a new StringName on each call.

Note: If you're looking to delay the function call by a frame, refer to the SceneTree.process_frame and SceneTree.physics_frame signals.

var node = Node3D.new()
# Make a Callable and bind the arguments to the node's rotate() call.
var callable = node.rotate.bind(Vector3(1.0, 0.0, 0.0), 1.571)
# Connect the callable to the process_frame signal, so it gets called in the next process frame.
# CONNECT_ONE_SHOT makes sure it only gets called once instead of every frame.
get_tree().process_frame.connect(callable, CONNECT_ONE_SHOT)

Variant callv(method: StringName, arg_array: Array) 🔗

在对象上调用 method 并返回结果。与 call() 不同,该方法期望所有参数都包含在 arg_array 中。

var node = Node3D.new()
node.callv("rotate", [Vector3(1.0, 0.0, 0.0), 1.571])

注意:在 C# 中,method 在引用 Godot 内置方法时必须是 snake_case。最好使用 MethodName 类中公开的名称,以避免在每次调用时分配新的 StringName


bool can_translate_messages() const 🔗

如果允许该对象使用 tr()tr_n() 翻译消息,则返回 true。另见 set_message_translation()


void cancel_free() 🔗

如果在 NOTIFICATION_PREDELETE 时调用该方法,则该对象拒绝释放,仍会保持已分配的状态。主要是作为内部函数使用,用于错误处理,避免用户释放不想释放的对象。


Error connect(signal: StringName, callable: Callable, flags: int = 0) 🔗

Connects a signal by name to a callable. Optional flags can be also added to configure the connection's behavior (see ConnectFlags constants).

A signal can only be connected once to the same Callable. If the signal is already connected, this method returns @GlobalScope.ERR_INVALID_PARAMETER and generates an error, unless the signal is connected with CONNECT_REFERENCE_COUNTED. To prevent this, use is_connected() first to check for existing connections.

Note: If the callable's object is freed, the connection will be lost.

Note: In GDScript, it is generally recommended to connect signals with Signal.connect() instead.

Note: This method, and all other signal-related methods, are thread-safe.


void disconnect(signal: StringName, callable: Callable) 🔗

按名称从给定的 callable 断开 signal。如果连接不存在,则生成一个错误。使用 is_connected() 确保该连接存在。


Error emit_signal(signal: StringName, ...) vararg 🔗

按名称发出给定的 signal。该信号必须存在,所以它应该是该类或其继承类之一的内置信号,或者是用户定义的信号(参见 add_user_signal())。该方法支持可变数量的参数,所以参数可以以逗号分隔的列表形式传递。

如果 signal 不存在或参数无效,则返回 @GlobalScope.ERR_UNAVAILABLE

emit_signal("hit", "剑", 100)
emit_signal("game_over")

注意:在C#中,在引用内置 Godot 信号时,signal 必须是 snake_case。最好使用 SignalName 类中公开的名称,以避免在每次调用时分配一个新的 StringName


void free() 🔗

从内存中删除该对象。此前存在的对该对象的引用会失效,尝试访问这些引用会产生运行时错误。使用 @GlobalScope.is_instance_valid() 检查引用时将返回 false。等价于 GDExtension C++ 中的 memdelete 函数。


Variant get(property: StringName) const 🔗

返回给定 propertyVariant 值。如果 property 不存在,则该方法返回 null

var node = Node2D.new()
node.rotation = 1.5
var a = node.get("rotation") # a 为 1.5

注意:在 C# 中,在引用 Godot 内置属性时,property 必须是 snake_case。最好使用 PropertyName 类中公开的名称,以避免在每次调用时分配一个新的 StringName


String get_class() const 🔗

返回该对象的内置类名,作为一个 String。另见 is_class()

注意:该方法将忽略 class_name 声明。如果该对象的脚本定义了一个 class_name,则改为返回内置基类名称。


Array[Dictionary] get_incoming_connections() const 🔗

返回该对象接收到的信号连接的 Array。每个连接都被表示为包含三个条目的 Dictionary


Variant get_indexed(property_path: NodePath) const 🔗

获取该对象的某个属性,该属性的属性路径由 property_path 给出。该路径应该是相对于当前对象的 NodePath,可使用英文冒号(:)访问内嵌属性。

示例:"position:x""material:next_pass:blend_mode"

var node = Node2D.new()
node.position = Vector2(5, -10)
var a = node.get_indexed("position")   # a 为 Vector2(5, -10)
var b = node.get_indexed("position:y") # b 为 -10

注意:在 C# 中引用内置 Godot 属性时 property_path 必须为 snake_case 蛇形大小写。请优先使用 PropertyName 类中暴露的名称,避免每次调用都重新分配一个 StringName

注意:这个方法不支持指向 SceneTree 中节点的路径,仅支持子属性路径。在节点语境下,请改用 Node.get_node_and_resource()


int get_instance_id() const 🔗

返回该对象的唯一实例 ID。该 ID 可以保存在 EncodedObjectAsID 中,通过 @GlobalScope.instance_from_id() 可以检索到对应的对象实例。

注意:该 ID 仅在当前会话中有意义:通过网络传输后并不对应相同的对象,隔段时间后从文件中加载亦然。


Variant get_meta(name: StringName, default: Variant = null) const 🔗

返回该对象的元数据中名称为 name 的条目。如果不存在该条目,则返回 default。如果 defaultnull,则还会生成错误。

注意:元数据的名称必须是符合 StringName.is_valid_identifier() 的有效标识符。

注意:名称以下划线(_)开头的元数据仅供编辑器使用。仅供编辑器使用的元数据不会在“检查器”中显示,虽然仍然能够被这个方法找到,但是不应该进行编辑。


Array[StringName] get_meta_list() const 🔗

将该对象的元数据条目名称以元素为 StringNameArray 形式返回。


int get_method_argument_count(method: StringName) const 🔗

根据名称返回给定 method 的参数数量。

注意:在 C# 中引用内置 Godot 方法时,method 必须采用 snake_case 蛇形命名法。请优先使用 MethodName 类中公开的名称,以避免在每次调用时分配一个新的 StringName


Array[Dictionary] get_method_list() const 🔗

将该对象的方法及对应签名作为字典 Array 返回。每个 Dictionary 包含以下条目:

-name 是该方法的名称,为 String

-args 是代表参数的字典 Array

-default_args 是默认参数,为变体 Array

-flagsMethodFlags 的组合;

-id 是该方法的内部标识符 int

-return 是返回值,为 Dictionary

注意:argsreturn 的字典格式与 get_property_list() 的结果相同,但不会用到所有条目。


Array[Dictionary] get_property_list() const 🔗

以字典 Array 的形式返回该对象的属性列表。每个 Dictionary 中都包含如下条目:

注意:在 GDScript 中,类的所有成员都被视为属性。在 C# 和 GDExtension 中,则需要使用装饰器或特性将类的成员显式标记为 Godot 属性。


Variant get_script() const 🔗

返回该对象的 Script 实例,如果没有附加脚本,则返回 null


Array[Dictionary] get_signal_connection_list(signal: StringName) const 🔗

返回给定 signal 名称的连接的 Array。每个连接都被表示为包含三个条目的 Dictionary


Array[Dictionary] get_signal_list() const 🔗

Returns the list of existing signals as an Array of dictionaries.

Note: Due to the implementation, each Dictionary is formatted very similarly to the returned values of get_method_list().


StringName get_translation_domain() const 🔗

返回 tr()tr_n() 所使用的翻译域的名称。另见 TranslationServer


bool has_connections(signal: StringName) const 🔗

如果给定的信号名称 signal 存在连接,则返回 true

注意:在 C# 中,引用 Godot 内置方法时 signal 必须使用 snake_case 形式命名。请优先使用 SignalName 类中暴露的名称,避免每次调用都分配一个新的 StringName


bool has_meta(name: StringName) const 🔗

如果找到名称为 name 的元数据条目,则返回 true。另见 get_meta()set_meta()remove_meta()

注意:元数据的名称必须是符合 StringName.is_valid_identifier() 的有效标识符。

注意:名称以下划线(_)开头的元数据仅供编辑器使用。仅供编辑器使用的元数据不会在“检查器”中显示,虽然仍然能够被这个方法找到,但是不应该进行编辑。


bool has_method(method: StringName) const 🔗

如果该对象中存在给定的方法名 method,则返回 true

注意:在 C# 中引用内置 Godot 方法时 method 必须为 snake_case 蛇形大小写。请优先使用 MethodName 类中暴露的名称,避免每次调用都重新分配一个 StringName


bool has_signal(signal: StringName) const 🔗

如果对象中存在给定的信号名称 signal,则返回 true

注意:在 C# 中,引用 Godot 内置方法时 signal 必须使用 snake_case 形式命名。请优先使用 SignalName 类中暴露的名称,避免每次调用都分配一个新的 StringName


bool has_user_signal(signal: StringName) const 🔗

如果存在给定的用户定义信号名称 signal,则返回 true。仅包含通过 add_user_signal() 添加的信号。另见 remove_user_signal()


bool is_blocking_signals() const 🔗

如果该对象正在阻止发出信号,则返回 true。见 set_block_signals()


bool is_class(class: String) const 🔗

如果该对象继承自给定的 class 则返回 true。另见 get_class()

var sprite2d = Sprite2D.new()
sprite2d.is_class("Sprite2D") # 返回 true
sprite2d.is_class("Node")     # 返回 true
sprite2d.is_class("Node3D")   # 返回 false

注意:此方法忽略对象脚本中的 class_name 声明。


bool is_connected(signal: StringName, callable: Callable) const 🔗

如果给定的信号名称 signal 与可调用体 callable 之间存在连接,则返回 true

注意:在 C# 中,引用 Godot 内置方法时 signal 必须使用 snake_case 形式命名。请优先使用 SignalName 类中暴露的名称,避免每次调用都分配一个新的 StringName


bool is_queued_for_deletion() const 🔗

如果为该对象调用了 Node.queue_free() 方法,则返回 true


void notification(what: int, reversed: bool = false) 🔗

将给定的 what 通知发送给对象继承的所有类,触发对 _notification() 的调用,从最高祖先(Object 类)开始,向下一直到对象的脚本。

如果 reversedtrue,则调用顺序会被颠倒。

var player = Node2D.new()
player.set_script(load("res://player.gd"))

player.notification(NOTIFICATION_ENTER_TREE)
# 调用顺序是 Object -> Node -> Node2D -> player.gd。

player.notification(NOTIFICATION_ENTER_TREE, true)
# 调用顺序是 player.gd -> Node2D -> Node -> Object。

void notify_property_list_changed() 🔗

发出 property_list_changed 信号。这主要是用来刷新编辑器,以让检查器和编辑器插件被正确更新。


bool property_can_revert(property: StringName) const 🔗

如果给定的属性 property 有自定义的默认值,则返回 true。请使用 property_get_revert() 获取 property 的默认值。

注意:“检查器”面板会使用这个方法来显示恢复图标。该对象必须实现 _property_can_revert() 来自定义默认值。如果未实现 _property_can_revert(),则这个方法返回 false


Variant property_get_revert(property: StringName) const 🔗

返回给定的属性 property 的自定义默认值。请使用 property_can_revert() 检查 property 是否有自定义的默认值。

注意:“检查器”面板会使用这个方法来显示恢复图标。该对象必须实现 _property_get_revert() 来自定义默认值。如果未实现 _property_get_revert(),则这个方法返回 null


void remove_meta(name: StringName) 🔗

从对象的元数据中移除名称为 name 的条目。另见 has_meta()get_meta()set_meta()

注意:元数据的名称必须是符合 StringName.is_valid_identifier() 的有效标识符。

注意:名称以下划线(_)开头的元数据仅供编辑器使用。仅供编辑器使用的元数据不会在“检查器”中显示,虽然仍然能够被这个方法找到,但是不应该进行编辑。


void remove_user_signal(signal: StringName) 🔗

从对象中移除给定的用户信号 signal。另见 add_user_signal()has_user_signal()


void set(property: StringName, value: Variant) 🔗

将给定属性 property 的值分配为 value。如果该属性不存在,或者给定 value 的类型不匹配,则不会发生任何事情。

var node = Node2D.new()
node.set("global_scale", Vector2(8, 2.5))
print(node.global_scale) # 输出 (8.0, 2.5)

注意:在 C# 中,引用 Godot 内置方法时 property 必须使用 snake_case 形式命名。请优先使用 PropertyName 类中暴露的名称,避免每次调用都分配一个新的 StringName


void set_block_signals(enable: bool) 🔗

如果设置为 true,这该对象将无法发出信号。因此,emit_signal() 和信号连接将不起作用,直到该属性被设置为 false


void set_deferred(property: StringName, value: Variant) 🔗

在当前帧的末尾,将给定属性 property 的值分配为 value。等价于通过 call_deferred() 调用 set()

var node = Node2D.new()
add_child(node)

node.rotation = 1.5
node.set_deferred("rotation", 3.0)
print(node.rotation) # 输出 1.5

await get_tree().process_frame
print(node.rotation) # 输出 3.0

注意:在 C# 中引用内置 Godot 属性时 property 必须为 snake_case 蛇形大小写。请优先使用 PropertyName 类中暴露的名称,避免每次调用都重新分配一个 StringName


void set_indexed(property_path: NodePath, value: Variant) 🔗

将由属性路径 property_path 标识的属性的值分配为 value。该路径应为相对于这个对象的 NodePath,可以使用英文冒号(:)访问内嵌属性。

var node = Node2D.new()
node.set_indexed("position", Vector2(42, 0))
node.set_indexed("position:y", -10)
print(node.position) # 输出 (42.0, -10.0)

注意:在 C# 中,引用 Godot 内置方法时 property_path 必须使用 snake_case 形式命名。请优先使用 PropertyName 类中暴露的名称,避免每次调用都分配一个新的 StringName


void set_message_translation(enable: bool) 🔗

如果设置为 true,则允许对象使用 tr()tr_n() 翻译消息。该属性默认启用。另见 can_translate_messages()


void set_meta(name: StringName, value: Variant) 🔗

添加或更改对象元数据中名称为 name 的条目。元数据值 value 可以是任何 Variant,尽管某些类型无法正确序列化。

如果 valuenull,则该条目被移除。等价于使用 remove_meta()。另见 has_meta()get_meta()

注意:元数据的名称必须是符合 StringName.is_valid_identifier() 的有效标识符。

注意:名称以下划线(_)开头的元数据仅供编辑器使用。仅供编辑器使用的元数据不会在“检查器”中显示,虽然仍然能够被这个方法找到,但是不应该进行编辑。


void set_script(script: Variant) 🔗

将脚本 script 附加至该对象,并进行实例化。因此会调用该脚本的 _init()Script 可用于扩展对象的功能。

如果已存在脚本,则该脚本的实例会被分离,其属性值和状态会丢失。仍会保留内置属性的值。


void set_translation_domain(domain: StringName) 🔗

设置 tr()tr_n() 所使用的翻译域的名称。另见 TranslationServer


String to_string() 🔗

返回表示对象的 String。默认为 "<ClassName#RID>"。覆盖 _to_string() 以自定义对象的字符串表示形式。


String tr(message: StringName, context: StringName = &"") const 🔗

翻译一条 message,该翻译使用项目设置中配置的翻译目录。可指定额外的 context 来辅助翻译。请注意,大多数 Control 节点会自动翻译其上的字符串,因此该方法主要用于格式化字符串或自定义绘制的文本。

can_translate_messages()false,或者无可用的翻译,则该方法将原样返回 message。见 set_message_translation()

详细示例请见国际化游戏

注意:该方法无法在没有 Object 实例的情况下使用,因为它依赖 can_translate_messages() 方法。静态上下文中的字符串翻译,请使用 TranslationServer.translate()


String tr_n(message: StringName, plural_message: StringName, n: int, context: StringName = &"") const 🔗

使用项目设置中配置的翻译目录,翻译一个 messageplural_message。可以进一步指定 context 来帮助翻译。

如果 can_translate_messages()false,或者没有翻译可用,则该方法将返回 messageplural_message,而不做任何更改。请参阅 set_message_translation()

n 是消息主题的数字或数量。它被翻译系统用来获取当前语言的正确复数形式。

有关详细示例,请参阅《使用 gettext 进行本地化》

注意:负数和 float 数字可能不适用于某些可数科目。建议使用 tr() 处理这些情况。

注意:如果没有 Object 实例,则无法使用该方法,因为它需要 can_translate_messages() 方法。要在静态上下文中翻译字符串,请使用 TranslationServer.translate_plural()