[email protected] 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/modules/gdscript/doc_classes/@GDScript.xml.
  6. .. _class_@GDScript:
  7. @GDScript
  8. =========
  9. Built-in GDScript functions.
  10. .. rst-class:: classref-introduction-group
  11. Description
  12. -----------
  13. A list of GDScript-specific utility functions and annotations accessible from any script.
  14. For the list of the global functions and constants see :ref:`@GlobalScope<class_@GlobalScope>`.
  15. .. rst-class:: classref-introduction-group
  16. Tutorials
  17. ---------
  18. - :doc:`GDScript exports <../tutorials/scripting/gdscript/gdscript_exports>`
  19. .. rst-class:: classref-reftable-group
  20. Methods
  21. -------
  22. .. table::
  23. :widths: auto
  24. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  25. | :ref:`Color<class_Color>` | :ref:`Color8<class_@GDScript_method_Color8>` **(** :ref:`int<class_int>` r8, :ref:`int<class_int>` g8, :ref:`int<class_int>` b8, :ref:`int<class_int>` a8=255 **)** |
  26. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  27. | void | :ref:`assert<class_@GDScript_method_assert>` **(** :ref:`bool<class_bool>` condition, :ref:`String<class_String>` message="" **)** |
  28. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  29. | :ref:`String<class_String>` | :ref:`char<class_@GDScript_method_char>` **(** :ref:`int<class_int>` char **)** |
  30. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  31. | :ref:`Variant<class_Variant>` | :ref:`convert<class_@GDScript_method_convert>` **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` type **)** |
  32. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  33. | :ref:`Object<class_Object>` | :ref:`dict_to_inst<class_@GDScript_method_dict_to_inst>` **(** :ref:`Dictionary<class_Dictionary>` dictionary **)** |
  34. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  35. | :ref:`Array<class_Array>` | :ref:`get_stack<class_@GDScript_method_get_stack>` **(** **)** |
  36. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  37. | :ref:`Dictionary<class_Dictionary>` | :ref:`inst_to_dict<class_@GDScript_method_inst_to_dict>` **(** :ref:`Object<class_Object>` instance **)** |
  38. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  39. | :ref:`int<class_int>` | :ref:`len<class_@GDScript_method_len>` **(** :ref:`Variant<class_Variant>` var **)** |
  40. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  41. | :ref:`Resource<class_Resource>` | :ref:`load<class_@GDScript_method_load>` **(** :ref:`String<class_String>` path **)** |
  42. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  43. | :ref:`Resource<class_Resource>` | :ref:`preload<class_@GDScript_method_preload>` **(** :ref:`String<class_String>` path **)** |
  44. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  45. | void | :ref:`print_debug<class_@GDScript_method_print_debug>` **(** ... **)** |vararg| |
  46. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  47. | void | :ref:`print_stack<class_@GDScript_method_print_stack>` **(** **)** |
  48. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  49. | :ref:`Array<class_Array>` | :ref:`range<class_@GDScript_method_range>` **(** ... **)** |vararg| |
  50. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  51. | :ref:`bool<class_bool>` | :ref:`type_exists<class_@GDScript_method_type_exists>` **(** :ref:`StringName<class_StringName>` type **)** |
  52. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  53. .. rst-class:: classref-section-separator
  54. ----
  55. .. rst-class:: classref-descriptions-group
  56. Constants
  57. ---------
  58. .. _class_@GDScript_constant_PI:
  59. .. rst-class:: classref-constant
  60. **PI** = ``3.14159265358979``
  61. Constant that represents how many times the diameter of a circle fits around its perimeter. This is equivalent to ``TAU / 2``, or 180 degrees in rotations.
  62. .. _class_@GDScript_constant_TAU:
  63. .. rst-class:: classref-constant
  64. **TAU** = ``6.28318530717959``
  65. The circle constant, the circumference of the unit circle in radians. This is equivalent to ``PI * 2``, or 360 degrees in rotations.
  66. .. _class_@GDScript_constant_INF:
  67. .. rst-class:: classref-constant
  68. **INF** = ``inf``
  69. Positive floating-point infinity. This is the result of floating-point division when the divisor is ``0.0``. For negative infinity, use ``-INF``. Dividing by ``-0.0`` will result in negative infinity if the numerator is positive, so dividing by ``0.0`` is not the same as dividing by ``-0.0`` (despite ``0.0 == -0.0`` returning ``true``).
  70. \ **Warning:** Numeric infinity is only a concept with floating-point numbers, and has no equivalent for integers. Dividing an integer number by ``0`` will not result in :ref:`INF<class_@GDScript_constant_INF>` and will result in a run-time error instead.
  71. .. _class_@GDScript_constant_NAN:
  72. .. rst-class:: classref-constant
  73. **NAN** = ``nan``
  74. "Not a Number", an invalid floating-point value. :ref:`NAN<class_@GDScript_constant_NAN>` has special properties, including that it is not equal to itself (``NAN == NAN`` returns ``false``). It is output by some invalid operations, such as dividing floating-point ``0.0`` by ``0.0``.
  75. \ **Warning:** "Not a Number" is only a concept with floating-point numbers, and has no equivalent for integers. Dividing an integer ``0`` by ``0`` will not result in :ref:`NAN<class_@GDScript_constant_NAN>` and will result in a run-time error instead.
  76. .. rst-class:: classref-section-separator
  77. ----
  78. Annotations
  79. -----------
  80. .. _class_@GDScript_annotation_@export:
  81. .. rst-class:: classref-annotation
  82. **@export** **(** **)**
  83. Mark the following property as exported (editable in the Inspector dock and saved to disk). To control the type of the exported property, use the type hint notation.
  84. ::
  85. @export var string = ""
  86. @export var int_number = 5
  87. @export var float_number: float = 5
  88. @export var image : Image
  89. .. rst-class:: classref-item-separator
  90. ----
  91. .. _class_@GDScript_annotation_@export_category:
  92. .. rst-class:: classref-annotation
  93. **@export_category** **(** :ref:`String<class_String>` name **)**
  94. Define a new category for the following exported properties. This helps to organize properties in the Inspector dock.
  95. See also :ref:`@GlobalScope.PROPERTY_USAGE_CATEGORY<class_@GlobalScope_constant_PROPERTY_USAGE_CATEGORY>`.
  96. ::
  97. @export_category("Statistics")
  98. @export var hp = 30
  99. @export var speed = 1.25
  100. \ **Note:** Categories in the Inspector dock's list usually divide properties coming from different classes (Node, Node2D, Sprite, etc.). For better clarity, it's recommended to use :ref:`@export_group<class_@GDScript_annotation_@export_group>` and :ref:`@export_subgroup<class_@GDScript_annotation_@export_subgroup>`, instead.
  101. .. rst-class:: classref-item-separator
  102. ----
  103. .. _class_@GDScript_annotation_@export_color_no_alpha:
  104. .. rst-class:: classref-annotation
  105. **@export_color_no_alpha** **(** **)**
  106. Export a :ref:`Color<class_Color>` property without allowing its transparency (:ref:`Color.a<class_Color_property_a>`) to be edited.
  107. See also :ref:`@GlobalScope.PROPERTY_HINT_COLOR_NO_ALPHA<class_@GlobalScope_constant_PROPERTY_HINT_COLOR_NO_ALPHA>`.
  108. ::
  109. @export_color_no_alpha var dye_color : Color
  110. .. rst-class:: classref-item-separator
  111. ----
  112. .. _class_@GDScript_annotation_@export_dir:
  113. .. rst-class:: classref-annotation
  114. **@export_dir** **(** **)**
  115. Export a :ref:`String<class_String>` property as a path to a directory. The path will be limited to the project folder and its subfolders. See :ref:`@export_global_dir<class_@GDScript_annotation_@export_global_dir>` to allow picking from the entire filesystem.
  116. See also :ref:`@GlobalScope.PROPERTY_HINT_DIR<class_@GlobalScope_constant_PROPERTY_HINT_DIR>`.
  117. ::
  118. @export_dir var sprite_folder_path: String
  119. .. rst-class:: classref-item-separator
  120. ----
  121. .. _class_@GDScript_annotation_@export_enum:
  122. .. rst-class:: classref-annotation
  123. **@export_enum** **(** :ref:`String<class_String>` names, ... **)** |vararg|
  124. Export an :ref:`int<class_int>` or :ref:`String<class_String>` property as an enumerated list of options. If the property is an :ref:`int<class_int>`, then the index of the value is stored, in the same order the values are provided. You can add explicit values using a colon. If the property is a :ref:`String<class_String>`, then the value is stored.
  125. See also :ref:`@GlobalScope.PROPERTY_HINT_ENUM<class_@GlobalScope_constant_PROPERTY_HINT_ENUM>`.
  126. ::
  127. @export_enum("Warrior", "Magician", "Thief") var character_class: int
  128. @export_enum("Slow:30", "Average:60", "Very Fast:200") var character_speed: int
  129. @export_enum("Rebecca", "Mary", "Leah") var character_name: String
  130. If you want to set an initial value, you must specify it explicitly:
  131. ::
  132. @export_enum("Rebecca", "Mary", "Leah") var character_name: String = "Rebecca"
  133. If you want to use named GDScript enums, then use :ref:`@export<class_@GDScript_annotation_@export>` instead:
  134. ::
  135. enum CharacterName {REBECCA, MARY, LEAH}
  136. @export var character_name: CharacterName
  137. .. rst-class:: classref-item-separator
  138. ----
  139. .. _class_@GDScript_annotation_@export_exp_easing:
  140. .. rst-class:: classref-annotation
  141. **@export_exp_easing** **(** :ref:`String<class_String>` hints="", ... **)** |vararg|
  142. Export a floating-point property with an easing editor widget. Additional hints can be provided to adjust the behavior of the widget. ``"attenuation"`` flips the curve, which makes it more intuitive for editing attenuation properties. ``"positive_only"`` limits values to only be greater than or equal to zero.
  143. See also :ref:`@GlobalScope.PROPERTY_HINT_EXP_EASING<class_@GlobalScope_constant_PROPERTY_HINT_EXP_EASING>`.
  144. ::
  145. @export_exp_easing var transition_speed
  146. @export_exp_easing("attenuation") var fading_attenuation
  147. @export_exp_easing("positive_only") var effect_power
  148. .. rst-class:: classref-item-separator
  149. ----
  150. .. _class_@GDScript_annotation_@export_file:
  151. .. rst-class:: classref-annotation
  152. **@export_file** **(** :ref:`String<class_String>` filter="", ... **)** |vararg|
  153. Export a :ref:`String<class_String>` property as a path to a file. The path will be limited to the project folder and its subfolders. See :ref:`@export_global_file<class_@GDScript_annotation_@export_global_file>` to allow picking from the entire filesystem.
  154. If ``filter`` is provided, only matching files will be available for picking.
  155. See also :ref:`@GlobalScope.PROPERTY_HINT_FILE<class_@GlobalScope_constant_PROPERTY_HINT_FILE>`.
  156. ::
  157. @export_file var sound_effect_path: String
  158. @export_file("*.txt") var notes_path: String
  159. .. rst-class:: classref-item-separator
  160. ----
  161. .. _class_@GDScript_annotation_@export_flags:
  162. .. rst-class:: classref-annotation
  163. **@export_flags** **(** :ref:`String<class_String>` names, ... **)** |vararg|
  164. Export an integer property as a bit flag field. This allows to store several "checked" or ``true`` values with one property, and comfortably select them from the Inspector dock.
  165. See also :ref:`@GlobalScope.PROPERTY_HINT_FLAGS<class_@GlobalScope_constant_PROPERTY_HINT_FLAGS>`.
  166. ::
  167. @export_flags("Fire", "Water", "Earth", "Wind") var spell_elements = 0
  168. You can add explicit values using a colon:
  169. ::
  170. @export_flags("Self:4", "Allies:8", "Foes:16") var spell_targets = 0
  171. You can also combine several flags:
  172. ::
  173. @export_flags("Self:4", "Allies:8", "Self and Allies:12", "Foes:16")
  174. var spell_targets = 0
  175. \ **Note:** A flag value must be at least ``1`` and at most ``2 ** 32 - 1``.
  176. \ **Note:** Unlike :ref:`@export_enum<class_@GDScript_annotation_@export_enum>`, the previous explicit value is not taken into account. In the following example, A is 16, B is 2, C is 4.
  177. ::
  178. @export_flags("A:16", "B", "C") var x
  179. .. rst-class:: classref-item-separator
  180. ----
  181. .. _class_@GDScript_annotation_@export_flags_2d_navigation:
  182. .. rst-class:: classref-annotation
  183. **@export_flags_2d_navigation** **(** **)**
  184. Export an integer property as a bit flag field for 2D navigation layers. The widget in the Inspector dock will use the layer names defined in :ref:`ProjectSettings.layer_names/2d_navigation/layer_1<class_ProjectSettings_property_layer_names/2d_navigation/layer_1>`.
  185. See also :ref:`@GlobalScope.PROPERTY_HINT_LAYERS_2D_NAVIGATION<class_@GlobalScope_constant_PROPERTY_HINT_LAYERS_2D_NAVIGATION>`.
  186. ::
  187. @export_flags_2d_navigation var navigation_layers: int
  188. .. rst-class:: classref-item-separator
  189. ----
  190. .. _class_@GDScript_annotation_@export_flags_2d_physics:
  191. .. rst-class:: classref-annotation
  192. **@export_flags_2d_physics** **(** **)**
  193. Export an integer property as a bit flag field for 2D physics layers. The widget in the Inspector dock will use the layer names defined in :ref:`ProjectSettings.layer_names/2d_physics/layer_1<class_ProjectSettings_property_layer_names/2d_physics/layer_1>`.
  194. See also :ref:`@GlobalScope.PROPERTY_HINT_LAYERS_2D_PHYSICS<class_@GlobalScope_constant_PROPERTY_HINT_LAYERS_2D_PHYSICS>`.
  195. ::
  196. @export_flags_2d_physics var physics_layers: int
  197. .. rst-class:: classref-item-separator
  198. ----
  199. .. _class_@GDScript_annotation_@export_flags_2d_render:
  200. .. rst-class:: classref-annotation
  201. **@export_flags_2d_render** **(** **)**
  202. Export an integer property as a bit flag field for 2D render layers. The widget in the Inspector dock will use the layer names defined in :ref:`ProjectSettings.layer_names/2d_render/layer_1<class_ProjectSettings_property_layer_names/2d_render/layer_1>`.
  203. See also :ref:`@GlobalScope.PROPERTY_HINT_LAYERS_2D_RENDER<class_@GlobalScope_constant_PROPERTY_HINT_LAYERS_2D_RENDER>`.
  204. ::
  205. @export_flags_2d_render var render_layers: int
  206. .. rst-class:: classref-item-separator
  207. ----
  208. .. _class_@GDScript_annotation_@export_flags_3d_navigation:
  209. .. rst-class:: classref-annotation
  210. **@export_flags_3d_navigation** **(** **)**
  211. Export an integer property as a bit flag field for 3D navigation layers. The widget in the Inspector dock will use the layer names defined in :ref:`ProjectSettings.layer_names/3d_navigation/layer_1<class_ProjectSettings_property_layer_names/3d_navigation/layer_1>`.
  212. See also :ref:`@GlobalScope.PROPERTY_HINT_LAYERS_3D_NAVIGATION<class_@GlobalScope_constant_PROPERTY_HINT_LAYERS_3D_NAVIGATION>`.
  213. ::
  214. @export_flags_3d_navigation var navigation_layers: int
  215. .. rst-class:: classref-item-separator
  216. ----
  217. .. _class_@GDScript_annotation_@export_flags_3d_physics:
  218. .. rst-class:: classref-annotation
  219. **@export_flags_3d_physics** **(** **)**
  220. Export an integer property as a bit flag field for 3D physics layers. The widget in the Inspector dock will use the layer names defined in :ref:`ProjectSettings.layer_names/3d_physics/layer_1<class_ProjectSettings_property_layer_names/3d_physics/layer_1>`.
  221. See also :ref:`@GlobalScope.PROPERTY_HINT_LAYERS_3D_PHYSICS<class_@GlobalScope_constant_PROPERTY_HINT_LAYERS_3D_PHYSICS>`.
  222. ::
  223. @export_flags_3d_physics var physics_layers: int
  224. .. rst-class:: classref-item-separator
  225. ----
  226. .. _class_@GDScript_annotation_@export_flags_3d_render:
  227. .. rst-class:: classref-annotation
  228. **@export_flags_3d_render** **(** **)**
  229. Export an integer property as a bit flag field for 3D render layers. The widget in the Inspector dock will use the layer names defined in :ref:`ProjectSettings.layer_names/3d_render/layer_1<class_ProjectSettings_property_layer_names/3d_render/layer_1>`.
  230. See also :ref:`@GlobalScope.PROPERTY_HINT_LAYERS_3D_RENDER<class_@GlobalScope_constant_PROPERTY_HINT_LAYERS_3D_RENDER>`.
  231. ::
  232. @export_flags_3d_render var render_layers: int
  233. .. rst-class:: classref-item-separator
  234. ----
  235. .. _class_@GDScript_annotation_@export_global_dir:
  236. .. rst-class:: classref-annotation
  237. **@export_global_dir** **(** **)**
  238. Export a :ref:`String<class_String>` property as an absolute path to a directory. The path can be picked from the entire filesystem. See :ref:`@export_dir<class_@GDScript_annotation_@export_dir>` to limit it to the project folder and its subfolders.
  239. See also :ref:`@GlobalScope.PROPERTY_HINT_GLOBAL_DIR<class_@GlobalScope_constant_PROPERTY_HINT_GLOBAL_DIR>`.
  240. ::
  241. @export_global_dir var sprite_folder_path: String
  242. .. rst-class:: classref-item-separator
  243. ----
  244. .. _class_@GDScript_annotation_@export_global_file:
  245. .. rst-class:: classref-annotation
  246. **@export_global_file** **(** :ref:`String<class_String>` filter="", ... **)** |vararg|
  247. Export a :ref:`String<class_String>` property as an absolute path to a file. The path can be picked from the entire filesystem. See :ref:`@export_file<class_@GDScript_annotation_@export_file>` to limit it to the project folder and its subfolders.
  248. If ``filter`` is provided, only matching files will be available for picking.
  249. See also :ref:`@GlobalScope.PROPERTY_HINT_GLOBAL_FILE<class_@GlobalScope_constant_PROPERTY_HINT_GLOBAL_FILE>`.
  250. ::
  251. @export_global_file var sound_effect_path: String
  252. @export_global_file("*.txt") var notes_path: String
  253. .. rst-class:: classref-item-separator
  254. ----
  255. .. _class_@GDScript_annotation_@export_group:
  256. .. rst-class:: classref-annotation
  257. **@export_group** **(** :ref:`String<class_String>` name, :ref:`String<class_String>` prefix="" **)**
  258. Define a new group for the following exported properties. This helps to organize properties in the Inspector dock. Groups can be added with an optional ``prefix``, which would make group to only consider properties that have this prefix. The grouping will break on the first property that doesn't have a prefix. The prefix is also removed from the property's name in the Inspector dock.
  259. If no ``prefix`` is provided, the every following property is added to the group. The group ends when then next group or category is defined. You can also force end a group by using this annotation with empty strings for parameters, ``@export_group("", "")``.
  260. Groups cannot be nested, use :ref:`@export_subgroup<class_@GDScript_annotation_@export_subgroup>` to add subgroups within groups.
  261. See also :ref:`@GlobalScope.PROPERTY_USAGE_GROUP<class_@GlobalScope_constant_PROPERTY_USAGE_GROUP>`.
  262. ::
  263. @export_group("Racer Properties")
  264. @export var nickname = "Nick"
  265. @export var age = 26
  266. @export_group("Car Properties", "car_")
  267. @export var car_label = "Speedy"
  268. @export var car_number = 3
  269. @export_group("", "")
  270. @export var ungrouped_number = 3
  271. .. rst-class:: classref-item-separator
  272. ----
  273. .. _class_@GDScript_annotation_@export_multiline:
  274. .. rst-class:: classref-annotation
  275. **@export_multiline** **(** **)**
  276. Export a :ref:`String<class_String>` property with a large :ref:`TextEdit<class_TextEdit>` widget instead of a :ref:`LineEdit<class_LineEdit>`. This adds support for multiline content and makes it easier to edit large amount of text stored in the property.
  277. See also :ref:`@GlobalScope.PROPERTY_HINT_MULTILINE_TEXT<class_@GlobalScope_constant_PROPERTY_HINT_MULTILINE_TEXT>`.
  278. ::
  279. @export_multiline var character_biography
  280. .. rst-class:: classref-item-separator
  281. ----
  282. .. _class_@GDScript_annotation_@export_node_path:
  283. .. rst-class:: classref-annotation
  284. **@export_node_path** **(** :ref:`String<class_String>` type="", ... **)** |vararg|
  285. Export a :ref:`NodePath<class_NodePath>` property with a filter for allowed node types.
  286. See also :ref:`@GlobalScope.PROPERTY_HINT_NODE_PATH_VALID_TYPES<class_@GlobalScope_constant_PROPERTY_HINT_NODE_PATH_VALID_TYPES>`.
  287. ::
  288. @export_node_path("Button", "TouchScreenButton") var some_button
  289. .. rst-class:: classref-item-separator
  290. ----
  291. .. _class_@GDScript_annotation_@export_placeholder:
  292. .. rst-class:: classref-annotation
  293. **@export_placeholder** **(** :ref:`String<class_String>` placeholder **)**
  294. Export a :ref:`String<class_String>` property with a placeholder text displayed in the editor widget when no value is present.
  295. See also :ref:`@GlobalScope.PROPERTY_HINT_PLACEHOLDER_TEXT<class_@GlobalScope_constant_PROPERTY_HINT_PLACEHOLDER_TEXT>`.
  296. ::
  297. @export_placeholder("Name in lowercase") var character_id: String
  298. .. rst-class:: classref-item-separator
  299. ----
  300. .. _class_@GDScript_annotation_@export_range:
  301. .. rst-class:: classref-annotation
  302. **@export_range** **(** :ref:`float<class_float>` min, :ref:`float<class_float>` max, :ref:`float<class_float>` step=1.0, :ref:`String<class_String>` extra_hints="", ... **)** |vararg|
  303. Export an :ref:`int<class_int>` or :ref:`float<class_float>` property as a range value. The range must be defined by ``min`` and ``max``, as well as an optional ``step`` and a variety of extra hints. The ``step`` defaults to ``1`` for integer properties. For floating-point numbers this value depends on your ``EditorSettings.interface/inspector/default_float_step`` setting.
  304. If hints ``"or_greater"`` and ``"or_less"`` are provided, the editor widget will not cap the value at range boundaries. The ``"exp"`` hint will make the edited values on range to change exponentially. The ``"hide_slider"`` hint will hide the slider element of the editor widget.
  305. Hints also allow to indicate the units for the edited value. Using ``"radians"`` you can specify that the actual value is in radians, but should be displayed in degrees in the Inspector dock. ``"degrees"`` allows to add a degree sign as a unit suffix. Finally, a custom suffix can be provided using ``"suffix:unit"``, where "unit" can be any string.
  306. See also :ref:`@GlobalScope.PROPERTY_HINT_RANGE<class_@GlobalScope_constant_PROPERTY_HINT_RANGE>`.
  307. ::
  308. @export_range(0, 20) var number
  309. @export_range(-10, 20) var number
  310. @export_range(-10, 20, 0.2) var number: float
  311. @export_range(0, 100, 1, "or_greater") var power_percent
  312. @export_range(0, 100, 1, "or_greater", "or_less") var health_delta
  313. @export_range(-3.14, 3.14, 0.001, "radians") var angle_radians
  314. @export_range(0, 360, 1, "degrees") var angle_degrees
  315. @export_range(-8, 8, 2, "suffix:px") var target_offset
  316. .. rst-class:: classref-item-separator
  317. ----
  318. .. _class_@GDScript_annotation_@export_subgroup:
  319. .. rst-class:: classref-annotation
  320. **@export_subgroup** **(** :ref:`String<class_String>` name, :ref:`String<class_String>` prefix="" **)**
  321. Define a new subgroup for the following exported properties. This helps to organize properties in the Inspector dock. Subgroups work exactly like groups, except they need a parent group to exist. See :ref:`@export_group<class_@GDScript_annotation_@export_group>`.
  322. See also :ref:`@GlobalScope.PROPERTY_USAGE_SUBGROUP<class_@GlobalScope_constant_PROPERTY_USAGE_SUBGROUP>`.
  323. ::
  324. @export_group("Racer Properties")
  325. @export var nickname = "Nick"
  326. @export var age = 26
  327. @export_subgroup("Car Properties", "car_")
  328. @export var car_label = "Speedy"
  329. @export var car_number = 3
  330. \ **Note:** Subgroups cannot be nested, they only provide one extra level of depth. Just like the next group ends the previous group, so do the subsequent subgroups.
  331. .. rst-class:: classref-item-separator
  332. ----
  333. .. _class_@GDScript_annotation_@icon:
  334. .. rst-class:: classref-annotation
  335. **@icon** **(** :ref:`String<class_String>` icon_path **)**
  336. Add a custom icon to the current script. The script must be registered as a global class using the ``class_name`` keyword for this to have a visible effect. The icon specified at ``icon_path`` is displayed in the Scene dock for every node of that class, as well as in various editor dialogs.
  337. ::
  338. @icon("res://path/to/class/icon.svg")
  339. \ **Note:** Only the script can have a custom icon. Inner classes are not supported.
  340. \ **Note:** As annotations describe their subject, the ``@icon`` annotation must be placed before the class definition and inheritance.
  341. \ **Note:** Unlike other annotations, the argument of the ``@icon`` annotation must be a string literal (constant expressions are not supported).
  342. .. rst-class:: classref-item-separator
  343. ----
  344. .. _class_@GDScript_annotation_@onready:
  345. .. rst-class:: classref-annotation
  346. **@onready** **(** **)**
  347. Mark the following property as assigned when the :ref:`Node<class_Node>` is ready. Values for these properties are not assigned immediately when the node is initialized (:ref:`Object._init<class_Object_method__init>`), and instead are computed and stored right before :ref:`Node._ready<class_Node_method__ready>`.
  348. ::
  349. @onready var character_name: Label = $Label
  350. .. rst-class:: classref-item-separator
  351. ----
  352. .. _class_@GDScript_annotation_@rpc:
  353. .. rst-class:: classref-annotation
  354. **@rpc** **(** :ref:`String<class_String>` mode="authority", :ref:`String<class_String>` sync="call_remote", :ref:`String<class_String>` transfer_mode="unreliable", :ref:`int<class_int>` transfer_channel=0, ... **)** |vararg|
  355. Mark the following method for remote procedure calls. See :doc:`High-level multiplayer <../tutorials/networking/high_level_multiplayer>`.
  356. The order of ``mode``, ``sync`` and ``transfer_mode`` does not matter and all arguments can be omitted, but ``transfer_channel`` always has to be the last argument. The accepted values for ``mode`` are ``"any_peer"`` or ``"authority"``, for ``sync`` are ``"call_remote"`` or ``"call_local"`` and for ``transfer_mode`` are ``"unreliable"``, ``"unreliable_ordered"`` or ``"reliable"``.
  357. ::
  358. @rpc
  359. func fn(): pass
  360. @rpc("any_peer", "unreliable_ordered")
  361. func fn_update_pos(): pass
  362. @rpc("authority", "call_remote", "unreliable", 0) # Equivalent to @rpc
  363. func fn_default(): pass
  364. .. rst-class:: classref-item-separator
  365. ----
  366. .. _class_@GDScript_annotation_@tool:
  367. .. rst-class:: classref-annotation
  368. **@tool** **(** **)**
  369. Mark the current script as a tool script, allowing it to be loaded and executed by the editor. See :doc:`Running code in the editor <../tutorials/plugins/running_code_in_the_editor>`.
  370. ::
  371. @tool
  372. extends Node
  373. \ **Note:** As annotations describe their subject, the ``@tool`` annotation must be placed before the class definition and inheritance.
  374. .. rst-class:: classref-item-separator
  375. ----
  376. .. _class_@GDScript_annotation_@warning_ignore:
  377. .. rst-class:: classref-annotation
  378. **@warning_ignore** **(** :ref:`String<class_String>` warning, ... **)** |vararg|
  379. Mark the following statement to ignore the specified ``warning``. See :doc:`GDScript warning system <../tutorials/scripting/gdscript/warning_system>`.
  380. ::
  381. func test():
  382. print("hello")
  383. return
  384. @warning_ignore("unreachable_code")
  385. print("unreachable")
  386. .. rst-class:: classref-section-separator
  387. ----
  388. .. rst-class:: classref-descriptions-group
  389. Method Descriptions
  390. -------------------
  391. .. _class_@GDScript_method_Color8:
  392. .. rst-class:: classref-method
  393. :ref:`Color<class_Color>` **Color8** **(** :ref:`int<class_int>` r8, :ref:`int<class_int>` g8, :ref:`int<class_int>` b8, :ref:`int<class_int>` a8=255 **)**
  394. Returns a :ref:`Color<class_Color>` constructed from red (``r8``), green (``g8``), blue (``b8``), and optionally alpha (``a8``) integer channels, each divided by ``255.0`` for their final value.
  395. ::
  396. var red = Color8(255, 0, 0) # Same as Color(1, 0, 0).
  397. var dark_blue = Color8(0, 0, 51) # Same as Color(0, 0, 0.2).
  398. var my_color = Color8(306, 255, 0, 102) # Same as Color(1.2, 1, 0, 0.4).
  399. .. rst-class:: classref-item-separator
  400. ----
  401. .. _class_@GDScript_method_assert:
  402. .. rst-class:: classref-method
  403. void **assert** **(** :ref:`bool<class_bool>` condition, :ref:`String<class_String>` message="" **)**
  404. Asserts that the ``condition`` is ``true``. If the ``condition`` is ``false``, an error is generated. When running from the editor, the running project will also be paused until you resume it. This can be used as a stronger form of :ref:`@GlobalScope.push_error<class_@GlobalScope_method_push_error>` for reporting errors to project developers or add-on users.
  405. An optional ``message`` can be shown in addition to the generic "Assertion failed" message. You can use this to provide additional details about why the assertion failed.
  406. \ **Warning:** For performance reasons, the code inside :ref:`assert<class_@GDScript_method_assert>` is only executed in debug builds or when running the project from the editor. Don't include code that has side effects in an :ref:`assert<class_@GDScript_method_assert>` call. Otherwise, the project will behave differently when exported in release mode.
  407. ::
  408. # Imagine we always want speed to be between 0 and 20.
  409. var speed = -10
  410. assert(speed < 20) # True, the program will continue.
  411. assert(speed >= 0) # False, the program will stop.
  412. assert(speed >= 0 and speed < 20) # You can also combine the two conditional statements in one check.
  413. assert(speed < 20, "the speed limit is 20") # Show a message.
  414. .. rst-class:: classref-item-separator
  415. ----
  416. .. _class_@GDScript_method_char:
  417. .. rst-class:: classref-method
  418. :ref:`String<class_String>` **char** **(** :ref:`int<class_int>` char **)**
  419. Returns a single character (as a :ref:`String<class_String>`) of the given Unicode code point (which is compatible with ASCII code).
  420. ::
  421. a = char(65) # a is "A"
  422. a = char(65 + 32) # a is "a"
  423. a = char(8364) # a is "€"
  424. .. rst-class:: classref-item-separator
  425. ----
  426. .. _class_@GDScript_method_convert:
  427. .. rst-class:: classref-method
  428. :ref:`Variant<class_Variant>` **convert** **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` type **)**
  429. Converts ``what`` to ``type`` in the best way possible. The ``type`` uses the :ref:`Variant.Type<enum_@GlobalScope_Variant.Type>` values.
  430. ::
  431. var a = [4, 2.5, 1.2]
  432. print(a is Array) # Prints true
  433. var b = convert(a, TYPE_PACKED_BYTE_ARRAY)
  434. print(b) # Prints [4, 2, 1]
  435. print(b is Array) # Prints false
  436. .. rst-class:: classref-item-separator
  437. ----
  438. .. _class_@GDScript_method_dict_to_inst:
  439. .. rst-class:: classref-method
  440. :ref:`Object<class_Object>` **dict_to_inst** **(** :ref:`Dictionary<class_Dictionary>` dictionary **)**
  441. Converts a ``dictionary`` (created with :ref:`inst_to_dict<class_@GDScript_method_inst_to_dict>`) back to an Object instance. Can be useful for deserializing.
  442. .. rst-class:: classref-item-separator
  443. ----
  444. .. _class_@GDScript_method_get_stack:
  445. .. rst-class:: classref-method
  446. :ref:`Array<class_Array>` **get_stack** **(** **)**
  447. Returns an array of dictionaries representing the current call stack. See also :ref:`print_stack<class_@GDScript_method_print_stack>`.
  448. ::
  449. func _ready():
  450. foo()
  451. func foo():
  452. bar()
  453. func bar():
  454. print(get_stack())
  455. Starting from ``_ready()``, ``bar()`` would print:
  456. ::
  457. [{function:bar, line:12, source:res://script.gd}, {function:foo, line:9, source:res://script.gd}, {function:_ready, line:6, source:res://script.gd}]
  458. \ **Note:** This function only works if the running instance is connected to a debugging server (i.e. an editor instance). :ref:`get_stack<class_@GDScript_method_get_stack>` will not work in projects exported in release mode, or in projects exported in debug mode if not connected to a debugging server.
  459. \ **Note:** Calling this function from a :ref:`Thread<class_Thread>` is not supported. Doing so will return an empty array.
  460. .. rst-class:: classref-item-separator
  461. ----
  462. .. _class_@GDScript_method_inst_to_dict:
  463. .. rst-class:: classref-method
  464. :ref:`Dictionary<class_Dictionary>` **inst_to_dict** **(** :ref:`Object<class_Object>` instance **)**
  465. Returns the passed ``instance`` converted to a Dictionary. Can be useful for serializing.
  466. \ **Note:** Cannot be used to serialize objects with built-in scripts attached or objects allocated within built-in scripts.
  467. ::
  468. var foo = "bar"
  469. func _ready():
  470. var d = inst_to_dict(self)
  471. print(d.keys())
  472. print(d.values())
  473. Prints out:
  474. ::
  475. [@subpath, @path, foo]
  476. [, res://test.gd, bar]
  477. .. rst-class:: classref-item-separator
  478. ----
  479. .. _class_@GDScript_method_len:
  480. .. rst-class:: classref-method
  481. :ref:`int<class_int>` **len** **(** :ref:`Variant<class_Variant>` var **)**
  482. Returns the length of the given Variant ``var``. The length can be the character count of a :ref:`String<class_String>`, the element count of any array type or the size of a :ref:`Dictionary<class_Dictionary>`. For every other Variant type, a run-time error is generated and execution is stopped.
  483. ::
  484. a = [1, 2, 3, 4]
  485. len(a) # Returns 4
  486. b = "Hello!"
  487. len(b) # Returns 6
  488. .. rst-class:: classref-item-separator
  489. ----
  490. .. _class_@GDScript_method_load:
  491. .. rst-class:: classref-method
  492. :ref:`Resource<class_Resource>` **load** **(** :ref:`String<class_String>` path **)**
  493. Returns a :ref:`Resource<class_Resource>` from the filesystem located at the absolute ``path``. Unless it's already referenced elsewhere (such as in another script or in the scene), the resource is loaded from disk on function call, which might cause a slight delay, especially when loading large scenes. To avoid unnecessary delays when loading something multiple times, either store the resource in a variable or use :ref:`preload<class_@GDScript_method_preload>`.
  494. \ **Note:** Resource paths can be obtained by right-clicking on a resource in the FileSystem dock and choosing "Copy Path", or by dragging the file from the FileSystem dock into the current script.
  495. ::
  496. # Load a scene called "main" located in the root of the project directory and cache it in a variable.
  497. var main = load("res://main.tscn") # main will contain a PackedScene resource.
  498. \ **Important:** The path must be absolute. A relative path will always return ``null``.
  499. This function is a simplified version of :ref:`ResourceLoader.load<class_ResourceLoader_method_load>`, which can be used for more advanced scenarios.
  500. \ **Note:** Files have to be imported into the engine first to load them using this function. If you want to load :ref:`Image<class_Image>`\ s at run-time, you may use :ref:`Image.load<class_Image_method_load>`. If you want to import audio files, you can use the snippet described in :ref:`AudioStreamMP3.data<class_AudioStreamMP3_property_data>`.
  501. .. rst-class:: classref-item-separator
  502. ----
  503. .. _class_@GDScript_method_preload:
  504. .. rst-class:: classref-method
  505. :ref:`Resource<class_Resource>` **preload** **(** :ref:`String<class_String>` path **)**
  506. Returns a :ref:`Resource<class_Resource>` from the filesystem located at ``path``. During run-time, the resource is loaded when the script is being parsed. This function effectively acts as a reference to that resource. Note that this function requires ``path`` to be a constant :ref:`String<class_String>`. If you want to load a resource from a dynamic/variable path, use :ref:`load<class_@GDScript_method_load>`.
  507. \ **Note:** Resource paths can be obtained by right-clicking on a resource in the Assets Panel and choosing "Copy Path", or by dragging the file from the FileSystem dock into the current script.
  508. ::
  509. # Create instance of a scene.
  510. var diamond = preload("res://diamond.tscn").instantiate()
  511. .. rst-class:: classref-item-separator
  512. ----
  513. .. _class_@GDScript_method_print_debug:
  514. .. rst-class:: classref-method
  515. void **print_debug** **(** ... **)** |vararg|
  516. Like :ref:`@GlobalScope.print<class_@GlobalScope_method_print>`, but includes the current stack frame when running with the debugger turned on.
  517. The output in the console may look like the following:
  518. ::
  519. Test print
  520. At: res://test.gd:15:_process()
  521. \ **Note:** Calling this function from a :ref:`Thread<class_Thread>` is not supported. Doing so will instead print the thread ID.
  522. .. rst-class:: classref-item-separator
  523. ----
  524. .. _class_@GDScript_method_print_stack:
  525. .. rst-class:: classref-method
  526. void **print_stack** **(** **)**
  527. Prints a stack trace at the current code location. See also :ref:`get_stack<class_@GDScript_method_get_stack>`.
  528. The output in the console may look like the following:
  529. ::
  530. Frame 0 - res://test.gd:16 in function '_process'
  531. \ **Note:** This function only works if the running instance is connected to a debugging server (i.e. an editor instance). :ref:`print_stack<class_@GDScript_method_print_stack>` will not work in projects exported in release mode, or in projects exported in debug mode if not connected to a debugging server.
  532. \ **Note:** Calling this function from a :ref:`Thread<class_Thread>` is not supported. Doing so will instead print the thread ID.
  533. .. rst-class:: classref-item-separator
  534. ----
  535. .. _class_@GDScript_method_range:
  536. .. rst-class:: classref-method
  537. :ref:`Array<class_Array>` **range** **(** ... **)** |vararg|
  538. Returns an array with the given range. :ref:`range<class_@GDScript_method_range>` can be called in three ways:
  539. \ ``range(n: int)``: Starts from 0, increases by steps of 1, and stops *before* ``n``. The argument ``n`` is **exclusive**.
  540. \ ``range(b: int, n: int)``: Starts from ``b``, increases by steps of 1, and stops *before* ``n``. The arguments ``b`` and ``n`` are **inclusive** and **exclusive**, respectively.
  541. \ ``range(b: int, n: int, s: int)``: Starts from ``b``, increases/decreases by steps of ``s``, and stops *before* ``n``. The arguments ``b`` and ``n`` are **inclusive** and **exclusive**, respectively. The argument ``s`` **can** be negative, but not ``0``. If ``s`` is ``0``, an error message is printed.
  542. \ :ref:`range<class_@GDScript_method_range>` converts all arguments to :ref:`int<class_int>` before processing.
  543. \ **Note:** Returns an empty array if no value meets the value constraint (e.g. ``range(2, 5, -1)`` or ``range(5, 5, 1)``).
  544. Examples:
  545. ::
  546. print(range(4)) # Prints [0, 1, 2, 3]
  547. print(range(2, 5)) # Prints [2, 3, 4]
  548. print(range(0, 6, 2)) # Prints [0, 2, 4]
  549. print(range(4, 1, -1)) # Prints [4, 3, 2]
  550. To iterate over an :ref:`Array<class_Array>` backwards, use:
  551. ::
  552. var array = [3, 6, 9]
  553. for i in range(array.size(), 0, -1):
  554. print(array[i - 1])
  555. Output:
  556. ::
  557. 9
  558. 6
  559. 3
  560. To iterate over :ref:`float<class_float>`, convert them in the loop.
  561. ::
  562. for i in range (3, 0, -1):
  563. print(i / 10.0)
  564. Output:
  565. ::
  566. 0.3
  567. 0.2
  568. 0.1
  569. .. rst-class:: classref-item-separator
  570. ----
  571. .. _class_@GDScript_method_type_exists:
  572. .. rst-class:: classref-method
  573. :ref:`bool<class_bool>` **type_exists** **(** :ref:`StringName<class_StringName>` type **)**
  574. Returns ``true`` if the given :ref:`Object<class_Object>`-derived class exists in :ref:`ClassDB<class_ClassDB>`. Note that :ref:`Variant<class_Variant>` data types are not registered in :ref:`ClassDB<class_ClassDB>`.
  575. ::
  576. type_exists("Sprite2D") # Returns true
  577. type_exists("NonExistentClass") # Returns false
  578. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  579. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  580. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  581. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  582. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  583. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`