Skip to content

Commit 98f5970

Browse files
committed
GDScript: Clarify setter/getter documentation
1 parent ccae259 commit 98f5970

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

tutorials/scripting/gdscript/gdscript_basics.rst

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,40 @@ Example::
16651665
set(value):
16661666
milliseconds = value * 1000
16671667

1668+
.. note::
1669+
1670+
Unlike ``setget`` in previous Godot versions, the properties setter and getter are **always** called (except as noted below),
1671+
even when accessed inside the same class (with or without prefixing with ``self.``). This makes the behavior
1672+
consistent. If you need direct access to the value, use another variable for direct access and make the property
1673+
code use that name.
1674+
1675+
Alternative syntax
1676+
^^^^^^^^^^^^^^^^^^
1677+
1678+
Also there is another notation to use existing class functions if you want to split the code from the variable declaration
1679+
or you need to reuse the code across multiple properties (but you can't distinguish which property the setter/getter is being called for)::
1680+
1681+
var my_prop:
1682+
get = get_my_prop, set = set_my_prop
1683+
1684+
This can also be done in the same line::
1685+
1686+
var my_prop: get = get_my_prop, set = set_my_prop
1687+
1688+
The setter and getter must use the same notation, mixing styles for the same variable is not allowed.
1689+
1690+
.. note::
1691+
1692+
You cannot specify type hints for *inline* setters and getters. This is done on purpose to reduce the boilerplate.
1693+
If the variable is typed, then the setter's argument is automatically of the same type, and the getter's return value must match it.
1694+
Separated setter/getter functions can have type hints, and the type must match the variable's type or be a wider type.
1695+
1696+
When setter/getter is not called
1697+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1698+
1699+
When a variable is initialized, the value of the initializer will be written directly to the variable.
1700+
Including if the ``@onready`` annotation is applied to the variable.
1701+
16681702
Using the variable's name to set it inside its own setter or to get it inside its own getter will directly access the underlying member,
16691703
so it won't generate infinite recursion and saves you from explicitly declaring another variable::
16701704

@@ -1676,22 +1710,24 @@ so it won't generate infinite recursion and saves you from explicitly declaring
16761710
changed.emit(value)
16771711
warns_when_changed = value
16781712

1679-
This backing member variable is not created if you don't use it.
1713+
This also applies to the alternative syntax::
16801714

1681-
.. note::
1715+
var my_prop: set = set_my_prop
16821716

1683-
Unlike ``setget`` in previous Godot versions, the properties setter and getter are **always** called,
1684-
even when accessed inside the same class (with or without prefixing with ``self.``). This makes the behavior
1685-
consistent. If you need direct access to the value, use another variable for direct access and make the property
1686-
code use that name.
1717+
func set_my_prop(value):
1718+
my_prop = value # No infinite recursion.
16871719

1688-
In case you want to split the code from the variable declaration or you need to share the code across multiple properties,
1689-
you can use a different notation to use existing class functions::
1720+
.. warning::
16901721

1691-
var my_prop:
1692-
get = get_my_prop, set = set_my_prop
1722+
The exception does **not** propagate to other functions called in the setter/getter.
1723+
For example, the following code **will** cause an infinite recursion::
1724+
1725+
var my_prop:
1726+
set(value):
1727+
set_my_prop(value)
16931728

1694-
This can also be done in the same line.
1729+
func set_my_prop(value):
1730+
my_prop = value # Infinite recursion, since `set_my_prop()` is not the setter.
16951731

16961732
.. _doc_gdscript_tool_mode:
16971733

0 commit comments

Comments
 (0)