Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a paragraph about self #8928

Merged
merged 16 commits into from
Dec 26, 2024
Merged
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 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 | See `self`_. |
elenakrittik marked this conversation as resolved.
Show resolved Hide resolved
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| 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,36 @@ 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
^^^^
tetrapod00 marked this conversation as resolved.
Show resolved Hide resolved

``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>`).
elenakrittik marked this conversation as resolved.
Show resolved Hide resolved

::

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
makes the relationships between parts of your game's code harder to reason about. It is easy to forget to define
a variable that the base class expected or forget about an invariant that the base class assumed to be true when
implementing a function needed by the base class.
elenakrittik marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down