From 59d47c6e08308c92e02a681d232ee3bea12cacdd Mon Sep 17 00:00:00 2001 From: Dana Olson Date: Tue, 16 Feb 2016 13:25:08 -0500 Subject: [PATCH] added Onready keyword section taken from old openproject docs --- reference/gdscript.rst | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/reference/gdscript.rst b/reference/gdscript.rst index d9bf4a17..9423cb36 100644 --- a/reference/gdscript.rst +++ b/reference/gdscript.rst @@ -1050,3 +1050,22 @@ signal is activated, execution will return. Here are some examples: #resume execution when animation is done playing: yield( get_node("AnimationPlayer"), "finished" ) + + +Onready Keyword +~~~~~~~~~~~~~~~ + +When using nodes, it's very common to desire to keep references to parts of the scene in a variable. As scenes are only warranted to be configured when entering the active scene tree, the sub-nodes can only be obtained when a call to Node._ready() is made. + +:: + + var mylabel + + func _ready(): + myabel = get_node("MyLabel") + +This can get a little cumbersome, specially when nodes and external references pile up. For this, GDScript has the ``onready`` keyword, that defers initialization of a member variable until _ready is called. It can replace the above code with a single line: + +:: + + onready var mylabel = get_node("MyLabel")