Skip to content

Commit 3106302

Browse files
authored
Merge pull request godotengine#7579 from dalexeev/gds-setter-getter-doc
GDScript: Clarify setter/getter documentation
2 parents 85fc55a + 98f5970 commit 3106302

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
@@ -1997,6 +1997,40 @@ Example::
19971997
set(value):
19981998
milliseconds = value * 1000
19991999

2000+
.. note::
2001+
2002+
Unlike ``setget`` in previous Godot versions, the properties setter and getter are **always** called (except as noted below),
2003+
even when accessed inside the same class (with or without prefixing with ``self.``). This makes the behavior
2004+
consistent. If you need direct access to the value, use another variable for direct access and make the property
2005+
code use that name.
2006+
2007+
Alternative syntax
2008+
^^^^^^^^^^^^^^^^^^
2009+
2010+
Also there is another notation to use existing class functions if you want to split the code from the variable declaration
2011+
or you need to reuse the code across multiple properties (but you can't distinguish which property the setter/getter is being called for)::
2012+
2013+
var my_prop:
2014+
get = get_my_prop, set = set_my_prop
2015+
2016+
This can also be done in the same line::
2017+
2018+
var my_prop: get = get_my_prop, set = set_my_prop
2019+
2020+
The setter and getter must use the same notation, mixing styles for the same variable is not allowed.
2021+
2022+
.. note::
2023+
2024+
You cannot specify type hints for *inline* setters and getters. This is done on purpose to reduce the boilerplate.
2025+
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.
2026+
Separated setter/getter functions can have type hints, and the type must match the variable's type or be a wider type.
2027+
2028+
When setter/getter is not called
2029+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2030+
2031+
When a variable is initialized, the value of the initializer will be written directly to the variable.
2032+
Including if the ``@onready`` annotation is applied to the variable.
2033+
20002034
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,
20012035
so it won't generate infinite recursion and saves you from explicitly declaring another variable::
20022036

@@ -2008,22 +2042,24 @@ so it won't generate infinite recursion and saves you from explicitly declaring
20082042
changed.emit(value)
20092043
warns_when_changed = value
20102044

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

2013-
.. note::
2047+
var my_prop: set = set_my_prop
20142048

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

2020-
In case you want to split the code from the variable declaration or you need to share the code across multiple properties,
2021-
you can use a different notation to use existing class functions::
2052+
.. warning::
20222053

2023-
var my_prop:
2024-
get = get_my_prop, set = set_my_prop
2054+
The exception does **not** propagate to other functions called in the setter/getter.
2055+
For example, the following code **will** cause an infinite recursion::
2056+
2057+
var my_prop:
2058+
set(value):
2059+
set_my_prop(value)
20252060

2026-
This can also be done in the same line.
2061+
func set_my_prop(value):
2062+
my_prop = value # Infinite recursion, since `set_my_prop()` is not the setter.
20272063

20282064
.. _doc_gdscript_tool_mode:
20292065

0 commit comments

Comments
 (0)