Skip to content
New issue

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

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

Already on GitHub? # to your account

Add a paragraph about self #8928

Merged
merged 16 commits into from
Dec 26, 2024
Merged
Changes from 5 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
33 changes: 29 additions & 4 deletions tutorials/scripting/gdscript/gdscript_basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,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`_. |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| signal | Defines a signal. |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
Expand Down Expand Up @@ -1293,9 +1293,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 @@ -1454,6 +1454,31 @@ 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, it also allows you to access properties, methods, and other names in subclasses of the current script.

::

class_name Weapon extends Node

func _ready() -> void:
print(self.damage)

``damage`` is not defined in ``Weapon``, but we (through, e.g., :ref:`documenting our code <doc_gdscript_documentation_comments>`)
can assume that it will exist in subclasses of ``Weapon``. For example:

::

extends Weapon

var damage := 10

Will print "10" when added to the scene, but if we forgot to define ``damage`` on our subclass or
used ``Weapon`` directly, that would result in an error.

if/else/elif
^^^^^^^^^^^^

Expand Down