Skip to content
Open
Changes from all 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
32 changes: 25 additions & 7 deletions tutorials/scripting/gdscript/gdscript_basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,20 @@ here's an example of how GDScript looks.
super.something(p1, p2)


# Inner class
# Inner class (also called "subclass").

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a thing that we are calling them subclasses in official stuff?

class Something:
var a = 10

# Constructor method for the Something inner class.
# This is called when the class is instantiated with `new()`.
# A constructor method may have parameters, optionally with default values
# and static type hints.
func _init():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment you linked asked for an example on how to call init with args. Maybe we could pass the message into _init as paramter to make clear how it works.

print("Constructed!")

# Constructor
func _init():
print("Constructed!")

func construct():
# Create a new instance of the above subclass.
var lv = Something.new()
print(lv.a)

Expand Down Expand Up @@ -2353,9 +2359,13 @@ the function name with the attribute operator:
Class constructor
~~~~~~~~~~~~~~~~~

The class constructor, called on class instantiation, is named ``_init``. If you
want to call the base class constructor, you can also use the ``super`` syntax.
Note that every class has an implicit constructor that is always called
The class constructor, called on class instantiation with ``new()``, is named
``_init()``. Like methods, the class constructor may have one or more
parameters, which can have default values. These parameters can be passed when

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if worth mentioning, but varargs seem to be supported in constructors as well.

calling ``new()``.

If you want to call the base class constructor, you can also use the ``super``
syntax. Note that every class has an implicit constructor that is always called
(defining the default values of class variables). ``super`` is used to call the
explicit constructor:

Expand Down Expand Up @@ -2409,6 +2419,14 @@ There are a few things to keep in mind here:
func _init():
super(5)

.. warning::

When instancing nodes through ``class_name`` or other mechanisms, the

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean by instancing through class_name.

You can totally inherit Node and provide a constructor with required arguments, as long as you create it manually at runtime. This is only an issue when trying to put the node into a scene. (And I think it's an issue for custom resources in general, because they don't load correctly iirc.)

``_init()`` constructor is called with no parameters. This means that for
constructors of classes that inherit Node, constructors must either have no
parameters, or have default values for **all** parameters. Otherwise, the
engine will print an error when instancing the node.

Static constructor
~~~~~~~~~~~~~~~~~~

Expand Down
Loading