File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
tutorials/scripting/gdscript Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -542,6 +542,38 @@ automatically. To update it, call
542
542
:ref: `notify_property_list_changed() <class_Object_method_notify_property_list_changed >`
543
543
after setting the exported variable's value.
544
544
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
+
545
577
Advanced exports
546
578
----------------
547
579
You can’t perform that action at this time.
0 commit comments