Skip to content

Commit bf521f2

Browse files
committed
Update style guide and static typing pages
Currently, the style guide uses what seem like good reasonable policies to me (notably, PascalCase adjectives for trait names, and `uses` after `extends`). We'll need to make sure this is agreed-upon as the good way to do it before merging, though
1 parent c4b5a31 commit bf521f2

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

tutorials/scripting/gdscript/gdscript_styleguide.rst

+33-20
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Here is a complete class example based on these guidelines:
3030

3131
class_name StateMachine
3232
extends Node
33+
uses Activatable
3334
## Hierarchical State machine for the player.
3435
##
3536
## Initializes states and delegates engine callbacks ([method Node._physics_process],
@@ -629,6 +630,8 @@ code. As a summary table:
629630
+---------------+----------------+----------------------------------------------------+
630631
| Class names | PascalCase | ``class_name YAMLParser`` |
631632
+---------------+----------------+----------------------------------------------------+
633+
| Trait names | PascalCase | ``trait_name Interactable`` |
634+
+---------------+----------------+----------------------------------------------------+
632635
| Node names | PascalCase | ``Camera3D``, ``Player`` |
633636
+---------------+----------------+----------------------------------------------------+
634637
| Functions | snake_case | ``func load_level():`` |
@@ -647,7 +650,7 @@ code. As a summary table:
647650
File names
648651
~~~~~~~~~~
649652

650-
Use snake_case for file names. For named classes, convert the PascalCase class
653+
Use snake_case for file names. For named classes and traits, convert the PascalCase class or trait
651654
name to snake_case::
652655

653656
# This file should be saved as `weapon.gd`.
@@ -660,20 +663,26 @@ name to snake_case::
660663
class_name YAMLParser
661664
extends Object
662665

666+
::
667+
668+
# This file should be saved as `interactable.gdt`.
669+
trait_name Interactable
670+
extends Node
671+
663672
This is consistent with how C++ files are named in Godot's source code. This
664673
also avoids case sensitivity issues that can crop up when exporting a project
665674
from Windows to other platforms.
666675

667676
Classes and nodes
668677
~~~~~~~~~~~~~~~~~
669678

670-
Use PascalCase for class and node names:
679+
Use PascalCase for class, trait, and node names:
671680

672681
::
673682

674683
extends CharacterBody3D
675684

676-
Also use PascalCase when loading a class into a constant or a variable:
685+
Also use PascalCase when loading a class or trait into a constant or a variable:
677686

678687
::
679688

@@ -766,23 +775,24 @@ We suggest to organize GDScript code this way:
766775
01. @tool
767776
02. class_name
768777
03. extends
769-
04. ## docstring
770-
771-
05. signals
772-
06. enums
773-
07. constants
774-
08. @export variables
775-
09. public variables
776-
10. private variables
777-
11. @onready variables
778-
779-
12. optional built-in virtual _init method
780-
13. optional built-in virtual _enter_tree() method
781-
14. built-in virtual _ready method
782-
15. remaining built-in virtual methods
783-
16. public methods
784-
17. private methods
785-
18. subclasses
778+
04. uses
779+
05. ## docstring
780+
781+
06. signals
782+
07. enums
783+
08. constants
784+
09. @export variables
785+
10. public variables
786+
11. private variables
787+
12. @onready variables
788+
789+
13. optional built-in virtual _init method
790+
14. optional built-in virtual _enter_tree() method
791+
15. built-in virtual _ready method
792+
16. remaining built-in virtual methods
793+
17. public methods
794+
18. private methods
795+
19. subclasses
786796

787797
We optimized the order to make it easy to read the code from top to bottom, to
788798
help developers reading the code for the first time understand how it works, and
@@ -808,6 +818,8 @@ global type in your project using this feature. For more information, see
808818
:ref:`doc_gdscript`.
809819

810820
Then, add the ``extends`` keyword if the class extends a built-in type.
821+
If the class uses any traits, add the ``uses`` keyword along with all of the trait
822+
names (or filepaths, if the traits are unnamed) of the traits it uses.
811823

812824
Following that, you should have the class's optional
813825
:ref:`documentation comments <doc_gdscript_documentation_comments>`.
@@ -818,6 +830,7 @@ and how other developers should use it, for example.
818830

819831
class_name MyNode
820832
extends Node
833+
uses MyTrait
821834
## A brief description of the class's role and functionality.
822835
##
823836
## The description of the script, what it can do,

tutorials/scripting/gdscript/static_typing.rst

+19-3
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,10 @@ Here is a complete list of what can be used as a type hint:
116116
7. Global, native and custom named enums. Note that an enum type is just an ``int``,
117117
there is no guarantee that the value belongs to the set of enum values.
118118
8. Constants (including local ones) if they contain a preloaded class or enum.
119+
9. :ref:`Global traits <doc_gdscript_basics_trait_name>`.
120+
10. :ref:`Inner traits <_doc_gdscript_basics_inner_traits>`.
119121

120-
You can use any class, including your custom classes, as types. There are two ways
122+
You can use any class or trait, including your custom classes and traits, as types. There are two ways
121123
to use them in scripts. The first method is to preload the script you want to use
122124
as a type in a constant::
123125

@@ -135,6 +137,20 @@ and you can use it anywhere, without having to preload it into a constant::
135137

136138
var my_rifle: Rifle
137139

140+
These methods also work with traits, except using ``trait_name`` and ``uses`` in place
141+
of ``class_name`` and ``extends``::
142+
143+
const Shootable = preload("res://player/weapons/shootable.gdt")
144+
var something_shootable: Shootable
145+
146+
::
147+
148+
class_name Rifle
149+
uses Shootable
150+
151+
# Somewhere else...
152+
var my_shootable_thing: Shootable
153+
138154
Specify the return type of a function with the arrow ``->``
139155
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
140156

@@ -151,7 +167,7 @@ as with variables::
151167
health_points -= damage
152168
return health_points <= 0
153169

154-
You can also use your own classes as return types::
170+
You can also use your own classes or traits as return types::
155171

156172
# Adds an item to the inventory and returns it.
157173
func add(reference: Item, amount: int) -> Item:
@@ -199,7 +215,7 @@ To define the type of an ``Array``, enclose the type name in ``[]``.
199215

200216
An array's type applies to ``for`` loop variables, as well as some operators like
201217
``[]``, ``[]=``, and ``+``. Array methods (such as ``push_back``) and other operators
202-
(such as ``==``) are still untyped. Built-in types, native and custom classes,
218+
(such as ``==``) are still untyped. Built-in types, native and custom classes and traits,
203219
and enums may be used as element types. Nested array types
204220
(like ``Array[Array[int]]``) are not supported.
205221

0 commit comments

Comments
 (0)