mirror of
https://github.com/godotengine/godot-docs-l10n.git
synced 2026-01-05 14:10:19 +03:00
348 lines
16 KiB
ReStructuredText
348 lines
16 KiB
ReStructuredText
:github_url: hide
|
|
|
|
.. DO NOT EDIT THIS FILE!!!
|
|
.. Generated automatically from Godot engine sources.
|
|
.. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
|
|
.. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/Script.xml.
|
|
|
|
.. _class_Script:
|
|
|
|
Script
|
|
======
|
|
|
|
**继承:** :ref:`Resource<class_Resource>` **<** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
|
|
|
|
**派生:** :ref:`CSharpScript<class_CSharpScript>`, :ref:`GDScript<class_GDScript>`, :ref:`ScriptExtension<class_ScriptExtension>`
|
|
|
|
作为资源存储的类。
|
|
|
|
.. rst-class:: classref-introduction-group
|
|
|
|
描述
|
|
----
|
|
|
|
作为资源存储的类。脚本可以扩展所有以它为实例的对象的功能。
|
|
|
|
这是所有脚本的基类,不应直接使用。尝试使用此类创建新脚本将导致错误。
|
|
|
|
脚本子类的 ``new`` 方法会创建新的实例。如果一个现有的对象的类与脚本的基类之一相匹配,\ :ref:`Object.set_script()<class_Object_method_set_script>` 会扩展该对象。
|
|
|
|
.. rst-class:: classref-introduction-group
|
|
|
|
教程
|
|
----
|
|
|
|
- :doc:`脚本文档索引 <../tutorials/scripting/index>`
|
|
|
|
.. rst-class:: classref-reftable-group
|
|
|
|
属性
|
|
----
|
|
|
|
.. table::
|
|
:widths: auto
|
|
|
|
+-----------------------------+-------------------------------------------------------+
|
|
| :ref:`String<class_String>` | :ref:`source_code<class_Script_property_source_code>` |
|
|
+-----------------------------+-------------------------------------------------------+
|
|
|
|
.. rst-class:: classref-reftable-group
|
|
|
|
方法
|
|
----
|
|
|
|
.. table::
|
|
:widths: auto
|
|
|
|
+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`bool<class_bool>` | :ref:`can_instantiate<class_Script_method_can_instantiate>`\ (\ ) |const| |
|
|
+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Script<class_Script>` | :ref:`get_base_script<class_Script_method_get_base_script>`\ (\ ) |const| |
|
|
+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`StringName<class_StringName>` | :ref:`get_global_name<class_Script_method_get_global_name>`\ (\ ) |const| |
|
|
+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`StringName<class_StringName>` | :ref:`get_instance_base_type<class_Script_method_get_instance_base_type>`\ (\ ) |const| |
|
|
+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Variant<class_Variant>` | :ref:`get_property_default_value<class_Script_method_get_property_default_value>`\ (\ property\: :ref:`StringName<class_StringName>`\ ) |
|
|
+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Variant<class_Variant>` | :ref:`get_rpc_config<class_Script_method_get_rpc_config>`\ (\ ) |const| |
|
|
+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Dictionary<class_Dictionary>` | :ref:`get_script_constant_map<class_Script_method_get_script_constant_map>`\ (\ ) |
|
|
+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Array<class_Array>`\[:ref:`Dictionary<class_Dictionary>`\] | :ref:`get_script_method_list<class_Script_method_get_script_method_list>`\ (\ ) |
|
|
+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Array<class_Array>`\[:ref:`Dictionary<class_Dictionary>`\] | :ref:`get_script_property_list<class_Script_method_get_script_property_list>`\ (\ ) |
|
|
+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Array<class_Array>`\[:ref:`Dictionary<class_Dictionary>`\] | :ref:`get_script_signal_list<class_Script_method_get_script_signal_list>`\ (\ ) |
|
|
+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`bool<class_bool>` | :ref:`has_script_signal<class_Script_method_has_script_signal>`\ (\ signal_name\: :ref:`StringName<class_StringName>`\ ) |const| |
|
|
+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`bool<class_bool>` | :ref:`has_source_code<class_Script_method_has_source_code>`\ (\ ) |const| |
|
|
+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`bool<class_bool>` | :ref:`instance_has<class_Script_method_instance_has>`\ (\ base_object\: :ref:`Object<class_Object>`\ ) |const| |
|
|
+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`bool<class_bool>` | :ref:`is_abstract<class_Script_method_is_abstract>`\ (\ ) |const| |
|
|
+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`bool<class_bool>` | :ref:`is_tool<class_Script_method_is_tool>`\ (\ ) |const| |
|
|
+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`Error<enum_@GlobalScope_Error>` | :ref:`reload<class_Script_method_reload>`\ (\ keep_state\: :ref:`bool<class_bool>` = false\ ) |
|
|
+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+
|
|
|
|
.. rst-class:: classref-section-separator
|
|
|
|
----
|
|
|
|
.. rst-class:: classref-descriptions-group
|
|
|
|
属性说明
|
|
--------
|
|
|
|
.. _class_Script_property_source_code:
|
|
|
|
.. rst-class:: classref-property
|
|
|
|
:ref:`String<class_String>` **source_code** :ref:`🔗<class_Script_property_source_code>`
|
|
|
|
.. rst-class:: classref-property-setget
|
|
|
|
- |void| **set_source_code**\ (\ value\: :ref:`String<class_String>`\ )
|
|
- :ref:`String<class_String>` **get_source_code**\ (\ )
|
|
|
|
脚本的源代码,如果源代码不可用,则为空字符串。当设置时,不会自动重新加载类的实现。
|
|
|
|
.. rst-class:: classref-section-separator
|
|
|
|
----
|
|
|
|
.. rst-class:: classref-descriptions-group
|
|
|
|
方法说明
|
|
--------
|
|
|
|
.. _class_Script_method_can_instantiate:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`bool<class_bool>` **can_instantiate**\ (\ ) |const| :ref:`🔗<class_Script_method_can_instantiate>`
|
|
|
|
如果该脚本可以被实例化,则返回 ``true``\ 。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_Script_method_get_base_script:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`Script<class_Script>` **get_base_script**\ (\ ) |const| :ref:`🔗<class_Script_method_get_base_script>`
|
|
|
|
返回由该脚本直接继承的脚本。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_Script_method_get_global_name:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`StringName<class_StringName>` **get_global_name**\ (\ ) |const| :ref:`🔗<class_Script_method_get_global_name>`
|
|
|
|
返回与脚本关联的类名(如果有)。否则返回空字符串。
|
|
|
|
要为脚本指定全局名称,你可以在 GDScript 中使用 ``class_name`` 关键字,在 C# 中使用 ``[GlobalClass]`` 属性。
|
|
|
|
|
|
.. tabs::
|
|
|
|
.. code-tab:: gdscript
|
|
|
|
class_name MyNode
|
|
extends Node
|
|
|
|
.. code-tab:: csharp
|
|
|
|
using Godot;
|
|
|
|
[GlobalClass]
|
|
public partial class MyNode : Node
|
|
{
|
|
}
|
|
|
|
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_Script_method_get_instance_base_type:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`StringName<class_StringName>` **get_instance_base_type**\ (\ ) |const| :ref:`🔗<class_Script_method_get_instance_base_type>`
|
|
|
|
返回脚本的基类类型。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_Script_method_get_property_default_value:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`Variant<class_Variant>` **get_property_default_value**\ (\ property\: :ref:`StringName<class_StringName>`\ ) :ref:`🔗<class_Script_method_get_property_default_value>`
|
|
|
|
返回指定属性的默认值。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_Script_method_get_rpc_config:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`Variant<class_Variant>` **get_rpc_config**\ (\ ) |const| :ref:`🔗<class_Script_method_get_rpc_config>`
|
|
|
|
返回一个 :ref:`Dictionary<class_Dictionary>`\ ,将方法名映射到由脚本定义的对应 RPC 配置。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_Script_method_get_script_constant_map:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`Dictionary<class_Dictionary>` **get_script_constant_map**\ (\ ) :ref:`🔗<class_Script_method_get_script_constant_map>`
|
|
|
|
返回一个包含常量名称及其值的字典。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_Script_method_get_script_method_list:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`Array<class_Array>`\[:ref:`Dictionary<class_Dictionary>`\] **get_script_method_list**\ (\ ) :ref:`🔗<class_Script_method_get_script_method_list>`
|
|
|
|
返回该 **Script** 中的方法列表。
|
|
|
|
\ **注意:**\ 该方法所返回的字典与 :ref:`Object.get_method_list()<class_Object_method_get_method_list>` 返回值的格式是一样的。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_Script_method_get_script_property_list:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`Array<class_Array>`\[:ref:`Dictionary<class_Dictionary>`\] **get_script_property_list**\ (\ ) :ref:`🔗<class_Script_method_get_script_property_list>`
|
|
|
|
返回该 **Script** 中的属性列表。
|
|
|
|
\ **注意:**\ 该方法所返回的字典与 :ref:`Object.get_property_list()<class_Object_method_get_property_list>` 返回值的格式是一样的。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_Script_method_get_script_signal_list:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`Array<class_Array>`\[:ref:`Dictionary<class_Dictionary>`\] **get_script_signal_list**\ (\ ) :ref:`🔗<class_Script_method_get_script_signal_list>`
|
|
|
|
返回该 **Script** 中定义的用户信号列表。
|
|
|
|
\ **注意:**\ 该方法所返回的字典与 :ref:`Object.get_signal_list()<class_Object_method_get_signal_list>` 返回值的格式是一样的。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_Script_method_has_script_signal:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`bool<class_bool>` **has_script_signal**\ (\ signal_name\: :ref:`StringName<class_StringName>`\ ) |const| :ref:`🔗<class_Script_method_has_script_signal>`
|
|
|
|
如果脚本或基类定义了具有给定名称的信号,则返回 ``true``\ 。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_Script_method_has_source_code:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`bool<class_bool>` **has_source_code**\ (\ ) |const| :ref:`🔗<class_Script_method_has_source_code>`
|
|
|
|
如果脚本包含非空源代码,则返回 ``true``\ 。
|
|
|
|
\ **注意:**\ 如果脚本没有源代码,并不意味着它无效或不可用。例如,使用二进制标记导出的 :ref:`GDScript<class_GDScript>` 没有源代码,但仍按预期运行并可以实例化。这可以使用 :ref:`can_instantiate()<class_Script_method_can_instantiate>` 进行检查。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_Script_method_instance_has:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`bool<class_bool>` **instance_has**\ (\ base_object\: :ref:`Object<class_Object>`\ ) |const| :ref:`🔗<class_Script_method_instance_has>`
|
|
|
|
如果 ``base_object`` 是该脚本的实例,则返回 ``true``\ 。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_Script_method_is_abstract:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`bool<class_bool>` **is_abstract**\ (\ ) |const| :ref:`🔗<class_Script_method_is_abstract>`
|
|
|
|
如果脚本是抽象脚本,则返回 ``true``\ 。抽象脚本没有构造函数,无法被实例化。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_Script_method_is_tool:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`bool<class_bool>` **is_tool**\ (\ ) |const| :ref:`🔗<class_Script_method_is_tool>`
|
|
|
|
如果脚本是工具脚本,则返回 ``true``\ 。工具脚本可以在编辑器中运行。
|
|
|
|
.. rst-class:: classref-item-separator
|
|
|
|
----
|
|
|
|
.. _class_Script_method_reload:
|
|
|
|
.. rst-class:: classref-method
|
|
|
|
:ref:`Error<enum_@GlobalScope_Error>` **reload**\ (\ keep_state\: :ref:`bool<class_bool>` = false\ ) :ref:`🔗<class_Script_method_reload>`
|
|
|
|
重新加载脚本的类的实现。返回一个错误代码。
|
|
|
|
.. |virtual| replace:: :abbr:`virtual (本方法通常需要用户覆盖才能生效。)`
|
|
.. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)`
|
|
.. |const| replace:: :abbr:`const (本方法无副作用,不会修改该实例的任何成员变量。)`
|
|
.. |vararg| replace:: :abbr:`vararg (本方法除了能接受在此处描述的参数外,还能够继续接受任意数量的参数。)`
|
|
.. |constructor| replace:: :abbr:`constructor (本方法用于构造某个类型。)`
|
|
.. |static| replace:: :abbr:`static (调用本方法无需实例,可直接使用类名进行调用。)`
|
|
.. |operator| replace:: :abbr:`operator (本方法描述的是使用本类型作为左操作数的有效运算符。)`
|
|
.. |bitfield| replace:: :abbr:`BitField (这个值是由下列位标志构成位掩码的整数。)`
|
|
.. |void| replace:: :abbr:`void (无返回值。)`
|