@@ -47,14 +47,13 @@ See [CONTRIBUTING.md](CONTRIBUTING.md)
47
47
# Using
48
48
49
49
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.
51
51
52
52
## Dart classes as Scripts
53
53
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.
58
57
59
58
While not required, the easiest way to create a scripts is to use ` build_runner `
60
59
and the ` godot_dart_builder ` package. After creating your script, run ` build_runner `
@@ -119,10 +118,48 @@ void main() {
119
118
}
120
119
```
121
120
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
+
122
160
## Dart classes as Extensions
123
161
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.
126
163
127
164
``` dart
128
165
class Simple extends Sprite2D {
@@ -227,8 +264,8 @@ pos.x = 5;
227
264
position = pos;
228
265
```
229
266
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.
232
269
233
270
# Performance
234
271
0 commit comments