Skip to content

Commit bce716b

Browse files
committed
chore: Signal documentation
1 parent 9c90f0f commit bce716b

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

README.md

+46-9
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,13 @@ See [CONTRIBUTING.md](CONTRIBUTING.md)
4747
# Using
4848

4949
Note there are two ways to use Dart with Godot: as an extension language and as
50-
a Script language. Both are only partially implemented
50+
a Script language. Both are only partially implemented.
5151

5252
## Dart classes as Scripts
5353

54-
Scripts require a little bit more setup, but can then be attached to exiting
55-
nodes, just like any other GDScript. You should be able to create a Dart script
56-
by using the "Attach Script" command in the editor. This will create the
57-
necessary boilerplate for a Dart Script.
54+
Scripts can then be attached to exiting nodes, just like any other GDScript.
55+
You should be able to create a Dart script by using the "Attach Script" command
56+
in the editor. This will create the necessary boilerplate for a Dart script.
5857

5958
While not required, the easiest way to create a scripts is to use `build_runner`
6059
and the `godot_dart_builder` package. After creating your script, run `build_runner`
@@ -119,10 +118,48 @@ void main() {
119118
}
120119
```
121120

121+
### Signals
122+
123+
You can add signals to your script with the `GodotSignal` property. This takes the signal name and a list
124+
of arguments for the signal:
125+
126+
```dart
127+
@GodotScript()
128+
class Hud extends CanvasLayer {
129+
//...
130+
131+
132+
@GodotSignal('start_game')
133+
late final Signal _startGame = Signal.fromObjectSignal(this, 'start_game');
134+
}
135+
```
136+
137+
You can then emit signals with `Signal.emit`.
138+
139+
Classes from Godot support type safe signal subscription for each or their signals. For example, if you
140+
want to subscribe to the `animation_added` signal on `AnimationLibrary`, you can do so like so:
141+
142+
```dart
143+
@GodotScript()
144+
class MyClass extends Node {
145+
@override
146+
void vReady() {
147+
final animationLibrary = getNodeT<AnimationLibrary>('AnimationLibrary');
148+
animationLibrary.animationAdded.connect(this, _animationAdded);
149+
}
150+
151+
void _animationAdded(String name) {
152+
// ...
153+
}
154+
}
155+
```
156+
157+
These signal connections are automatically cleaned up if the target supplied to `connect` is removed.
158+
159+
122160
## Dart classes as Extensions
123161

124-
There are requirements for almost any Godot accessible Dart class. Here's a Simple
125-
example class
162+
Here's a Simple example class that can be used as an extension.
126163

127164
```dart
128165
class Simple extends Sprite2D {
@@ -227,8 +264,8 @@ pos.x = 5;
227264
position = pos;
228265
```
229266

230-
But in my opinion, this defeats the purpose of wrapping properties. Properties should mimic public member variables, and, when they
231-
can't, use methods instead.
267+
But in my opinion, this defeats the purpose of wrapping properties. Properties
268+
should mimic public member variables, and, when they can't, use methods instead.
232269

233270
# Performance
234271

0 commit comments

Comments
 (0)