[email protected] 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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 accessed in 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 int_number = 5
  86. @export var float_number: float = 5
  87. .. rst-class:: classref-item-separator
  88. ----
  89. .. _class_@GDScript_annotation_@export_category:
  90. .. rst-class:: classref-annotation
  91. **@export_category** **(** :ref:`String<class_String>` name **)**
  92. Define a new category for the following exported properties. This helps to organize properties in the Inspector dock.
  93. See also :ref:`@GlobalScope.PROPERTY_USAGE_CATEGORY<class_@GlobalScope_constant_PROPERTY_USAGE_CATEGORY>`.
  94. ::
  95. @export_category("My Properties")
  96. @export var number = 3
  97. @export var string = ""
  98. \ **Note:** Categories in the property list are supposed to indicate different base types, so the use of this annotation is not encouraged. See :ref:`@export_group<class_@GDScript_annotation_@export_group>` and :ref:`@export_subgroup<class_@GDScript_annotation_@export_subgroup>` instead.
  99. .. rst-class:: classref-item-separator
  100. ----
  101. .. _class_@GDScript_annotation_@export_color_no_alpha:
  102. .. rst-class:: classref-annotation
  103. **@export_color_no_alpha** **(** **)**
  104. Export a :ref:`Color<class_Color>` property without transparency (its alpha fixed as ``1.0``).
  105. See also :ref:`@GlobalScope.PROPERTY_HINT_COLOR_NO_ALPHA<class_@GlobalScope_constant_PROPERTY_HINT_COLOR_NO_ALPHA>`.
  106. ::
  107. @export_color_no_alpha var modulate_color: Color
  108. .. rst-class:: classref-item-separator
  109. ----
  110. .. _class_@GDScript_annotation_@export_dir:
  111. .. rst-class:: classref-annotation
  112. **@export_dir** **(** **)**
  113. 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.
  114. See also :ref:`@GlobalScope.PROPERTY_HINT_DIR<class_@GlobalScope_constant_PROPERTY_HINT_DIR>`.
  115. ::
  116. @export_dir var sprite_folder: String
  117. .. rst-class:: classref-item-separator
  118. ----
  119. .. _class_@GDScript_annotation_@export_enum:
  120. .. rst-class:: classref-annotation
  121. **@export_enum** **(** :ref:`String<class_String>` names, ... **)** |vararg|
  122. 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.
  123. See also :ref:`@GlobalScope.PROPERTY_HINT_ENUM<class_@GlobalScope_constant_PROPERTY_HINT_ENUM>`.
  124. ::
  125. @export_enum("Warrior", "Magician", "Thief") var character_class: int
  126. @export_enum("Slow:30", "Average:60", "Very Fast:200") var character_speed: int
  127. @export_enum("Rebecca", "Mary", "Leah") var character_name: String
  128. If you want to set an initial value, you must specify it explicitly:
  129. ::
  130. @export_enum("Rebecca", "Mary", "Leah") var character_name: String = "Rebecca"
  131. If you want to use named GDScript enums, then use :ref:`@export<class_@GDScript_annotation_@export>` instead:
  132. ::
  133. enum CharacterName {REBECCA, MARY, LEAH}
  134. @export var character_name: CharacterName
  135. .. rst-class:: classref-item-separator
  136. ----
  137. .. _class_@GDScript_annotation_@export_exp_easing:
  138. .. rst-class:: classref-annotation
  139. **@export_exp_easing** **(** :ref:`String<class_String>` hints="", ... **)** |vararg|
  140. 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.
  141. See also :ref:`@GlobalScope.PROPERTY_HINT_EXP_EASING<class_@GlobalScope_constant_PROPERTY_HINT_EXP_EASING>`.
  142. ::
  143. @export_exp_easing var transition_speed
  144. @export_exp_easing("attenuation") var fading_attenuation
  145. @export_exp_easing("positive_only") var effect_power
  146. .. rst-class:: classref-item-separator
  147. ----
  148. .. _class_@GDScript_annotation_@export_file:
  149. .. rst-class:: classref-annotation
  150. **@export_file** **(** :ref:`String<class_String>` filter="", ... **)** |vararg|
  151. 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.
  152. If ``filter`` is provided, only matching files will be available for picking.
  153. See also :ref:`@GlobalScope.PROPERTY_HINT_FILE<class_@GlobalScope_constant_PROPERTY_HINT_FILE>`.
  154. ::
  155. @export_file var sound_effect_file: String
  156. @export_file("*.txt") var notes_file: String
  157. .. rst-class:: classref-item-separator
  158. ----
  159. .. _class_@GDScript_annotation_@export_flags:
  160. .. rst-class:: classref-annotation
  161. **@export_flags** **(** :ref:`String<class_String>` names, ... **)** |vararg|
  162. 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.
  163. See also :ref:`@GlobalScope.PROPERTY_HINT_FLAGS<class_@GlobalScope_constant_PROPERTY_HINT_FLAGS>`.
  164. ::
  165. @export_flags("Fire", "Water", "Earth", "Wind") var spell_elements = 0
  166. You can add explicit values using a colon:
  167. ::
  168. @export_flags("Self:4", "Allies:8", "Foes:16") var spell_targets = 0
  169. You can also combine several flags:
  170. ::
  171. @export_flags("Self:4", "Allies:8", "Self and Allies:12", "Foes:16")
  172. var spell_targets = 0
  173. \ **Note:** A flag value must be at least ``1`` and at most ``2 ** 32 - 1``.
  174. \ **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.
  175. ::
  176. @export_flags("A:16", "B", "C") var x
  177. .. rst-class:: classref-item-separator
  178. ----
  179. .. _class_@GDScript_annotation_@export_flags_2d_navigation:
  180. .. rst-class:: classref-annotation
  181. **@export_flags_2d_navigation** **(** **)**
  182. 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>`.
  183. See also :ref:`@GlobalScope.PROPERTY_HINT_LAYERS_2D_NAVIGATION<class_@GlobalScope_constant_PROPERTY_HINT_LAYERS_2D_NAVIGATION>`.
  184. ::
  185. @export_flags_2d_navigation var navigation_layers: int
  186. .. rst-class:: classref-item-separator
  187. ----
  188. .. _class_@GDScript_annotation_@export_flags_2d_physics:
  189. .. rst-class:: classref-annotation
  190. **@export_flags_2d_physics** **(** **)**
  191. 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>`.
  192. See also :ref:`@GlobalScope.PROPERTY_HINT_LAYERS_2D_PHYSICS<class_@GlobalScope_constant_PROPERTY_HINT_LAYERS_2D_PHYSICS>`.
  193. ::
  194. @export_flags_2d_physics var physics_layers: int
  195. .. rst-class:: classref-item-separator
  196. ----
  197. .. _class_@GDScript_annotation_@export_flags_2d_render:
  198. .. rst-class:: classref-annotation
  199. **@export_flags_2d_render** **(** **)**
  200. 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>`.
  201. See also :ref:`@GlobalScope.PROPERTY_HINT_LAYERS_2D_RENDER<class_@GlobalScope_constant_PROPERTY_HINT_LAYERS_2D_RENDER>`.
  202. ::
  203. @export_flags_2d_render var render_layers: int
  204. .. rst-class:: classref-item-separator
  205. ----
  206. .. _class_@GDScript_annotation_@export_flags_3d_navigation:
  207. .. rst-class:: classref-annotation
  208. **@export_flags_3d_navigation** **(** **)**
  209. 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>`.
  210. See also :ref:`@GlobalScope.PROPERTY_HINT_LAYERS_3D_NAVIGATION<class_@GlobalScope_constant_PROPERTY_HINT_LAYERS_3D_NAVIGATION>`.
  211. ::
  212. @export_flags_3d_navigation var navigation_layers: int
  213. .. rst-class:: classref-item-separator
  214. ----
  215. .. _class_@GDScript_annotation_@export_flags_3d_physics:
  216. .. rst-class:: classref-annotation
  217. **@export_flags_3d_physics** **(** **)**
  218. 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>`.
  219. See also :ref:`@GlobalScope.PROPERTY_HINT_LAYERS_3D_PHYSICS<class_@GlobalScope_constant_PROPERTY_HINT_LAYERS_3D_PHYSICS>`.
  220. ::
  221. @export_flags_3d_physics var physics_layers: int
  222. .. rst-class:: classref-item-separator
  223. ----
  224. .. _class_@GDScript_annotation_@export_flags_3d_render:
  225. .. rst-class:: classref-annotation
  226. **@export_flags_3d_render** **(** **)**
  227. 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>`.
  228. See also :ref:`@GlobalScope.PROPERTY_HINT_LAYERS_3D_RENDER<class_@GlobalScope_constant_PROPERTY_HINT_LAYERS_3D_RENDER>`.
  229. ::
  230. @export_flags_3d_render var render_layers: int
  231. .. rst-class:: classref-item-separator
  232. ----
  233. .. _class_@GDScript_annotation_@export_global_dir:
  234. .. rst-class:: classref-annotation
  235. **@export_global_dir** **(** **)**
  236. Export a :ref:`String<class_String>` property as a 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.
  237. See also :ref:`@GlobalScope.PROPERTY_HINT_GLOBAL_DIR<class_@GlobalScope_constant_PROPERTY_HINT_GLOBAL_DIR>`.
  238. ::
  239. @export_global_dir var sprite_folder: String
  240. .. rst-class:: classref-item-separator
  241. ----
  242. .. _class_@GDScript_annotation_@export_global_file:
  243. .. rst-class:: classref-annotation
  244. **@export_global_file** **(** :ref:`String<class_String>` filter="", ... **)** |vararg|
  245. Export a :ref:`String<class_String>` property as a 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.
  246. If ``filter`` is provided, only matching files will be available for picking.
  247. See also :ref:`@GlobalScope.PROPERTY_HINT_GLOBAL_FILE<class_@GlobalScope_constant_PROPERTY_HINT_GLOBAL_FILE>`.
  248. ::
  249. @export_global_file var sound_effect_file: String
  250. @export_global_file("*.txt") var notes_file: String
  251. .. rst-class:: classref-item-separator
  252. ----
  253. .. _class_@GDScript_annotation_@export_group:
  254. .. rst-class:: classref-annotation
  255. **@export_group** **(** :ref:`String<class_String>` name, :ref:`String<class_String>` prefix="" **)**
  256. 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.
  257. 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("", "")``.
  258. Groups cannot be nested, use :ref:`@export_subgroup<class_@GDScript_annotation_@export_subgroup>` to add subgroups within groups.
  259. See also :ref:`@GlobalScope.PROPERTY_USAGE_GROUP<class_@GlobalScope_constant_PROPERTY_USAGE_GROUP>`.
  260. ::
  261. @export_group("My Properties")
  262. @export var number = 3
  263. @export var string = ""
  264. @export_group("Prefixed Properties", "prefix_")
  265. @export var prefix_number = 3
  266. @export var prefix_string = ""
  267. @export_group("", "")
  268. @export var ungrouped_number = 3
  269. .. rst-class:: classref-item-separator
  270. ----
  271. .. _class_@GDScript_annotation_@export_multiline:
  272. .. rst-class:: classref-annotation
  273. **@export_multiline** **(** **)**
  274. 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.
  275. See also :ref:`@GlobalScope.PROPERTY_HINT_MULTILINE_TEXT<class_@GlobalScope_constant_PROPERTY_HINT_MULTILINE_TEXT>`.
  276. ::
  277. @export_multiline var character_biography
  278. .. rst-class:: classref-item-separator
  279. ----
  280. .. _class_@GDScript_annotation_@export_node_path:
  281. .. rst-class:: classref-annotation
  282. **@export_node_path** **(** :ref:`String<class_String>` type="", ... **)** |vararg|
  283. Export a :ref:`NodePath<class_NodePath>` property with a filter for allowed node types.
  284. See also :ref:`@GlobalScope.PROPERTY_HINT_NODE_PATH_VALID_TYPES<class_@GlobalScope_constant_PROPERTY_HINT_NODE_PATH_VALID_TYPES>`.
  285. ::
  286. @export_node_path("Button", "TouchScreenButton") var some_button
  287. .. rst-class:: classref-item-separator
  288. ----
  289. .. _class_@GDScript_annotation_@export_placeholder:
  290. .. rst-class:: classref-annotation
  291. **@export_placeholder** **(** :ref:`String<class_String>` placeholder **)**
  292. Export a :ref:`String<class_String>` property with a placeholder text displayed in the editor widget when no value is present.
  293. See also :ref:`@GlobalScope.PROPERTY_HINT_PLACEHOLDER_TEXT<class_@GlobalScope_constant_PROPERTY_HINT_PLACEHOLDER_TEXT>`.
  294. ::
  295. @export_placeholder("Name in lowercase") var character_id: String
  296. .. rst-class:: classref-item-separator
  297. ----
  298. .. _class_@GDScript_annotation_@export_range:
  299. .. rst-class:: classref-annotation
  300. **@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|
  301. 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.
  302. 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.
  303. 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.
  304. See also :ref:`@GlobalScope.PROPERTY_HINT_RANGE<class_@GlobalScope_constant_PROPERTY_HINT_RANGE>`.
  305. ::
  306. @export_range(0, 20) var number
  307. @export_range(-10, 20) var number
  308. @export_range(-10, 20, 0.2) var number: float
  309. @export_range(0, 100, 1, "or_greater") var power_percent
  310. @export_range(0, 100, 1, "or_greater", "or_less") var health_delta
  311. @export_range(-3.14, 3.14, 0.001, "radians") var angle_radians
  312. @export_range(0, 360, 1, "degrees") var angle_degrees
  313. @export_range(-8, 8, 2, "suffix:px") var target_offset
  314. .. rst-class:: classref-item-separator
  315. ----
  316. .. _class_@GDScript_annotation_@export_subgroup:
  317. .. rst-class:: classref-annotation
  318. **@export_subgroup** **(** :ref:`String<class_String>` name, :ref:`String<class_String>` prefix="" **)**
  319. 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>`.
  320. See also :ref:`@GlobalScope.PROPERTY_USAGE_SUBGROUP<class_@GlobalScope_constant_PROPERTY_USAGE_SUBGROUP>`.
  321. ::
  322. @export_group("My Properties")
  323. @export var number = 3
  324. @export var string = ""
  325. @export_subgroup("My Prefixed Properties", "prefix_")
  326. @export var prefix_number = 3
  327. @export var prefix_string = ""
  328. \ **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.
  329. .. rst-class:: classref-item-separator
  330. ----
  331. .. _class_@GDScript_annotation_@icon:
  332. .. rst-class:: classref-annotation
  333. **@icon** **(** :ref:`String<class_String>` icon_path **)**
  334. 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.
  335. ::
  336. @icon("res://path/to/class/icon.svg")
  337. \ **Note:** Only the script can have a custom icon. Inner classes are not supported.
  338. \ **Note:** As annotations describe their subject, the ``@icon`` annotation must be placed before the class definition and inheritance.
  339. \ **Note:** Unlike other annotations, the argument of the ``@icon`` annotation must be a string literal (constant expressions are not supported).
  340. .. rst-class:: classref-item-separator
  341. ----
  342. .. _class_@GDScript_annotation_@onready:
  343. .. rst-class:: classref-annotation
  344. **@onready** **(** **)**
  345. Mark the following property as assigned on :ref:`Node<class_Node>`'s ready state change. Values for these properties are not assigned immediately upon the node's creation, and instead are computed and stored right before :ref:`Node._ready<class_Node_method__ready>`.
  346. ::
  347. @onready var character_name: Label = $Label
  348. .. rst-class:: classref-item-separator
  349. ----
  350. .. _class_@GDScript_annotation_@rpc:
  351. .. rst-class:: classref-annotation
  352. **@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|
  353. Mark the following method for remote procedure calls. See :doc:`High-level multiplayer <../tutorials/networking/high_level_multiplayer>`.
  354. 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"``.
  355. ::
  356. @rpc
  357. func fn(): pass
  358. @rpc("any_peer", "unreliable_ordered")
  359. func fn_update_pos(): pass
  360. @rpc("authority", "call_remote", "unreliable", 0) # Equivalent to @rpc
  361. func fn_default(): pass
  362. .. rst-class:: classref-item-separator
  363. ----
  364. .. _class_@GDScript_annotation_@tool:
  365. .. rst-class:: classref-annotation
  366. **@tool** **(** **)**
  367. 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>`.
  368. ::
  369. @tool
  370. extends Node
  371. \ **Note:** As annotations describe their subject, the ``@tool`` annotation must be placed before the class definition and inheritance.
  372. .. rst-class:: classref-item-separator
  373. ----
  374. .. _class_@GDScript_annotation_@warning_ignore:
  375. .. rst-class:: classref-annotation
  376. **@warning_ignore** **(** :ref:`String<class_String>` warning, ... **)** |vararg|
  377. Mark the following statement to ignore the specified ``warning``. See :doc:`GDScript warning system <../tutorials/scripting/gdscript/warning_system>`.
  378. ::
  379. func test():
  380. print("hello")
  381. return
  382. @warning_ignore("unreachable_code")
  383. print("unreachable")
  384. .. rst-class:: classref-section-separator
  385. ----
  386. .. rst-class:: classref-descriptions-group
  387. Method Descriptions
  388. -------------------
  389. .. _class_@GDScript_method_Color8:
  390. .. rst-class:: classref-method
  391. :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 **)**
  392. 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.
  393. ::
  394. var red = Color8(255, 0, 0) # Same as Color(1, 0, 0)
  395. var dark_blue = Color8(0, 0, 51) # Same as Color(0, 0, 0.2).
  396. var my_color = Color8(306, 255, 0, 102) # Same as Color(1.2, 1, 0, 0.4).
  397. .. rst-class:: classref-item-separator
  398. ----
  399. .. _class_@GDScript_method_assert:
  400. .. rst-class:: classref-method
  401. void **assert** **(** :ref:`bool<class_bool>` condition, :ref:`String<class_String>` message="" **)**
  402. 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.
  403. 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.
  404. \ **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.
  405. ::
  406. # Imagine we always want speed to be between 0 and 20.
  407. var speed = -10
  408. assert(speed < 20) # True, the program will continue
  409. assert(speed >= 0) # False, the program will stop
  410. assert(speed >= 0 and speed < 20) # You can also combine the two conditional statements in one check
  411. assert(speed < 20, "the speed limit is 20") # Show a message
  412. .. rst-class:: classref-item-separator
  413. ----
  414. .. _class_@GDScript_method_char:
  415. .. rst-class:: classref-method
  416. :ref:`String<class_String>` **char** **(** :ref:`int<class_int>` char **)**
  417. Returns a single character (as a :ref:`String<class_String>`) of the given Unicode code point (which is compatible with ASCII code).
  418. ::
  419. a = char(65) # a is "A"
  420. a = char(65 + 32) # a is "a"
  421. a = char(8364) # a is "€"
  422. .. rst-class:: classref-item-separator
  423. ----
  424. .. _class_@GDScript_method_convert:
  425. .. rst-class:: classref-method
  426. :ref:`Variant<class_Variant>` **convert** **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` type **)**
  427. Converts ``what`` to ``type`` in the best way possible. The ``type`` uses the :ref:`Variant.Type<enum_@GlobalScope_Variant.Type>` values.
  428. ::
  429. var a = [4, 2.5, 1.2]
  430. print(a is Array) # Prints true
  431. var b = convert(a, TYPE_PACKED_BYTE_ARRAY)
  432. print(b) # Prints [4, 2, 1]
  433. print(b is Array) # Prints false
  434. .. rst-class:: classref-item-separator
  435. ----
  436. .. _class_@GDScript_method_dict_to_inst:
  437. .. rst-class:: classref-method
  438. :ref:`Object<class_Object>` **dict_to_inst** **(** :ref:`Dictionary<class_Dictionary>` dictionary **)**
  439. 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.
  440. .. rst-class:: classref-item-separator
  441. ----
  442. .. _class_@GDScript_method_get_stack:
  443. .. rst-class:: classref-method
  444. :ref:`Array<class_Array>` **get_stack** **(** **)**
  445. Returns an array of dictionaries representing the current call stack. See also :ref:`print_stack<class_@GDScript_method_print_stack>`.
  446. ::
  447. func _ready():
  448. foo()
  449. func foo():
  450. bar()
  451. func bar():
  452. print(get_stack())
  453. Starting from ``_ready()``, ``bar()`` would print:
  454. ::
  455. [{function:bar, line:12, source:res://script.gd}, {function:foo, line:9, source:res://script.gd}, {function:_ready, line:6, source:res://script.gd}]
  456. \ **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.
  457. \ **Note:** Calling this function from a :ref:`Thread<class_Thread>` is not supported. Doing so will return an empty array.
  458. .. rst-class:: classref-item-separator
  459. ----
  460. .. _class_@GDScript_method_inst_to_dict:
  461. .. rst-class:: classref-method
  462. :ref:`Dictionary<class_Dictionary>` **inst_to_dict** **(** :ref:`Object<class_Object>` instance **)**
  463. Returns the passed ``instance`` converted to a Dictionary. Can be useful for serializing.
  464. \ **Note:** Cannot be used to serialize objects with built-in scripts attached or objects allocated within built-in scripts.
  465. ::
  466. var foo = "bar"
  467. func _ready():
  468. var d = inst_to_dict(self)
  469. print(d.keys())
  470. print(d.values())
  471. Prints out:
  472. ::
  473. [@subpath, @path, foo]
  474. [, res://test.gd, bar]
  475. .. rst-class:: classref-item-separator
  476. ----
  477. .. _class_@GDScript_method_len:
  478. .. rst-class:: classref-method
  479. :ref:`int<class_int>` **len** **(** :ref:`Variant<class_Variant>` var **)**
  480. 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.
  481. ::
  482. a = [1, 2, 3, 4]
  483. len(a) # Returns 4
  484. b = "Hello!"
  485. len(b) # Returns 6
  486. .. rst-class:: classref-item-separator
  487. ----
  488. .. _class_@GDScript_method_load:
  489. .. rst-class:: classref-method
  490. :ref:`Resource<class_Resource>` **load** **(** :ref:`String<class_String>` path **)**
  491. 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>`.
  492. \ **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 script.
  493. ::
  494. # Load a scene called "main" located in the root of the project directory and cache it in a variable.
  495. var main = load("res://main.tscn") # main will contain a PackedScene resource.
  496. \ **Important:** The path must be absolute. A relative path will always return ``null``.
  497. This function is a simplified version of :ref:`ResourceLoader.load<class_ResourceLoader_method_load>`, which can be used for more advanced scenarios.
  498. \ **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>`.
  499. .. rst-class:: classref-item-separator
  500. ----
  501. .. _class_@GDScript_method_preload:
  502. .. rst-class:: classref-method
  503. :ref:`Resource<class_Resource>` **preload** **(** :ref:`String<class_String>` path **)**
  504. 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>`.
  505. \ **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 script.
  506. ::
  507. # Create instance of a scene.
  508. var diamond = preload("res://diamond.tscn").instantiate()
  509. .. rst-class:: classref-item-separator
  510. ----
  511. .. _class_@GDScript_method_print_debug:
  512. .. rst-class:: classref-method
  513. void **print_debug** **(** ... **)** |vararg|
  514. Like :ref:`@GlobalScope.print<class_@GlobalScope_method_print>`, but includes the current stack frame when running with the debugger turned on.
  515. The output in the console may look like the following:
  516. ::
  517. Test print
  518. At: res://test.gd:15:_process()
  519. \ **Note:** Calling this function from a :ref:`Thread<class_Thread>` is not supported. Doing so will instead print the thread ID.
  520. .. rst-class:: classref-item-separator
  521. ----
  522. .. _class_@GDScript_method_print_stack:
  523. .. rst-class:: classref-method
  524. void **print_stack** **(** **)**
  525. Prints a stack trace at the current code location. See also :ref:`get_stack<class_@GDScript_method_get_stack>`.
  526. The output in the console may look like the following:
  527. ::
  528. Frame 0 - res://test.gd:16 in function '_process'
  529. \ **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.
  530. \ **Note:** Calling this function from a :ref:`Thread<class_Thread>` is not supported. Doing so will instead print the thread ID.
  531. .. rst-class:: classref-item-separator
  532. ----
  533. .. _class_@GDScript_method_range:
  534. .. rst-class:: classref-method
  535. :ref:`Array<class_Array>` **range** **(** ... **)** |vararg|
  536. Returns an array with the given range. :ref:`range<class_@GDScript_method_range>` can be called in three ways:
  537. \ ``range(n: int)``: Starts from 0, increases by steps of 1, and stops *before* ``n``. The argument ``n`` is **exclusive**.
  538. \ ``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.
  539. \ ``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.
  540. \ :ref:`range<class_@GDScript_method_range>` converts all arguments to :ref:`int<class_int>` before processing.
  541. \ **Note:** Returns an empty array if no value meets the value constraint (e.g. ``range(2, 5, -1)`` or ``range(5, 5, 1)``).
  542. Examples:
  543. ::
  544. print(range(4)) # Prints [0, 1, 2, 3]
  545. print(range(2, 5)) # Prints [2, 3, 4]
  546. print(range(0, 6, 2)) # Prints [0, 2, 4]
  547. print(range(4, 1, -1)) # Prints [4, 3, 2]
  548. To iterate over an :ref:`Array<class_Array>` backwards, use:
  549. ::
  550. var array = [3, 6, 9]
  551. for i in range(array.size(), 0, -1):
  552. print(array[i - 1])
  553. Output:
  554. ::
  555. 9
  556. 6
  557. 3
  558. To iterate over :ref:`float<class_float>`, convert them in the loop.
  559. ::
  560. for i in range (3, 0, -1):
  561. print(i / 10.0)
  562. Output:
  563. ::
  564. 0.3
  565. 0.2
  566. 0.1
  567. .. rst-class:: classref-item-separator
  568. ----
  569. .. _class_@GDScript_method_type_exists:
  570. .. rst-class:: classref-method
  571. :ref:`bool<class_bool>` **type_exists** **(** :ref:`StringName<class_StringName>` type **)**
  572. 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>`.
  573. ::
  574. type_exists("Sprite2D") # Returns true
  575. type_exists("NonExistentClass") # Returns false
  576. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  577. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  578. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  579. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  580. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  581. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`