-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Improve documentation on constructors in GDScript basics #12138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,14 +103,20 @@ here's an example of how GDScript looks. | |
| super.something(p1, p2) | ||
|
|
||
|
|
||
| # Inner class | ||
| # Inner class (also called "subclass"). | ||
| 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(): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| print("Constructed!") | ||
|
|
||
| # Constructor | ||
| func _init(): | ||
| print("Constructed!") | ||
|
|
||
| func construct(): | ||
| # Create a new instance of the above subclass. | ||
| var lv = Something.new() | ||
| print(lv.a) | ||
|
|
||
|
|
@@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
|
||
|
|
@@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what you mean by instancing through You can totally inherit |
||
| ``_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 | ||
| ~~~~~~~~~~~~~~~~~~ | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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?