Skip to content

Commit 6361c66

Browse files
authored
Merge pull request #10711 from raulsntos/dotnet/export-tool-diagnostics
C#: Document `[ExportToolButton]` diagnostics
2 parents f2d2133 + 662a055 commit 6361c66

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
GD0108: The exported tool button is not in a tool class
2+
=======================================================
3+
4+
==================================== ======================================
5+
Value
6+
==================================== ======================================
7+
**Rule ID** GD0108
8+
**Category** Usage
9+
**Fix is breaking or non-breaking** Non-breaking
10+
**Enabled by default** Yes
11+
==================================== ======================================
12+
13+
Cause
14+
-----
15+
16+
A property is annotated with the ``[ExportToolButton]`` attribute in a class that
17+
is **not** annotated with the ``[Tool]`` attribute.
18+
19+
Rule description
20+
----------------
21+
22+
The ``[ExportToolButton]`` is used to create clickable buttons in the inspector so,
23+
like every other script that runs in the editor, it needs to be annotated with the
24+
``[Tool]`` attribute.
25+
26+
.. code-block:: csharp
27+
:emphasize-lines: 1
28+
29+
[Tool]
30+
public partial class MyNode : Node
31+
{
32+
[ExportToolButton("Click me!")]
33+
public Callable ClickMeButton => Callable.From(ClickMe);
34+
35+
private static void ClickMe()
36+
{
37+
GD.Print("Hello world!");
38+
}
39+
}
40+
41+
How to fix violations
42+
---------------------
43+
44+
To fix a violation of this rule, add the ``[Tool]`` attribute to the class that
45+
contains the member annotated with the ``[ExportToolButton]`` attribute.
46+
47+
When to suppress warnings
48+
-------------------------
49+
50+
Do not suppress a warning from this rule. The clickable buttons in the inspector
51+
won't be functional if their script is not annotated with the ``[Tool]`` attribute.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
GD0109: The '[ExportToolButton]' attribute cannot be used with another '[Export]' attribute
2+
===========================================================================================
3+
4+
==================================== ======================================
5+
Value
6+
==================================== ======================================
7+
**Rule ID** GD0109
8+
**Category** Usage
9+
**Fix is breaking or non-breaking** Non-breaking
10+
**Enabled by default** Yes
11+
==================================== ======================================
12+
13+
Cause
14+
-----
15+
16+
A property is annotated with both the ``[ExportToolButton]`` and the ``[Export]``
17+
attributes.
18+
19+
Rule description
20+
----------------
21+
22+
The ``[ExportToolButton]`` attribute already implies exporting the member, so
23+
the ``[Export]`` is unnecessary.
24+
25+
How to fix violations
26+
---------------------
27+
28+
To fix a violation of this rule, remove the ``[Export]`` attribute.
29+
30+
When to suppress warnings
31+
-------------------------
32+
33+
Do not suppress a warning from this rule. Multiple export attributes may lead
34+
to duplicated members, resulting in unexpected runtime errors.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
GD0110: The exported tool button is not a Callable
2+
==================================================
3+
4+
==================================== ======================================
5+
Value
6+
==================================== ======================================
7+
**Rule ID** GD0110
8+
**Category** Usage
9+
**Fix is breaking or non-breaking** Breaking - If the property's type is changed to ``Callable``
10+
11+
Non-breaking - If the ``[ExportToolButton]`` is replaced with ``[Export]``
12+
**Enabled by default** Yes
13+
==================================== ======================================
14+
15+
Cause
16+
-----
17+
18+
A property of a type different from ``Callable`` is annotated with the
19+
``[ExportToolButton]`` attribute.
20+
21+
Rule description
22+
----------------
23+
24+
The ``[ExportToolButton]`` attribute is used to create clickable buttons in the inspector so,
25+
the property must be a ``Callable`` that will be executed when clicking the button.
26+
27+
How to fix violations
28+
---------------------
29+
30+
To fix a violation of this rule, change the type of the property to ``Callable``.
31+
Alternatively, if you intended to export a normal property, replace the
32+
``[ExportToolButton]`` attribute with ``[Export]``.
33+
34+
When to suppress warnings
35+
-------------------------
36+
37+
Do not suppress a warning from this rule. The exported property must be a ``Callable``
38+
so it can executed in the editor when clicking the button in the inspector.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
GD0111: The exported tool button must be an expression-bodied property
2+
======================================================================
3+
4+
==================================== ======================================
5+
Value
6+
==================================== ======================================
7+
**Rule ID** GD0111
8+
**Category** Usage
9+
**Fix is breaking or non-breaking** Non-breaking
10+
**Enabled by default** Yes
11+
==================================== ======================================
12+
13+
Cause
14+
-----
15+
16+
A property is annotated with the ``[ExportToolButton]`` attribute but it's not
17+
an `expression-bodied property`_.
18+
19+
Rule description
20+
----------------
21+
22+
When reloading the .NET assembly, Godot will attempt to serialize exported
23+
members to preserve their values. A field or a property with a backing field
24+
that stores a ``Callable`` may prevent the unloading of the assembly.
25+
26+
An expression-bodied property doesn't have a backing field and won't store
27+
the ``Callable``, so Godot won't attempt to serialize it, which should result
28+
in the successful reloading of the .NET assembly.
29+
30+
.. code-block:: csharp
31+
32+
[ExportToolButton("Click me!")]
33+
public Callable ValidClickMeButton => Callable.From(ClickMe);
34+
35+
// Invalid because the Callable will be stored in the property's backing field.
36+
[ExportToolButton("Click me!")]
37+
public Callable InvalidClickMeButton { get; } = Callable.From(ClickMe);
38+
39+
How to fix violations
40+
---------------------
41+
42+
To fix a violation of this rule, replace the property implementation with an
43+
`expression-bodied property`_.
44+
45+
When to suppress warnings
46+
-------------------------
47+
48+
Do not suppress a warning from this rule. ``Callable`` instances may prevent
49+
the .NET assembly from unloading.
50+
51+
.. _expression-bodied property: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members#properties

tutorials/scripting/c_sharp/diagnostics/index.rst

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ or unsupported code and let you know that something is wrong during build time.
2525
GD0105
2626
GD0106
2727
GD0107
28+
GD0108
29+
GD0109
30+
GD0110
31+
GD0111
2832
GD0201
2933
GD0202
3034
GD0203

0 commit comments

Comments
 (0)