Skip to content

Commit 15f32c5

Browse files
committed
GDScript: Add mention that @onready @export doesn't work as expected
1 parent b7b25ba commit 15f32c5

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

tutorials/scripting/gdscript/gdscript_basics.rst

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -404,11 +404,11 @@ between parentheses and separated by commas.
404404

405405
Both of these are the same::
406406

407-
@onready
408-
@export_node_path("TextEdit", "LineEdit")
409-
var input_field
407+
@annotation_a
408+
@annotation_b
409+
var variable
410410

411-
@onready @export_node_path("TextEdit", "LineEdit") var input_field
411+
@annotation_a @annotation_b var variable
412412

413413
.. _doc_gdscript_onready_annotation:
414414

@@ -435,6 +435,29 @@ can replace the above code with a single line::
435435

436436
@onready var my_label = get_node("MyLabel")
437437

438+
.. warning::
439+
440+
Applying ``@onready`` and any ``@export`` annotation to the same variable
441+
doesn't work as you might expect. The ``@onready`` annotation will cause
442+
the default value to be set after the ``@export`` takes effect and will
443+
override it::
444+
445+
@export var a = "init_value_a"
446+
@onready @export var b = "init_value_b"
447+
448+
func _init():
449+
prints(a, b) # init_value_a <null>
450+
451+
func _notification(what):
452+
if what == NOTIFICATION_SCENE_INSTANTIATED:
453+
prints(a, b) # exported_value_a exported_value_b
454+
455+
func _ready():
456+
prints(a, b) # exported_value_a init_value_b
457+
458+
Therefore, the ``ONREADY_WITH_EXPORT`` warning is generated, which is treated
459+
as an error by default. We do not recommend disabling or ignoring it.
460+
438461
Comments
439462
~~~~~~~~
440463

@@ -911,7 +934,7 @@ want to assign consecutive integers to some constant.
911934
::
912935

913936
enum {TILE_BRICK, TILE_FLOOR, TILE_SPIKE, TILE_TELEPORT}
914-
937+
915938
# Is the same as:
916939
const TILE_BRICK = 0
917940
const TILE_FLOOR = 1
@@ -930,10 +953,10 @@ a dictionary can also be used with a named enum.
930953
::
931954

932955
enum State {STATE_IDLE, STATE_JUMP = 5, STATE_SHOOT}
933-
956+
934957
# Is the same as:
935958
const State = {STATE_IDLE = 0, STATE_JUMP = 5, STATE_SHOOT = 6}
936-
959+
937960
func _ready():
938961
# Access values with Name.KEY, prints '5'
939962
print(State.STATE_JUMP)

tutorials/scripting/gdscript/gdscript_documentation_comments.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ Examples
106106

107107
## If the member has any annotation, the annotation should
108108
## immediately precede it.
109-
@export
110109
@onready
111110
var v3 := some_func()
112111

0 commit comments

Comments
 (0)