Skip to content

Commit ee6a25d

Browse files
committed
Document reading an exported property value's early on in GDScript exports
1 parent 5e68807 commit ee6a25d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

tutorials/scripting/gdscript/gdscript_exports.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,38 @@ automatically. To update it, call
542542
:ref:`notify_property_list_changed() <class_Object_method_notify_property_list_changed>`
543543
after setting the exported variable's value.
544544

545+
Reading an exported variable's value early on
546+
---------------------------------------------
547+
548+
If you read an exported variable's value in :ref:`_init() <class_Object_private_method__init>`,
549+
it will return the default value specified in the export annotation instead of the value
550+
that was set in the inspector. This is because assigning values from the saved scene/resource
551+
file occurs *after* object initialization; until then, the default value is used.
552+
553+
To get the value that was set in the inspector (and therefore saved in the scene/resource file),
554+
you need to read it *after* the object is constructed, such as in
555+
:ref:`Node._ready() <class_Node_method_ready>`. You can also read the value
556+
in a setter that's defined on the exported property, which is useful in
557+
custom resources where ``_ready()`` is not available:
558+
559+
::
560+
561+
# Set this property to 3 in the inspector.
562+
@export var exported_variable = 2:
563+
set(value):
564+
exported_variable = value
565+
print("Inspector-set value: ", exported_variable)
566+
567+
func _init():
568+
print("Initial value: ", exported_variable)
569+
570+
Results in:
571+
572+
.. code-block:: none
573+
574+
Initial value: 2
575+
Inspector-set value: 3
576+
545577
Advanced exports
546578
----------------
547579

0 commit comments

Comments
 (0)