Skip to content

Commit

Permalink
Add a paragraph about self (#8928)
Browse files Browse the repository at this point in the history
Adds a paragraph about the self keyword and documents that
it can be used to refer to variables defined in subclasses of
current class.

---------

Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com>
Co-authored-by: RedMser <5117197+RedMser@users.noreply.github.com>
Co-authored-by: Danil Alexeev <dalexeev12@yandex.ru>
Co-authored-by: tetrapod <145553014+tetrapod00@users.noreply.github.com>
  • Loading branch information
5 people authored Dec 26, 2024
1 parent e117dc8 commit 63cec69
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions tutorials/scripting/gdscript/gdscript_basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ in case you want to take a look under the hood.
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| as | Cast the value to a given type if possible. |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| self | Refers to current class instance. |
| self | Refers to current class instance. See `self`_. |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| super | Resolves the scope of the parent method. See `Inheritance`_. |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
Expand Down Expand Up @@ -1326,9 +1326,9 @@ Functions

Functions always belong to a `class <Classes_>`_. The scope priority for
variable look-up is: local → class member → global. The ``self`` variable is
always available and is provided as an option for accessing class members, but
is not always required (and should *not* be sent as the function's first
argument, unlike Python).
always available and is provided as an option for accessing class members
(see `self`_), but is not always required (and should *not* be sent as the
function's first argument, unlike Python).

::

Expand Down Expand Up @@ -1530,6 +1530,39 @@ Here are some examples of expressions::
Identifiers, attributes, and subscripts are valid assignment targets. Other expressions cannot be on the left side of
an assignment.

self
^^^^

``self`` can be used to refer to the current instance and is often equivalent to
directly referring to symbols available in the current script. However, ``self``
also allows you to access properties, methods, and other names that are defined
dynamically (i.e. are expected to exist in subtypes of the current class, or are
provided using :ref:`_set() <class_Object_private_method__set>` and/or
:ref:`_get() <class_Object_private_method__get>`).

::

extends Node

func _ready():
# Compile time error, as `my_var` is not defined in the current class or its ancestors.
print(my_var)
# Checked at runtime, thus may work for dynamic properties or descendant classes.
print(self.my_var)

# Compile time error, as `my_func()` is not defined in the current class or its ancestors.
my_func()
# Checked at runtime, thus may work for descendant classes.
self.my_func()

.. warning::

Beware that accessing members of child classes in the base class is often
considered a bad practice, because this blurs the area of responsibility of
any given piece of code, making the overall relationship between parts of
your game harder to reason about. Besides that, one can simply forget that
the parent class had some expectations about it's descendants.

if/else/elif
~~~~~~~~~~~~

Expand Down

0 comments on commit 63cec69

Please # to comment.