Improve documentation on constructors in GDScript basics - #12138
Conversation
Calinou
commented
Jul 9, 2026
- See https://github.com/godotengine/godot-docs-user-notes/discussions/758#discussioncomment-17579978.
|
|
||
|
|
||
| # Inner class | ||
| # Inner class (also called "subclass"). |
There was a problem hiding this comment.
Is it a thing that we are calling them subclasses in official stuff?
|
|
||
| .. warning:: | ||
|
|
||
| When instancing nodes through ``class_name`` or other mechanisms, the |
There was a problem hiding this comment.
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.)
| # 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(): |
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
Not sure if worth mentioning, but varargs seem to be supported in constructors as well.