Skip to content

Commit d82940e

Browse files
authored
Merge pull request godotengine#8293 from michaelgundlach/patch-1
Clarify confusion around property initialization
2 parents 2e38e7c + 79863df commit d82940e

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

tutorials/best_practices/godot_notifications.rst

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -164,28 +164,39 @@ _init vs. initialization vs. export
164164
-----------------------------------
165165

166166
If the script initializes its own node subtree, without a scene,
167-
that code should execute here. Other property or SceneTree-independent
168-
initializations should also run here. This triggers before ``_ready()`` or
169-
``_enter_tree()``, but after a script creates and initializes its properties.
167+
that code should execute in ``_init()``. Other property or SceneTree-independent
168+
initializations should also run here.
170169

171-
Scripts have three types of property assignments that can occur during
172-
instantiation:
170+
.. note::
171+
The C# equivalent to GDScript's ``_init()`` method is the constructor.
172+
173+
``_init()`` triggers before ``_enter_tree()`` or ``_ready()``, but after a script
174+
creates and initializes its properties. When instantiating a scene, property
175+
values will set up according to the following sequence:
176+
177+
1. **Initial value assignment:** the property is assigned its initialization value,
178+
or its default value if one is not specified. If a setter exists, it is not used.
179+
180+
2. **``_init()`` assignment:** the property's value is replaced by any assignments
181+
made in ``_init()``, triggering the setter.
182+
183+
3. **Exported value assignment:** an exported property's value is again replaced by
184+
any value set in the Inspector, triggering the setter.
173185

174186
.. tabs::
175187
.. code-tab:: gdscript GDScript
176188

177-
# "one" is an "initialized value". These DO NOT trigger the setter.
178-
# If someone set the value as "two" from the Inspector, this would be an
179-
# "exported value". These DO trigger the setter.
189+
# test is initialized to "one", without triggering the setter.
180190
@export var test: String = "one":
181191
set(value):
182-
test = value
183-
print("Setting: ", test)
192+
test = value + "!"
184193

185194
func _init():
186-
# "three" is an "init assignment value".
187-
# Trigger the setter
188-
test = "three"
195+
# Triggers the setter, changing test's value from "one" to "two!".
196+
test = "two"
197+
198+
# If someone sets test to "three" from the Inspector, it would trigger
199+
# the setter, changing test's value from "two!" to "three!".
189200

190201
.. code-tab:: csharp
191202

@@ -195,37 +206,24 @@ instantiation:
195206
{
196207
private string _test = "one";
197208

198-
// Changing the value from the inspector does trigger the setter in C#.
199209
[Export]
200210
public string Test
201211
{
202212
get { return _test; }
203-
set
204-
{
205-
_test = value;
206-
GD.Print($"Setting: {_test}");
207-
}
213+
set { _test = $"{value}!"; }
208214
}
209215

210216
public MyNode()
211217
{
212-
// Triggers the setter as well
213-
Test = "three";
218+
// Triggers the setter, changing _test's value from "one" to "two!".
219+
Test = "two";
214220
}
215-
}
216221

217-
When instantiating a scene, property values will set up according to the
218-
following sequence:
219-
220-
1. **Initial value assignment:** instantiation will assign either the
221-
initialization value or the init assignment value. Init assignments take
222-
priority over initialization values.
223-
224-
2. **Exported value assignment:** If instancing from a scene rather than
225-
a script, Godot will assign the exported value to replace the initial
226-
value defined in the script.
222+
// If someone sets Test to "three" in the Inspector, it would trigger
223+
// the setter, changing _test's value from "two!" to "three!".
224+
}
227225

228-
As a result, instantiating a script versus a scene will affect both the
226+
As a result, instantiating a script versus a scene may affect both the
229227
initialization *and* the number of times the engine calls the setter.
230228

231229
_ready vs. _enter_tree vs. NOTIFICATION_PARENTED

0 commit comments

Comments
 (0)