Skip to content

Commit d812b9b

Browse files
committed
Update documentation to match new traits PR
Mostly removing `trait_name` references
1 parent 7e1389d commit d812b9b

File tree

3 files changed

+37
-126
lines changed

3 files changed

+37
-126
lines changed

tutorials/scripting/gdscript/gdscript_basics.rst

Lines changed: 33 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,7 @@ in case you want to take a look under the hood.
190190
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
191191
| extends | Defines what class to extend with the current class. |
192192
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
193-
| trait | Defines an inner trait. See `Inner traits`_. |
194-
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
195-
| trait_name | Defines the script as a globally accessible trait with the specified name. See `Registering named traits`_. |
193+
| trait | Defines a trait. See `Traits`_. |
196194
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
197195
| uses | Defines what trait(s) the current class should use. |
198196
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
@@ -1107,7 +1105,6 @@ Valid types are:
11071105
- Constant names if they contain a script or trait resource (``MyScript`` if you declared ``const MyScript = preload("res://my_script.gd")``).
11081106
- Other classes or traits in the same file, respecting scope (``InnerClass.NestedClass`` if you declared ``class NestedClass`` inside the ``class InnerClass`` in the same scope).
11091107
- Script classes declared with the ``class_name`` keyword.
1110-
- Traits declared with the ``trait_name`` keyword.
11111108
- Autoloads registered as singletons.
11121109

11131110
.. note::
@@ -1258,18 +1255,6 @@ A base class static variable can also be accessed via a child class:
12581255
B.x = 3
12591256
prints(A.x, B.x) # 3 3
12601257

1261-
Likewise, if a class uses a trait with static variables, it will inherit those
1262-
variables::
1263-
1264-
trait HasStatic:
1265-
static var static_var = 3
1266-
1267-
class UsingClass:
1268-
uses HasStatic
1269-
1270-
func _ready():
1271-
print(UsingClass.static_var)
1272-
12731258
``@static_unload`` annotation
12741259
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12751260

@@ -2381,94 +2366,74 @@ class resource is done by calling the ``new`` function on the class object:
23812366
var a = MyClass.new()
23822367
a.some_function()
23832368

2369+
2370+
.. _doc_gdscript_basics_traits:
2371+
23842372
Traits
23852373
------
23862374

23872375
Since Godot 4.x, GDScript supports traits, which are collections of behaviors and attributes
23882376
that classes can use to guarantee
23892377
functionality to themselves and other objects that may be attempting to use them.
23902378

2391-
Like classes, by default all ``.gdt`` files are unnamed traits, and you must reference
2392-
them using a relative or absolute path.
2393-
::
2394-
# Use the trait 'interactable.gdt'.
2395-
uses "res://path/to/interactable.gdt"
2396-
23972379
Note that traits on their own *cannot* be instantiated the same way that classes can.
23982380

2399-
.. _doc_gdscript_basics_trait_name:
2400-
2401-
Registering named traits
2381+
Registering traits
24022382
~~~~~~~~~~~~~~~~~~~~~~~~
24032383

2404-
Traits can be given a global name by using the ``trait_name`` keyword.
2384+
Traits can be created inside of a GDScript class by using the ``trait`` keyword.
24052385
::
2406-
trait_name MyTrait
2386+
2387+
trait Damageable:
2388+
signal died
2389+
2390+
const MAX_HEALTH = 100
2391+
var health = MAX_HEALTH
2392+
2393+
func take_damage(amount): # Will automatically exist in any class using this trait.
2394+
health -= amount
2395+
on_hit()
2396+
if health <= 0:
2397+
on_death()
2398+
died.emit()
2399+
2400+
func on_hit() # Unimplemented method - Must be overriden in any class using this trait.
24072401
24082402
Using traits in a class
24092403
~~~~~~~~~~~~~~~~~~~~~~~
24102404

2411-
For a class to use a trait, use the ``uses`` keyword:
2412-
::
2413-
class_name MyScript
2414-
uses MyTrait
2415-
2416-
2417-
Traits may also extend classes. If a trait extends a class, then any class
2418-
that uses that trait must also have that class as an ancestor.
2405+
For a class to use a trait, include the ``uses`` keyword:
24192406
::
2420-
# movable.gdt
2421-
trait_name Movable
2422-
extends PhysicsBody2D
2423-
2424-
# character.gd
2425-
class_name Character
2426-
extends CharacterBody2D
2427-
uses Movable # Allowed, since CharacterBody2D inherits from PhysicsBody2D.
2407+
class_name Player
2408+
uses Damageable
24282409

24292410
The ``is`` keyword can be used to determine if a given instance uses a particular trait.
24302411
::
2431-
if entity is Movable:
2432-
entity.move()
2412+
if entity is Damageable:
2413+
entity.take_damage(1)
24332414

24342415
If a trait provides a method signature, but no body, then the using class must implement
24352416
a body for the method.
24362417
::
2437-
# explosive.gdt
2438-
trait_name Explosive
2439-
2440-
func explode() # Body is not defined here, so it must be defined in each class that uses it.
2441-
2442-
2443-
# exploding_barrel.gd
2444-
class_name ExplodingBarrel
2445-
extends Sprite2D
2446-
uses Explosive
2418+
class_name Player
2419+
extends CharacterBody2D
2420+
uses Damageable
24472421

2448-
func explode(): # If this definition of Explosive.explode isn't provided, we will get an error.
2449-
print("Kaboom!")
2450-
queue_free()
2422+
func on_hit():
2423+
print("Ouch, that hurt!")
24512424

24522425
If a trait provides a method signature *and* a body, then the using class inherits it by default
24532426
and doesn't need to provide its own implementation. It still can override the trait's
24542427
implementation if desired, but the parameter count must stay the same, and the parameter and return
24552428
types must be compatible.
24562429
::
2457-
# damageable.gdt
2458-
trait_name Damageable
2459-
2460-
func take_damage():
2461-
print("Ouch!")
2462-
2463-
2464-
# invincible_npc.gd
24652430
class_name InvincibleNPC
24662431
extends Sprite2D
24672432
uses Damageable
24682433

24692434
# Allowed, and will run instead of Damageable's original take_damage method.
2470-
func take_damage():
2471-
print("You can't hurt me!")
2435+
func take_damage(amount):
2436+
print("Ah ha ha! You can't hurt me!")
24722437

24732438
..
24742439
TODO: Confirm these behaviors
@@ -2479,39 +2444,6 @@ Other class members have similar rules:
24792444
- Signals can be overriden, as long as the parameter count is maintained and the parameter types are compatible.
24802445
- Named enums can be overriden and have new enum values.
24812446

2482-
.. _doc_gdscript_basics_inner_traits:
2483-
2484-
Inner traits
2485-
~~~~~~~~~~~~
2486-
2487-
Like inner classes, a class or trait file may contain inner traits, defined with the ``trait``
2488-
keyword. Unlike inner classes, they cannot be instantiated directly, but their name can be
2489-
referenced for using or checking use of themselves.
2490-
::
2491-
# An inner trait in this class file.
2492-
trait SomeInnerTrait:
2493-
func do_something():
2494-
print("I did something!")
2495-
2496-
2497-
# An inner class in this class file, which uses the inner trait.
2498-
class SomeInnerClass:
2499-
uses SomeInnerTrait
2500-
2501-
2502-
func _init():
2503-
var c = SomeInnerClass.new()
2504-
if c is SomeInnerTrait:
2505-
c.do_something()
2506-
2507-
.. _doc_gdscript_basics_traits_as_resources:
2508-
2509-
Traits as resources
2510-
~~~~~~~~~~~~~~~~~~~
2511-
2512-
Traits stored as files are treated as :ref:`GDTraits <class_GDTrait>`, and must
2513-
be loaded similarly to classes (see `Classes as resources`_).
2514-
25152447
Exports
25162448
-------
25172449

tutorials/scripting/gdscript/gdscript_styleguide.rst

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ code. As a summary table:
651651
+---------------+----------------+----------------------------------------------------+
652652
| Class names | PascalCase | ``class_name YAMLParser`` |
653653
+---------------+----------------+----------------------------------------------------+
654-
| Trait names | PascalCase | ``trait_name Interactable`` |
654+
| Trait names | PascalCase | ``trait Interactable`` |
655655
+---------------+----------------+----------------------------------------------------+
656656
| Node names | PascalCase | ``Camera3D``, ``Player`` |
657657
+---------------+----------------+----------------------------------------------------+
@@ -671,8 +671,7 @@ code. As a summary table:
671671
File names
672672
~~~~~~~~~~
673673

674-
Use snake_case for file names. For named classes and traits, convert the PascalCase class or trait
675-
name to snake_case::
674+
Use snake\_case for file names. For named classes, convert the PascalCase class name to snake_case::
676675

677676
# This file should be saved as `weapon.gd`.
678677
class_name Weapon
@@ -684,12 +683,6 @@ name to snake_case::
684683
class_name YAMLParser
685684
extends Object
686685

687-
::
688-
689-
# This file should be saved as `interactable.gdt`.
690-
trait_name Interactable
691-
extends Node
692-
693686
This is consistent with how C++ files are named in Godot's source code. This
694687
also avoids case sensitivity issues that can crop up when exporting a project
695688
from Windows to other platforms.

tutorials/scripting/gdscript/static_typing.rst

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ Here is a complete list of what can be used as a type hint:
122122
7. Global, native and custom named enums. Note that an enum type is just an ``int``,
123123
there is no guarantee that the value belongs to the set of enum values.
124124
8. Constants (including local ones) if they contain a preloaded class or enum.
125-
9. :ref:`Global traits <doc_gdscript_basics_trait_name>`.
126-
10. :ref:`Inner traits <_doc_gdscript_basics_inner_traits>`.
125+
9. :ref:`Traits <doc_gdscript_basics_traits>`.
127126

128127
You can use any class or trait, including your custom classes and traits, as types. There are two ways
129128
to use them in scripts. The first method is to preload the script you want to use
@@ -148,20 +147,7 @@ and you can use it anywhere, without having to preload it into a constant:
148147
::
149148

150149
var my_rifle: Rifle
151-
152-
These methods also work with traits, except using ``trait_name`` and ``uses`` in place
153-
of ``class_name`` and ``extends``::
154-
155-
const Shootable = preload("res://player/weapons/shootable.gdt")
156-
var something_shootable: Shootable
157-
158-
::
159-
160-
class_name Rifle
161-
uses Shootable
162-
163-
# Somewhere else...
164-
var my_shootable_thing: Shootable
150+
165151

166152
Specify the return type of a function with the arrow ``->``
167153
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)