gdscript_exports.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. .. _doc_gdscript_exports:
  2. GDScript exports
  3. ================
  4. Introduction to exports
  5. -----------------------
  6. In Godot, class members can be exported. This means their value gets saved along
  7. with the resource (such as the :ref:`scene <class_PackedScene>`) they're
  8. attached to. They will also be available for editing in the property editor.
  9. Exporting is done by using the ``export`` keyword::
  10. extends Button
  11. export var number = 5 # Value will be saved and visible in the property editor.
  12. An exported variable must be initialized to a constant expression or have an
  13. export hint in the form of an argument to the ``export`` keyword (see the
  14. *Examples* section below).
  15. One of the fundamental benefits of exporting member variables is to have
  16. them visible and editable in the editor. This way, artists and game designers
  17. can modify values that later influence how the program runs. For this, a
  18. special export syntax is provided.
  19. .. note::
  20. Exporting properties can also be done in other languages such as C#.
  21. The syntax varies depending on the language.
  22. ..
  23. See ref `doc_c_sharp_exports` for information on C# exports.
  24. Examples
  25. --------
  26. ::
  27. # If the exported value assigns a constant or constant expression,
  28. # the type will be inferred and used in the editor.
  29. export var number = 5
  30. # Export can take a basic data type as an argument, which will be
  31. # used in the editor.
  32. export(int) var number
  33. # Export can also take a resource type to use as a hint.
  34. export(Texture) var character_face
  35. export(PackedScene) var scene_file
  36. # There are many resource types that can be used this way, try e.g.
  37. # the following to list them:
  38. export(Resource) var resource
  39. # Integers and strings hint enumerated values.
  40. # Editor will enumerate as 0, 1 and 2.
  41. export(int, "Warrior", "Magician", "Thief") var character_class
  42. # Editor will enumerate with string names.
  43. export(String, "Rebecca", "Mary", "Leah") var character_name
  44. # Named enum values
  45. # Editor will enumerate as THING_1, THING_2, ANOTHER_THING.
  46. enum NamedEnum {THING_1, THING_2, ANOTHER_THING = -1}
  47. export(NamedEnum) var x
  48. # Strings as paths
  49. # String is a path to a file.
  50. export(String, FILE) var f
  51. # String is a path to a directory.
  52. export(String, DIR) var f
  53. # String is a path to a file, custom filter provided as hint.
  54. export(String, FILE, "*.txt") var f
  55. # Using paths in the global filesystem is also possible,
  56. # but only in scripts in "tool" mode.
  57. # String is a path to a PNG file in the global filesystem.
  58. export(String, FILE, GLOBAL, "*.png") var tool_image
  59. # String is a path to a directory in the global filesystem.
  60. export(String, DIR, GLOBAL) var tool_dir
  61. # The MULTILINE setting tells the editor to show a large input
  62. # field for editing over multiple lines.
  63. export(String, MULTILINE) var text
  64. # Limiting editor input ranges
  65. # Allow integer values from 0 to 20.
  66. export(int, 20) var i
  67. # Allow integer values from -10 to 20.
  68. export(int, -10, 20) var j
  69. # Allow floats from -10 to 20 and snap the value to multiples of 0.2.
  70. export(float, -10, 20, 0.2) var k
  71. # Allow values 'y = exp(x)' where 'y' varies between 100 and 1000
  72. # while snapping to steps of 20. The editor will present a
  73. # slider for easily editing the value.
  74. export(float, EXP, 100, 1000, 20) var l
  75. # Floats with easing hint
  76. # Display a visual representation of the 'ease()' function
  77. # when editing.
  78. export(float, EASE) var transition_speed
  79. # Colors
  80. # Color given as red-green-blue value (alpha will always be 1).
  81. export(Color, RGB) var col
  82. # Color given as red-green-blue-alpha value.
  83. export(Color, RGBA) var col
  84. # Nodes
  85. # Another node in the scene can be exported as a NodePath.
  86. export(NodePath) var node_path
  87. # Do take note that the node itself isn't being exported -
  88. # there is one more step to call the true node:
  89. onready var node = get_node(node_path)
  90. # Resources
  91. export(Resource) var resource
  92. # In the Inspector, you can then drag and drop a resource file
  93. # from the FileSystem dock into the variable slot.
  94. # Opening the inspector dropdown may result in an
  95. # extremely long list of possible classes to create, however.
  96. # Therefore, if you specify an extension of Resource such as:
  97. export(AnimationNode) var resource
  98. # The drop-down menu will be limited to AnimationNode and all
  99. # its inherited classes.
  100. It must be noted that even if the script is not being run while in the
  101. editor, the exported properties are still editable. This can be used
  102. in conjunction with a :ref:`script in "tool" mode <doc_gdscript_tool_mode>`.
  103. Exporting bit flags
  104. -------------------
  105. Integers used as bit flags can store multiple ``true``/``false`` (boolean)
  106. values in one property. By using the export hint ``int, FLAGS, ...``, they
  107. can be set from the editor::
  108. # Set any of the given flags from the editor.
  109. export(int, FLAGS, "Fire", "Water", "Earth", "Wind") var spell_elements = 0
  110. You must provide a string description for each flag. In this example, ``Fire``
  111. has value 1, ``Water`` has value 2, ``Earth`` has value 4 and ``Wind``
  112. corresponds to value 8. Usually, constants should be defined accordingly (e.g.
  113. ``const ELEMENT_WIND = 8`` and so on).
  114. Export hints are also provided for the physics and render layers defined in the project settings::
  115. export(int, LAYERS_2D_PHYSICS) var layers_2d_physics
  116. export(int, LAYERS_2D_RENDER) var layers_2d_render
  117. export(int, LAYERS_3D_PHYSICS) var layers_3d_physics
  118. export(int, LAYERS_3D_RENDER) var layers_3d_render
  119. Using bit flags requires some understanding of bitwise operations.
  120. If in doubt, use boolean variables instead.
  121. Exporting arrays
  122. ----------------
  123. Exported arrays can have initializers, but they must be constant expressions.
  124. If the exported array specifies a type which inherits from Resource, the array
  125. values can be set in the inspector by dragging and dropping multiple files
  126. from the FileSystem dock at once.
  127. ::
  128. # Default value must be a constant expression.
  129. export var a = [1, 2, 3]
  130. # Exported arrays can specify type (using the same hints as before).
  131. export(Array, int) var ints = [1, 2, 3]
  132. export(Array, int, "Red", "Green", "Blue") var enums = [2, 1, 0]
  133. export(Array, Array, float) var two_dimensional = [[1.0, 2.0], [3.0, 4.0]]
  134. # You can omit the default value, but then it would be null if not assigned.
  135. export(Array) var b
  136. export(Array, PackedScene) var scenes
  137. # Arrays with specified types which inherit from resource can be set by
  138. # drag-and-dropping multiple files from the FileSystem dock.
  139. export(Array, Texture) var textures
  140. export(Array, PackedScene) var scenes
  141. # Typed arrays also work, only initialized empty:
  142. export var vector3s = PoolVector3Array()
  143. export var strings = PoolStringArray()
  144. # Default value can include run-time values, but can't
  145. # be exported.
  146. var c = [a, 2, 3]
  147. Setting exported variables from a tool script
  148. ---------------------------------------------
  149. When changing an exported variable's value from a script in
  150. :ref:`doc_gdscript_tool_mode`, the value in the inspector won't be updated
  151. automatically. To update it, call
  152. :ref:`property_list_changed_notify() <class_Object_method_property_list_changed_notify>`
  153. after setting the exported variable's value.
  154. Advanced exports
  155. ----------------
  156. Not every type of export can be provided on the level of the language itself to
  157. avoid unnecessary design complexity. The following describes some more or less
  158. common exporting features which can be implemented with a low-level API.
  159. Before reading further, you should get familiar with the way properties are
  160. handled and how they can be customized with
  161. :ref:`_set() <class_Object_method__get_property_list>`,
  162. :ref:`_get() <class_Object_method__get_property_list>`, and
  163. :ref:`_get_property_list() <class_Object_method__get_property_list>` methods as
  164. described in :ref:`doc_accessing_data_or_logic_from_object`.
  165. .. seealso:: For binding properties using the above methods in C++, see
  166. :ref:`doc_binding_properties_using_set_get_property_list`.
  167. .. warning:: The script must operate in the ``tool`` mode so the above methods
  168. can work from within the editor.
  169. Properties
  170. ~~~~~~~~~~
  171. To understand how to better use the sections below, you should understand
  172. how to make properties with advanced exports.
  173. ::
  174. func _get_property_list():
  175. var properties = []
  176. # Same as "export(int) var my_property"
  177. properties.append({
  178. name = "my_property",
  179. type = TYPE_INT
  180. })
  181. return properties
  182. * The ``_get_property_list()`` function gets called by the inspector. You
  183. can override it for more advanced exports. You must return an ``Array``
  184. with the contents of the properties for the function to work.
  185. * ``name`` is the name of the property
  186. * ``type`` is the type of the property from ``Variant.Type``.
  187. .. note:: The ``float`` type is called a real (``TYPE_REAL``) in the ``Variant.Type`` enum.
  188. Attaching variables to properties
  189. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  190. To attach variables to properties (allowing the value of the property to be used
  191. in scripts), you need to create a variable with the exact same name as the
  192. property or else you may need to override the
  193. :ref:`_set() <class_Object_method__get_property_list>` and
  194. :ref:`_get() <class_Object_method__get_property_list>` methods. Attaching
  195. a variable to to a property also gives you the ability to give it a default state.
  196. ::
  197. # This variable is determined by the function below.
  198. # This variable acts just like a regular gdscript export.
  199. var my_property = 5
  200. func _get_property_list():
  201. var properties = []
  202. # Same as "export(int) var my_property"
  203. properties.append({
  204. name = "my_property",
  205. type = TYPE_INT
  206. })
  207. return properties
  208. Adding script categories
  209. ~~~~~~~~~~~~~~~~~~~~~~~~
  210. For better visual distinguishing of properties, a special script category can be
  211. embedded into the inspector to act as a separator. ``Script Variables`` is one
  212. example of a built-in category.
  213. ::
  214. func _get_property_list():
  215. var properties = []
  216. properties.append({
  217. name = "Debug",
  218. type = TYPE_NIL,
  219. usage = PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_SCRIPT_VARIABLE
  220. })
  221. # Example of adding a property to the script category
  222. properties.append({
  223. name = "Logging_Enabled",
  224. type = TYPE_BOOL
  225. })
  226. return properties
  227. * ``name`` is the name of a category to be added to the inspector;
  228. * Every following property added after the category definition will be a part
  229. of the category.
  230. * ``PROPERTY_USAGE_CATEGORY`` indicates that the property should be treated as a
  231. script category specifically, so the type ``TYPE_NIL`` can be ignored as it
  232. won't be actually used for the scripting logic, yet it must be defined anyway.
  233. Grouping properties
  234. ~~~~~~~~~~~~~~~~~~~
  235. A list of properties with similar names can be grouped.
  236. ::
  237. func _get_property_list():
  238. var properties = []
  239. properties.append({
  240. name = "Rotate",
  241. type = TYPE_NIL,
  242. hint_string = "rotate_",
  243. usage = PROPERTY_USAGE_GROUP | PROPERTY_USAGE_SCRIPT_VARIABLE
  244. })
  245. # Example of adding to the group
  246. properties.append({
  247. name = "rotate_speed",
  248. type = TYPE_REAL
  249. })
  250. # This property won't get added to the group
  251. # due to not having the "rotate_" prefix.
  252. properties.append({
  253. name = "trail_color",
  254. type = TYPE_COLOR
  255. })
  256. return properties
  257. * ``name`` is the name of a group which is going to be displayed as collapsible
  258. list of properties;
  259. * Every following property added after the group property with the prefix
  260. (which determined by ``hint_string``) will be shortened. For instance,
  261. ``rotate_speed`` is going to be shortened to ``speed`` in this case.
  262. However, ``movement_speed`` won't be a part of the group and will not
  263. be shortened.
  264. * ``PROPERTY_USAGE_GROUP`` indicates that the property should be treated as a
  265. script group specifically, so the type ``TYPE_NIL`` can be ignored as it
  266. won't be actually used for the scripting logic, yet it must be defined anyway.