@defold-typescript/types 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/lifecycle.ts CHANGED
@@ -1,41 +1,147 @@
1
1
  import type { Hash, Url } from "./core-types";
2
2
 
3
+ // Field docs are hand-reconciled from the `on_input` description's "Touch input
4
+ // table:" prose (not structured ref-doc data); a future ref-doc re-pin is
5
+ // reconciled by hand, drift-guarded against that prose in lifecycle-member-docs.
3
6
  export interface InputTouch {
7
+ /**
8
+ * A number identifying the touch input during its duration.
9
+ */
4
10
  id?: number;
11
+ /**
12
+ * True if the finger was pressed this frame.
13
+ */
5
14
  pressed?: boolean;
15
+ /**
16
+ * True if the finger was released this frame.
17
+ */
6
18
  released?: boolean;
19
+ /**
20
+ * Number of taps, one for single, two for double-tap, etc
21
+ */
7
22
  tap_count?: number;
23
+ /**
24
+ * The x touch location.
25
+ */
8
26
  x?: number;
27
+ /**
28
+ * The y touch location.
29
+ */
9
30
  y?: number;
31
+ /**
32
+ * The change in x value.
33
+ */
10
34
  dx?: number;
35
+ /**
36
+ * The change in y value.
37
+ */
11
38
  dy?: number;
39
+ /**
40
+ * Accelerometer x value (if present).
41
+ */
12
42
  acc_x?: number;
43
+ /**
44
+ * Accelerometer y value (if present).
45
+ */
13
46
  acc_y?: number;
47
+ /**
48
+ * Accelerometer z value (if present).
49
+ */
14
50
  acc_z?: number;
15
51
  }
16
52
 
53
+ // Field docs are hand-reconciled from the `on_input` description's main action
54
+ // and "Gamepad specific fields:" prose (not structured ref-doc data); a future
55
+ // ref-doc re-pin is reconciled by hand, drift-guarded against that prose in
56
+ // lifecycle-member-docs.
17
57
  export interface InputAction {
58
+ /**
59
+ * The amount of input given by the user. This is usually 1 for buttons and 0-1 for analogue inputs. This is not present for mouse movement and text input.
60
+ */
18
61
  value?: number;
62
+ /**
63
+ * If the input was pressed this frame. This is not present for mouse movement and text input.
64
+ */
19
65
  pressed?: boolean;
66
+ /**
67
+ * If the input was released this frame. This is not present for mouse movement and text input.
68
+ */
20
69
  released?: boolean;
70
+ /**
71
+ * If the input was repeated this frame. This is similar to how a key on a keyboard is repeated when you hold it down. This is not present for mouse movement and text input.
72
+ */
21
73
  repeated?: boolean;
74
+ /**
75
+ * The x value of a pointer device, if present. This is not present for gamepad, key and text input.
76
+ */
22
77
  x?: number;
78
+ /**
79
+ * The y value of a pointer device, if present. This is not present for gamepad, key and text input.
80
+ */
23
81
  y?: number;
82
+ /**
83
+ * The screen space x value of a pointer device, if present. This is not present for gamepad, key and text input.
84
+ */
24
85
  screen_x?: number;
86
+ /**
87
+ * The screen space y value of a pointer device, if present. This is not present for gamepad, key and text input.
88
+ */
25
89
  screen_y?: number;
90
+ /**
91
+ * The change in x value of a pointer device, if present. This is not present for gamepad, key and text input.
92
+ */
26
93
  dx?: number;
94
+ /**
95
+ * The change in y value of a pointer device, if present. This is not present for gamepad, key and text input.
96
+ */
27
97
  dy?: number;
98
+ /**
99
+ * The change in screen space x value of a pointer device, if present. This is not present for gamepad, key and text input.
100
+ */
28
101
  screen_dx?: number;
102
+ /**
103
+ * The change in screen space y value of a pointer device, if present. This is not present for gamepad, key and text input.
104
+ */
29
105
  screen_dy?: number;
106
+ /**
107
+ * The index of the gamepad device that provided the input. See table below about gamepad input.
108
+ */
30
109
  gamepad?: number;
110
+ /**
111
+ * Id of the user associated with the controller. Usually only relevant on consoles.
112
+ */
31
113
  userid?: number;
114
+ /**
115
+ * True if the inout originated from an unknown/unmapped gamepad.
116
+ */
32
117
  gamepad_unknown?: boolean;
118
+ /**
119
+ * Name of the gamepad
120
+ */
33
121
  gamepad_name?: string;
122
+ /**
123
+ * List of gamepad axis values. For raw gamepad input only.
124
+ */
34
125
  gamepad_axis?: number[];
126
+ /**
127
+ * List of gamepad hat values. For raw gamepad input only.
128
+ */
35
129
  gamepadhats?: number[];
130
+ /**
131
+ * List of gamepad button values. For raw gamepad input only.
132
+ */
36
133
  gamepad_buttons?: number[];
134
+ /**
135
+ * List of touch input, one element per finger, if present. See table below about touch input
136
+ */
37
137
  touch?: InputTouch[];
138
+ /**
139
+ * Text input from a (virtual) keyboard or similar.
140
+ */
38
141
  text?: string;
142
+ /**
143
+ * Sequence of entered symbols while entering a symbol combination, for example Japanese Kana.
144
+ */
39
145
  marked_text?: string;
40
146
  }
41
147
 
@@ -64,26 +170,265 @@ export interface ScriptHooks<TSelf, TInitState = TSelf> {
64
170
  // solves `TInitState` from. The engine owns `self` (a userdata-backed table),
65
171
  // so every other hook wraps it in `NoInfer<TSelf>` — otherwise their `self`
66
172
  // competes as a second inference site and `TSelf` collapses to `{}`.
173
+ /**
174
+ * This is a callback-function, which is called by the engine when a script component is initialized. It can be used
175
+ * to set the initial state of the script.
176
+ *
177
+ * @example
178
+ * ```ts
179
+ * init() {
180
+ * return { hits: 0 };
181
+ * },
182
+ * ```
183
+ */
67
184
  init?(): TInitState;
185
+ /**
186
+ * This is a callback-function, which is called by the engine every frame to update the state of a script component.
187
+ * It can be used to perform any kind of game related tasks, e.g. moving the game object instance.
188
+ *
189
+ * @param self - reference to the script state to be used for storing data
190
+ * @param dt - the time-step of the frame update
191
+ * @example
192
+ * ```ts
193
+ * update(self, dt) {
194
+ * self.hits += 1;
195
+ * },
196
+ * ```
197
+ */
68
198
  update?(self: NoInfer<TSelf>, dt: number): void;
199
+ /**
200
+ * This is a callback-function, which is called by the engine at fixed intervals to update the state of a script
201
+ * component. The function will be called if 'Fixed Update Frequency' is enabled in the Engine section of game.project.
202
+ * It can for instance be used to update game logic with the physics simulation if using a fixed timestep for the
203
+ * physics (enabled by ticking 'Use Fixed Timestep' in the Physics section of game.project).
204
+ *
205
+ * @param self - reference to the script state to be used for storing data
206
+ * @param dt - the time-step of the frame update
207
+ * @example
208
+ * ```ts
209
+ * fixed_update(self, dt) {
210
+ * self.vel.y -= 9.8 * dt;
211
+ * },
212
+ * ```
213
+ */
69
214
  fixed_update?(self: NoInfer<TSelf>, dt: number): void;
215
+ /**
216
+ * This is a callback-function, which is called by the engine at the end of the frame to update the state of a script
217
+ * component. Use it to make final adjustments to the game object instance.
218
+ *
219
+ * @param self - reference to the script state to be used for storing data
220
+ * @param dt - the time-step of the frame update
221
+ * @example
222
+ * ```ts
223
+ * late_update(self, dt) {
224
+ * self.camera = self.target;
225
+ * },
226
+ * ```
227
+ */
70
228
  late_update?(self: NoInfer<TSelf>, dt: number): void;
71
229
  // Defold delivers message_id as a pre-hashed `hash`, so handlers must compare
72
230
  // it against `hash("...")` constants — a string literal never matches. Sender-
73
231
  // side payload narrowing by message id lives on `msg.post` (msg-overloads.d.ts).
232
+ /**
233
+ * This is a callback-function, which is called by the engine whenever a message has been sent to the script component.
234
+ * It can be used to take action on the message, e.g. send a response back to the sender of the message.
235
+ * The `message` parameter is a table containing the message data. If the message is sent from the engine, the
236
+ * documentation of the message specifies which data is supplied.
237
+ *
238
+ * @param self - reference to the script state to be used for storing data
239
+ * @param message_id - id of the received message
240
+ * @param message - a table containing the message data
241
+ * @param sender - address of the sender
242
+ * @example
243
+ * ```ts
244
+ * on_message(self, message_id, message, sender) {
245
+ * if (message_id === hash("hit")) self.hits += 1;
246
+ * },
247
+ * ```
248
+ */
74
249
  on_message?(
75
250
  self: NoInfer<TSelf>,
76
251
  message_id: Hash,
77
252
  message: Record<string | number, unknown>,
78
253
  sender: Url,
79
254
  ): void;
255
+ /**
256
+ * This is a callback-function, which is called by the engine when user input is sent to the game object instance of the script.
257
+ * It can be used to take action on the input, e.g. move the instance according to the input.
258
+ * For an instance to obtain user input, it must first acquire input focus
259
+ * through the message `acquire_input_focus`.
260
+ * Any instance that has obtained input will be put on top of an
261
+ * input stack. Input is sent to all listeners on the stack until the
262
+ * end of stack is reached, or a listener returns `true`
263
+ * to signal that it wants input to be consumed.
264
+ * See the documentation of acquire_input_focus for more
265
+ * information.
266
+ * The `action` parameter is a table containing data about the input mapped to the
267
+ * `action_id`.
268
+ * For mapped actions it specifies the value of the input and if it was just pressed or released.
269
+ * Actions are mapped to input in an input_binding-file.
270
+ * Mouse movement is specifically handled and uses `nil` as its `action_id`.
271
+ * The `action` only contains positional parameters in this case, such as x and y of the pointer.
272
+ * Here is a brief description of the available table fields:
273
+ *
274
+ * Field
275
+ * Description
276
+ *
277
+ * `value`
278
+ * The amount of input given by the user. This is usually 1 for buttons and 0-1 for analogue inputs. This is not present for mouse movement and text input.
279
+ *
280
+ * `pressed`
281
+ * If the input was pressed this frame. This is not present for mouse movement and text input.
282
+ *
283
+ * `released`
284
+ * If the input was released this frame. This is not present for mouse movement and text input.
285
+ *
286
+ * `repeated`
287
+ * If the input was repeated this frame. This is similar to how a key on a keyboard is repeated when you hold it down. This is not present for mouse movement and text input.
288
+ *
289
+ * `x`
290
+ * The x value of a pointer device, if present. This is not present for gamepad, key and text input.
291
+ *
292
+ * `y`
293
+ * The y value of a pointer device, if present. This is not present for gamepad, key and text input.
294
+ *
295
+ * `screen_x`
296
+ * The screen space x value of a pointer device, if present. This is not present for gamepad, key and text input.
297
+ *
298
+ * `screen_y`
299
+ * The screen space y value of a pointer device, if present. This is not present for gamepad, key and text input.
300
+ *
301
+ * `dx`
302
+ * The change in x value of a pointer device, if present. This is not present for gamepad, key and text input.
303
+ *
304
+ * `dy`
305
+ * The change in y value of a pointer device, if present. This is not present for gamepad, key and text input.
306
+ *
307
+ * `screen_dx`
308
+ * The change in screen space x value of a pointer device, if present. This is not present for gamepad, key and text input.
309
+ *
310
+ * `screen_dy`
311
+ * The change in screen space y value of a pointer device, if present. This is not present for gamepad, key and text input.
312
+ *
313
+ * `gamepad`
314
+ * The index of the gamepad device that provided the input. See table below about gamepad input.
315
+ *
316
+ * `touch`
317
+ * List of touch input, one element per finger, if present. See table below about touch input
318
+ *
319
+ * `text`
320
+ * Text input from a (virtual) keyboard or similar.
321
+ *
322
+ * `marked_text`
323
+ * Sequence of entered symbols while entering a symbol combination, for example Japanese Kana.
324
+ *
325
+ * Gamepad specific fields:
326
+ *
327
+ * Field
328
+ * Description
329
+ *
330
+ * `gamepad`
331
+ * The index of the gamepad device that provided the input.
332
+ *
333
+ * `userid`
334
+ * Id of the user associated with the controller. Usually only relevant on consoles.
335
+ *
336
+ * `gamepad_unknown`
337
+ * True if the inout originated from an unknown/unmapped gamepad.
338
+ *
339
+ * `gamepad_name`
340
+ * Name of the gamepad
341
+ *
342
+ * `gamepad_axis`
343
+ * List of gamepad axis values. For raw gamepad input only.
344
+ *
345
+ * `gamepadhats`
346
+ * List of gamepad hat values. For raw gamepad input only.
347
+ *
348
+ * `gamepad_buttons`
349
+ * List of gamepad button values. For raw gamepad input only.
350
+ *
351
+ * Touch input table:
352
+ *
353
+ * Field
354
+ * Description
355
+ *
356
+ * `id`
357
+ * A number identifying the touch input during its duration.
358
+ *
359
+ * `pressed`
360
+ * True if the finger was pressed this frame.
361
+ *
362
+ * `released`
363
+ * True if the finger was released this frame.
364
+ *
365
+ * `tap_count`
366
+ * Number of taps, one for single, two for double-tap, etc
367
+ *
368
+ * `x`
369
+ * The x touch location.
370
+ *
371
+ * `y`
372
+ * The y touch location.
373
+ *
374
+ * `dx`
375
+ * The change in x value.
376
+ *
377
+ * `dy`
378
+ * The change in y value.
379
+ *
380
+ * `acc_x`
381
+ * Accelerometer x value (if present).
382
+ *
383
+ * `acc_y`
384
+ * Accelerometer y value (if present).
385
+ *
386
+ * `acc_z`
387
+ * Accelerometer z value (if present).
388
+ *
389
+ * @param self - reference to the script state to be used for storing data
390
+ * @param action_id - id of the received input action, as mapped in the input_binding-file
391
+ * @param action - a table containing the input data, see above for a description
392
+ * @example
393
+ * ```ts
394
+ * on_input(self, action_id, action) {
395
+ * if (action_id === hash("left") && action.pressed) self.dir = -1;
396
+ * return false;
397
+ * },
398
+ * ```
399
+ */
80
400
  on_input?(
81
401
  self: NoInfer<TSelf>,
82
402
  action_id: Hash | undefined,
83
403
  action: InputAction,
84
404
  // biome-ignore lint/suspicious/noConfusingVoidType: Defold lets handlers omit the return; `void` is the right shape for "may return boolean or nothing".
85
405
  ): boolean | void;
406
+ /**
407
+ * This is a callback-function, which is called by the engine when a script component is finalized (destroyed). It can
408
+ * be used to e.g. take some last action, report the finalization to other game object instances, delete spawned objects
409
+ * or release user input focus (see release_input_focus).
410
+ *
411
+ * @param self - reference to the script state to be used for storing data
412
+ * @example
413
+ * ```ts
414
+ * final(self) {
415
+ * msg.post("#", "done");
416
+ * },
417
+ * ```
418
+ */
86
419
  final?(self: NoInfer<TSelf>): void;
420
+ /**
421
+ * This is a callback-function, which is called by the engine when the script component is reloaded, e.g. from the editor.
422
+ * It can be used for live development, e.g. to tweak constants or set up the state properly for the instance.
423
+ *
424
+ * @param self - reference to the script state to be used for storing data
425
+ * @example
426
+ * ```ts
427
+ * on_reload(self) {
428
+ * self.speed = 200;
429
+ * },
430
+ * ```
431
+ */
87
432
  on_reload?(self: NoInfer<TSelf>): void;
88
433
  }
89
434
 
@@ -108,7 +453,10 @@ const _hookNamesPinnedToInterface: Equal<ScriptHookName, keyof ScriptHooks<unkno
108
453
  true;
109
454
  void _hookNamesPinnedToInterface;
110
455
 
111
- export type GuiScriptHooks<TSelf, TInitState = TSelf> = ScriptHooks<TSelf, TInitState>;
456
+ export type GuiScriptHooks<TSelf, TInitState = TSelf> = Omit<
457
+ ScriptHooks<TSelf, TInitState>,
458
+ "fixed_update" | "late_update"
459
+ >;
112
460
 
113
461
  export type RenderScriptHooks<TSelf, TInitState = TSelf> = Omit<
114
462
  ScriptHooks<TSelf, TInitState>,
@@ -132,7 +480,17 @@ export type ScriptHooksWithProperties<TProps, TSelf, TInitState> = Omit<
132
480
  ScriptHooks<TSelf, TInitState>,
133
481
  "init"
134
482
  > & {
483
+ /**
484
+ * Receives `self` pre-populated with the declared `properties` before the
485
+ * engine runs init, so init-time setup can read those default values. The
486
+ * returned object still seeds the inferred script state.
487
+ */
135
488
  init?(self: NoInfer<TProps>): TInitState;
489
+ /**
490
+ * Value-keyed editor properties: each key is a property name and its value
491
+ * the default, so the value's type threads onto `self` alongside init's
492
+ * returned state.
493
+ */
136
494
  properties?: TProps;
137
495
  };
138
496
 
@@ -140,7 +498,17 @@ export type GuiScriptHooksWithProperties<TProps, TSelf, TInitState> = Omit<
140
498
  GuiScriptHooks<TSelf, TInitState>,
141
499
  "init"
142
500
  > & {
501
+ /**
502
+ * Receives `self` pre-populated with the declared `properties` before the
503
+ * engine runs init, so init-time setup can read those default values. The
504
+ * returned object still seeds the inferred script state.
505
+ */
143
506
  init?(self: NoInfer<TProps>): TInitState;
507
+ /**
508
+ * Value-keyed editor properties: each key is a property name and its value
509
+ * the default, so the value's type threads onto `self` alongside init's
510
+ * returned state.
511
+ */
144
512
  properties?: TProps;
145
513
  };
146
514
 
@@ -148,7 +516,17 @@ export type RenderScriptHooksWithProperties<TProps, TSelf, TInitState> = Omit<
148
516
  RenderScriptHooks<TSelf, TInitState>,
149
517
  "init"
150
518
  > & {
519
+ /**
520
+ * Receives `self` pre-populated with the declared `properties` before the
521
+ * engine runs init, so init-time setup can read those default values. The
522
+ * returned object still seeds the inferred script state.
523
+ */
151
524
  init?(self: NoInfer<TProps>): TInitState;
525
+ /**
526
+ * Value-keyed editor properties: each key is a property name and its value
527
+ * the default, so the value's type threads onto `self` alongside init's
528
+ * returned state.
529
+ */
152
530
  properties?: TProps;
153
531
  };
154
532
 
@@ -211,9 +589,10 @@ export function defineScript<TProps extends object = Record<never, never>, TInit
211
589
  * {@link defineScript}, and is erased by the transpiler's `lifecycle-erasure`
212
590
  * pass into the flat Defold chunk shape.
213
591
  *
214
- * `GuiScriptHooks` is an alias of the `.script` hook set, so it accepts the same
215
- * full set, all optional: `init`, `update`, `fixed_update`, `late_update`,
216
- * `on_message`, `on_input`, `final`, `on_reload`.
592
+ * `GuiScriptHooks` is `Omit<ScriptHooks, "fixed_update" | "late_update">` gui
593
+ * scripts are not driven by the fixed-timestep or late-update passes. It accepts
594
+ * the rest, all optional: `init`, `update`, `on_message`, `on_input`, `final`,
595
+ * `on_reload`.
217
596
  *
218
597
  * Scaffold it with the `defold-gui` / `defold-gui-typed` VSCode snippets from
219
598
  * `defold-typescript init`.
@@ -0,0 +1,48 @@
1
+ /** @noSelfInFile */
2
+
3
+ // `socket-types.d.ts` ships the standalone type aliases the pinned
4
+ // ts-defold-types fixture (commit 4f0672a, Defold 1.12.4) declares inside
5
+ // its second `namespace socket` block (fixture lines 11124-11148). The
6
+ // receiver-interface methods in `generated/socket.d.ts` keep their fixture-
7
+ // equivalent `string` / `number` parameter types — these aliases exist for
8
+ // consumer code to spell out the literal sets explicitly:
9
+ //
10
+ // const opt: socket.TCPOptions = 'keepalive';
11
+ // // TCP/UDP are convenience intersections for consumer annotations;
12
+ // // no socket.* function returns them (tcp() yields a `master` handle).
13
+ //
14
+ // The literal unions are LuaSocket-domain, not engine-ref-doc, so they live
15
+ // here rather than in the regen pipeline.
16
+
17
+ declare global {
18
+ namespace socket {
19
+ /** A TCP handle in any of its three lifecycle roles. */
20
+ type TCP = client & master & server;
21
+ /** Option names accepted by a TCP handle's `setoption`. */
22
+ type TCPOptions = "ipv6-v6only" | "keepalive" | "linger" | "reuseaddr" | "tcp-nodelay";
23
+ /** Error string a TCP `receive` may return. */
24
+ type TCPReceiveError = "closed" | "timeout";
25
+ /** Read pattern accepted by a TCP `receive`. */
26
+ type TCPReceivePattern = number | "*a" | "*l";
27
+ /** Direction passed to a TCP handle's `shutdown`. */
28
+ type TCPShutdownMode = "both" | "receive" | "send";
29
+ /** Blocking mode passed to a TCP handle's `settimeout`. */
30
+ type TCPTimeoutMode = "b" | "t";
31
+ /** A UDP handle in either of its two lifecycle roles. */
32
+ type UDP = connected & unconnected;
33
+ /** Option names accepted by a UDP handle's `setoption`. */
34
+ type UDPOptions =
35
+ | "broadcast"
36
+ | "dontroute"
37
+ | "ip-add-membership"
38
+ | "ip-drop-membership"
39
+ | "ip-multicast-if"
40
+ | "ip-multicast-loop"
41
+ | "ip-multicast-ttl"
42
+ | "ipv6-v6only"
43
+ | "reuseaddr"
44
+ | "reuseport";
45
+ }
46
+ }
47
+
48
+ export {};