Skip to content

Commit 26421fa

Browse files
authored
Merge pull request #9993 from Calinou/gdscript-exports-export-custom
Document `@export_custom` and additional range hints in GDScript exports
2 parents e06a814 + 4c3020b commit 26421fa

File tree

2 files changed

+101
-9
lines changed

2 files changed

+101
-9
lines changed

tutorials/scripting/c_sharp/c_sharp_exports.rst

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Properties with a backing field use the default value of the backing field.
108108
node with an attached tool script, ``_number`` will be ``2``, and
109109
``NumberWithBackingField`` will return ``5``. This difference may cause
110110
confusing behavior. To avoid this, don't use complex properties. Alternatively,
111-
if the default value can be explicitly specified, it can be overridden with the
111+
if the default value can be explicitly specified, it can be overridden with the
112112
:ref:`_PropertyCanRevert() <class_Object_private_method__property_can_revert>` and
113113
:ref:`_PropertyGetRevert() <class_Object_private_method__property_get_revert>` methods.
114114

@@ -259,14 +259,30 @@ the slider.
259259
Floats with easing hint
260260
-----------------------
261261

262-
Display a visual representation of the 'ease()' function
263-
when editing.
262+
Display a visual representation of the :ref:`ease<class_@GlobalScope_method_ease>`
263+
function when editing.
264264

265265
.. code-block:: csharp
266266
267267
[Export(PropertyHint.ExpEasing)]
268268
public float TransitionSpeed { get; set; }
269269
270+
Export with suffix hint
271+
-----------------------
272+
273+
Display a unit hint suffix for exported variables. Works with numeric types,
274+
such as floats or vectors:
275+
276+
.. code-block:: csharp
277+
278+
[Export(PropertyHint.None, "suffix:m/s\u00b2")]
279+
public float Gravity { get; set; } = 9.8f;
280+
[Export(PropertyHint.None, "suffix:m/s")]
281+
public Vector3 Velocity { get; set; }
282+
283+
In the above example, ``\u00b2`` is used to write the "squared" character
284+
(``²``).
285+
270286
Colors
271287
------
272288

tutorials/scripting/gdscript/gdscript_exports.rst

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Resources and nodes can be exported.
5252
@export var resource: Resource
5353
@export var node: Node
5454

55-
Grouping Exports
55+
Grouping exports
5656
----------------
5757

5858
It is possible to group your exported properties inside the Inspector
@@ -160,18 +160,68 @@ Allow floats from -10 to 20 and snap the value to multiples of 0.2.
160160

161161
@export_range(-10, 20, 0.2) var k: float
162162

163-
The limits can be only for the slider if you add the hints "or_greater" and/or "or_less".
163+
The limits can be made to affect only the slider if you add the hints ``"or_less"``
164+
and/or ``"or_greater"``. If either these hints are used, it will be possible for
165+
the user to enter any value or drag the value with the mouse when not using
166+
the slider, even if outside the specified range.
164167

165168
::
166169

167-
@export_range(0, 100, 0.1, "or_greater", "or_less") var l
170+
@export_range(0, 100, 1, "or_less", "or_greater") var l: int
168171

169-
.. TODO: Document other hint strings usable with export_range.
172+
The ``"exp"`` hint can be used to make a value have an exponential slider
173+
instead of a linear slider. This means that when dragging the slider towards
174+
the right, changes will become progressively faster when dragging the mouse.
175+
This is useful to make editing values that can be either very small or very large
176+
easier, at the cost of being less intuitive.
177+
178+
::
179+
180+
@export_range(0, 100000, 0.01, "exp") var exponential: float
181+
182+
For values that are meant to represent an easing factor, use
183+
:ref:`doc_gdscript_exports_floats_with_easing_hint` instead.
184+
185+
The ``"hide_slider"`` hint can be used to hide the horizontal bar that
186+
appears below ``float`` properties, or the up/down arrows that appear besides
187+
``int`` properties:
188+
189+
::
190+
191+
@export_range(0, 1000, 0.01, "hide_slider") var no_slider: float
192+
193+
Adding suffixes and handling degrees/radians
194+
--------------------------------------------
195+
196+
A suffix can also be defined to make the value more self-explanatory in the
197+
inspector. For example, to define a value that is meant to be configured as
198+
"meters" (``m``) by the user:
199+
200+
::
201+
202+
@export_range(0, 100, 1, "suffix:m") var m: int
203+
204+
For angles that are stored in radians but displayed as degrees to the user, use
205+
the `"radians_as_degrees"` hint:
206+
207+
::
208+
209+
@export_range(0, 360, 0.1, "radians_as_degrees") var angle: float
210+
211+
This performs automatic conversion when the value is displayed or modified in
212+
the inspector and also displays a degree (``°``) suffix. This approach is used
213+
by Godot's own `rotation` properties throughout the editor.
214+
215+
If the angle is stored in degrees instead, use the `"degrees"` hint to display
216+
the degree symbol while disabling the automatic degrees-to-radians conversion
217+
when the value is modified from the inspector.
218+
219+
.. _doc_gdscript_exports_floats_with_easing_hint:
170220

171221
Floats with easing hint
172222
-----------------------
173223

174-
Display a visual representation of the 'ease()' function
224+
Display a visual representation of the ``ease()`` function
175225
when editing.
176226

177227
::
@@ -372,7 +422,7 @@ Other export variants can also be used when exporting arrays:
372422

373423
::
374424

375-
@export_range(-360, 360, 0.001, "radians") var laser_angles: Array[float] = []
425+
@export_range(-360, 360, 0.001, "degrees") var laser_angles: Array[float] = []
376426
@export_file("*.json") var skill_trees: Array[String] = []
377427
@export_color_no_alpha var hair_colors = PackedColorArray()
378428
@export_enum("Espresso", "Mocha", "Latte", "Capuccino") var barista_suggestions: Array[String] = []
@@ -399,6 +449,32 @@ or :ref:`Node.duplicate() <class_Node_method_duplicate>` is called, unlike non-e
399449
@export_storage var b # Stored in the file, not displayed in the editor.
400450
@export var c: int # Stored in the file, displayed in the editor.
401451

452+
``@export_custom``
453+
------------------
454+
455+
If you need more control than what's exposed with the built-in ``@export``
456+
annotations, you can use ``@export_custom`` instead. This allows defining any
457+
property hint, hint string and usage flags, with a syntax similar to the one
458+
used by the editor for built-in nodes.
459+
460+
For example, this exposes the ``altitude`` property with no range limits but a
461+
``m`` (meter) suffix defined:
462+
463+
::
464+
465+
@export_custom(PROPERTY_HINT_NONE, "altitude:m") var altitude: Vector3
466+
467+
The above is normally not feasible with the standard ``@export_range`` syntax,
468+
since it requires defining a range.
469+
470+
See the :ref:`class reference <class_@GDScript_annotation_@export_custom>`
471+
for a list of parameters and their allowed values.
472+
473+
.. warning::
474+
475+
When using ``@export_custom``, GDScript does not perform any validation on
476+
the syntax. Invalid syntax may have unexpected behavior in the inspector.
477+
402478
Setting exported variables from a tool script
403479
---------------------------------------------
404480

0 commit comments

Comments
 (0)