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...
Script
继承: Resource < RefCounted < Object
派生: CSharpScript, GDScript, ScriptExtension
作为资源存储的类。
描述
作为资源存储的类。脚本可以扩展所有以它为实例的对象的功能。
这是所有脚本的基类,不应直接使用。尝试使用此类创建新脚本将导致错误。
脚本子类的 new
方法会创建新的实例。如果一个现有的对象的类与脚本的基类之一相匹配,Object.set_script() 会扩展该对象。
教程
属性
方法
can_instantiate() const |
|
get_base_script() const |
|
get_global_name() const |
|
get_instance_base_type() const |
|
get_property_default_value(property: StringName) |
|
get_rpc_config() const |
|
has_script_signal(signal_name: StringName) const |
|
has_source_code() const |
|
instance_has(base_object: Object) const |
|
is_abstract() const |
|
is_tool() const |
|
属性说明
脚本的源代码,如果源代码不可用,则为空字符串。当设置时,不会自动重新加载类的实现。
方法说明
bool can_instantiate() const 🔗
如果该脚本可以被实例化,则返回 true
。
Script get_base_script() const 🔗
返回由该脚本直接继承的脚本。
StringName get_global_name() const 🔗
返回与脚本关联的类名(如果有)。否则返回空字符串。
要为脚本指定全局名称,你可以在 GDScript 中使用 class_name
关键字,在 C# 中使用 [GlobalClass]
属性。
class_name MyNode
extends Node
using Godot;
[GlobalClass]
public partial class MyNode : Node
{
}
StringName get_instance_base_type() const 🔗
返回脚本的基类类型。
Variant get_property_default_value(property: StringName) 🔗
返回指定属性的默认值。
Variant get_rpc_config() const 🔗
返回一个 Dictionary,将方法名映射到由脚本定义的对应 RPC 配置。
Dictionary get_script_constant_map() 🔗
返回一个包含常量名称及其值的字典。
Array[Dictionary] get_script_method_list() 🔗
返回此 Script 中的方法列表。
Array[Dictionary] get_script_property_list() 🔗
返回此 Script 中的属性列表。
Array[Dictionary] get_script_signal_list() 🔗
返回在此 Script 中定义的用户信号列表。
bool has_script_signal(signal_name: StringName) const 🔗
如果脚本或基类定义了具有给定名称的信号,则返回 true
。
bool has_source_code() const 🔗
如果脚本包含非空源代码,则返回 true
。
注意:如果脚本没有源代码,并不意味着它无效或不可用。例如,使用二进制标记导出的 GDScript 没有源代码,但仍按预期运行并可以实例化。这可以使用 can_instantiate() 进行检查。
bool instance_has(base_object: Object) const 🔗
如果 base_object
是该脚本的实例,则返回 true
。
如果脚本是抽象脚本,则返回 true
。抽象脚本没有构造函数,无法被实例化。
如果脚本是工具脚本,则返回 true
。工具脚本可以在编辑器中运行。
Error reload(keep_state: bool = false) 🔗
重新加载脚本的类的实现。返回一个错误代码。