Skip to content

Commit 473c817

Browse files
committed
update
1 parent 1912b3f commit 473c817

File tree

1 file changed

+14
-33
lines changed

1 file changed

+14
-33
lines changed

tutorials/scripting/gdscript/gdscript_basics.rst

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,49 +1535,30 @@ self
15351535

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

15401541
::
15411542

1542-
class_name Item extends Node
1543-
1544-
# Returns the expected size an item will take up in player's inventory (or -1 if it cannot be collected).
1545-
func get_size() -> int:
1546-
var size = get(&"size")
1547-
1548-
return size if size else -1
1549-
1550-
# When player touches an item, collect it if it is collectible (i.e. has a `collect` method).
1551-
func on_player_touch() -> void:
1552-
if has_method(&"collect"):
1553-
# collect() # Would be an error!
1554-
# self.collect() # An error, too! `self` only bypasses property checks.
1555-
call(&"collect")
1556-
1557-
``size`` nor ``collect()`` are defined in the base ``Item`` class, but if they are defined at runtime, our code will
1558-
react appropriately. For example::
1559-
1560-
class_name Potion extends Item
1561-
1562-
var size := 2
1543+
extends Node
15631544

1564-
func collect() -> void:
1565-
print("Collected a potion!")
1545+
func _ready():
1546+
# Compile time error, as `my_var` is not defined in the current class or its ancestors.
1547+
print(my_var)
1548+
# Checked at runtime, thus may work for dynamic properties or descendant classes.
1549+
print(self.my_var)
15661550

1567-
If we call ``get_size`` on our ``Potion``, it will return ``2``, and if we (hypothetically) touch the potion with our
1568-
character, we will see "Collected a potion!" in the logs.
1551+
# Compile time error, as `my_func()` is not defined in the current class or its ancestors.
1552+
my_func()
1553+
# Checked at runtime, thus may work for descendant classes.
1554+
self.my_func()
15691555

15701556
.. warning::
15711557

15721558
Beware that accessing members of child classes in the base class is often considered a bad practice because this
15731559
makes the relationships between parts of your game's code harder to reason about. It is easy to forget to define
1574-
a variable that a base class expected, and more complex dependencies between classes cause the code to be
1575-
non-self-explanatory.
1576-
1577-
The above example isn't ideal too and is exaggerated for illustration purposes. In ``get_size``, instead of querying
1578-
for the ``size`` property, we could've instead made it return ``-1`` by default in our base class, and then in our
1579-
potion, we would've overridden it to return ``2``. Same for ``collect()`` - we could've defined it in our base class
1580-
but made it do nothing by default. Then, the potion subclass would've overridden it to do its own logic.
1560+
a variable that the base class expected or forget about an invariant that the base class assumed to be true when
1561+
implementing a function needed by the base class.
15811562

15821563
if/else/elif
15831564
~~~~~~~~~~~~

0 commit comments

Comments
 (0)