Skip to content

Commit 38ae512

Browse files
authored
Merge pull request #10945 from Calinou/gdscript-abstract-classes
Document abstract classes in GDScript
2 parents f86a5dc + db2bc41 commit 38ae512

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed

tutorials/scripting/gdscript/gdscript_basics.rst

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,70 @@ in other scripts without the need to ``load`` or ``preload`` them:
20022002
automatically hidden by the editor windows along with the built-in editor nodes used
20032003
by the Godot editor.
20042004

2005+
.. _doc_gdscript_basics_abstract_class:
2006+
2007+
Registering abstract classes
2008+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2009+
2010+
Since Godot 4.5, you can register abstract classes using the ``abstract`` keyword.
2011+
An abstract class is a class that cannot be instantiated directly. Instead, it
2012+
is meant to be subclassed by other classes. Attempting to instantiate
2013+
an abstract class will result in an error.
2014+
2015+
For example, you could have an abstract class called ``Shape`` that defines
2016+
a method called ``draw()``. You can then create subclasses like ``Circle``
2017+
and ``Square`` that implement the ``draw()`` method in their own way.
2018+
This allows you to define a common *interface* for all shapes without
2019+
having to implement all the details in the abstract class itself:
2020+
2021+
.. code-block:: gdscript
2022+
2023+
abstract class Shape:
2024+
func draw():
2025+
# It is possible for subclasses to call the parent class method using `super()`.
2026+
# In this example, we won't use `super()` to call the parent class method,
2027+
# so we can leave this method empty.
2028+
pass
2029+
2030+
# This is a concrete (non-abstract) subclass of Shape.
2031+
class Circle extends Shape:
2032+
func draw():
2033+
print("Drawing a circle.")
2034+
2035+
class Square extends Shape:
2036+
func draw():
2037+
print("Drawing a square.")
2038+
2039+
Both subclasses and classes created using ``class_name`` can be abstract. This
2040+
example creates two abstract classes, one of which is a subclass of another
2041+
abstract class:
2042+
2043+
.. code-block:: gdscript
2044+
2045+
abstract class_name AbstractClass
2046+
extends Node
2047+
2048+
abstract class AbstractSubClass:
2049+
func _ready():
2050+
pass
2051+
2052+
# This is an example of a concrete subclass of AbstractSubClass.
2053+
# This class can be instantiated using `AbstractClass.ConcreteSubclass.new()`
2054+
# in other scripts, even though it's part of an abstract `class_name` script.
2055+
class ConcreteClass extends AbstractSubClass:
2056+
func _ready():
2057+
print("Concrete class ready.")
2058+
2059+
.. warning::
2060+
2061+
Since an abstract class cannot be instantiated, it is not possible to attach
2062+
an abstract class to a node. If you attempt to do so, the engine will print
2063+
an error when running the scene:
2064+
2065+
::
2066+
2067+
Cannot set object script. Script '<path to script>' should not be abstract.
2068+
20052069
Inheritance
20062070
~~~~~~~~~~~
20072071

tutorials/scripting/gdscript/gdscript_styleguide.rst

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ We suggest to organize GDScript code this way:
801801
13. remaining static methods
802802
14. overridden built-in virtual methods:
803803
1. _init()
804-
2. _enter_tree()
804+
2. _enter_tree()
805805
3. _ready()
806806
4. _process()
807807
5. _physics_process()
@@ -813,7 +813,7 @@ We suggest to organize GDScript code this way:
813813
And put the class methods and variables in the following order depending on their access modifiers:
814814

815815
::
816-
816+
817817
1. public
818818
2. private
819819

@@ -829,7 +829,6 @@ This code order follows four rules of thumb:
829829
4. The object's construction and initialization functions, ``_init`` and
830830
``_ready``, come before functions that modify the object at runtime.
831831

832-
833832
Class declaration
834833
~~~~~~~~~~~~~~~~~
835834

@@ -838,7 +837,9 @@ first line of the script.
838837

839838
Follow with the optional ``@icon`` then the ``class_name`` if necessary. You can turn a
840839
GDScript file into a global type in your project using ``class_name``. For more
841-
information, see :ref:`doc_gdscript_basics_class_name`.
840+
information, see :ref:`doc_gdscript_basics_class_name`. If the class is meant
841+
to be an :ref:`abstract class <doc_gdscript_basics_abstract_class>`,
842+
add ``abstract`` *before* the ``class_name`` keyword, but on the same line.
842843

843844
Then, add the ``extends`` keyword if the class extends a built-in type.
844845

@@ -849,7 +850,7 @@ and how other developers should use it, for example.
849850

850851
::
851852

852-
class_name MyNode
853+
abstract class_name MyNode
853854
extends Node
854855
## A brief description of the class's role and functionality.
855856
##
@@ -1066,5 +1067,3 @@ that type will be used to infer the type of the var.
10661067
This option is considered more :ref:`type-safe<doc_gdscript_static_typing_safe_lines>` than type hints,
10671068
but also less null-safe as it silently casts the variable to ``null`` in case of a type mismatch at runtime,
10681069
without an error/warning.
1069-
1070-

0 commit comments

Comments
 (0)