Skip to content

Commit 80fc54e

Browse files
authored
docs: update docs for typescript (#6)
1 parent 3828265 commit 80fc54e

11 files changed

+198
-134
lines changed

docs/css/extra.css

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/documentation/getting-started.md

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,48 @@
1-
### How to export script class to Godot
1+
[Download the editor](https://github.com/godotjs/javascript/releases) and start the application
2+
3+
Recommended for TypeScript:
4+
5+
- Rename the downloaded file to `godot` and [add Godot to your path](https://docs.godotengine.org/en/stable/tutorials/editor/command_line_tutorial.html#path)
6+
- Open a terminal
7+
- Test if you can use Godot via terminal and run `godot --version`
8+
9+
## How to export script class to Godot
210

311
1. Define your JavaScript class and inherit from a Godot class, then export it as the **default** entry:
412

513
```javascript title="my-sprite.mjs"
614
// The default export entry is treated as an exported class to Godot
715
export default class MySprite extends godot.Sprite {
8-
// this is _init() in GDScript
9-
constructor() {
10-
super();
11-
}
16+
// this is _init() in GDScript
17+
constructor() {
18+
super();
19+
}
1220

13-
_ready() {}
21+
_ready() {}
1422

15-
_process(delta) {}
23+
_process(delta) {}
1624
}
1725
```
1826

1927
2. Save the script with extension `.mjs`
2028
3. Attach the script file to the node or resource object like you do with GDScript
2129

22-
### How to export signals
30+
## How to export signals
2331

2432
```javascript title="my-sprite.mjs"
2533
export default class MySprite extends godot.Sprite {}
2634
// register game_over signal to MySprite class
2735
godot.register_signal(MySprite, "game_over");
2836
```
2937

30-
### How to export properties
38+
## How to export properties
3139

3240
```javascript title="my-sprite.mjs"
3341
export default class MySprite extends godot.Sprite {
34-
_process(delta) {
35-
// Yes! We can use operators in JavaScript like GDScript
36-
this.position += this.direction * delta;
37-
}
42+
_process(delta) {
43+
// Yes! We can use operators in JavaScript like GDScript
44+
this.position += this.direction * delta;
45+
}
3846
}
3947
// export 'direction' properties to MySprite Godot inspector
4048
godot.register_property(MySprite, "direction", new godot.Vector2(1, 0));
@@ -57,10 +65,10 @@ Is the simplified version of:
5765

5866
```js
5967
godot.register_property(MyClass, "number_value", {
60-
type: godot.TYPE_REAL,
61-
hint: godot.PropertyHint.PROPERTY_HINT_NONE,
62-
hint_string: "",
63-
default: 3.14,
68+
type: godot.TYPE_REAL,
69+
hint: godot.PropertyHint.PROPERTY_HINT_NONE,
70+
hint_string: "",
71+
default: 3.14,
6472
});
6573
```
6674

docs/documentation/gotchas.md

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,6 @@
22

33
Some common mistakes and limitations.
44

5-
## Import dependency from `node_modules`
6-
7-
If you use `TypeScript` you may encounter the problem where dependencies from `node_modules` are not bundled correctly.
8-
9-
As a workaround you can create a new file `npm-modules.bundle.ts`:
10-
11-
```ts title="npm-modules.bundle.ts"
12-
import { default as dayjs } from "dayjs";
13-
export default { dayjs };
14-
```
15-
16-
In your class you can use the dependency like this:
17-
18-
```ts title="main.ts"
19-
import npm from "./npm-modules.bundle";
20-
21-
export default class Main extends godot.Node {
22-
_ready(): void {
23-
console.log(npm.dayjs().toString());
24-
}
25-
}
26-
```
27-
28-
With a bundler like `esbuild` you should build the `npm-modules.bundle.ts` with the `--bundle` option, but all the other classes like `main.ts` without it.
29-
305
## Position.x is immutable
316

327
You cannot change `this.position.x` try to change `this.position`:
@@ -46,13 +21,12 @@ export default class Player extends godot.KinematicBody2D {
4621
godot.register_property(Player, "direction", new godot.Vector2(1, 0));
4722
```
4823

49-
## ``register_property`` has to be a target
24+
## `register_property` has to be a target
5025

5126
You cannot change `this.position.x` try to change `this.position`:
5227

5328
```javascript title="player.mjs"
54-
export default class Player extends godot.KinematicBody2D {
55-
}
29+
export default class Player extends godot.KinematicBody2D {}
5630
// This works
5731
godot.register_property(Player, "directionWorks", new godot.Vector2(1, 0));
5832
// This breaks because `player` isn't a correct target

docs/documentation/typescript.md

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
One feature of GodotTS is that your can use decorators for adding `register` functions to your class like [signals](../getting-started.md#how-to-export-signals).
2+
3+
Most of the `register` functions are available as various decorators as seen below. Check the decorators in `src/decorators.bundle` generated in [intro](intro.md).
4+
5+
```ts
6+
import { signal, property, tool, onready, node } from "./decorators.bundle";
7+
8+
@tool // make the script runnable in godot editor
9+
export default class InputLine extends godot.HBoxContainer {
10+
// define a signal
11+
@signal
12+
static readonly OnTextChanged: string;
13+
14+
// expose a node property
15+
@node
16+
icon: godot.Sprite;
17+
18+
// register offset property with the godot inspector with default value of Vector2(0, 0)
19+
@property({ default: godot.Vector2.ZERO })
20+
offset: godot.Vector2;
21+
22+
// register properties for godot editor inspector
23+
@property({ type: godot.VariantType.TYPE_STRING })
24+
get title() {
25+
return this._title;
26+
}
27+
set title(v: string) {
28+
this._title = v;
29+
if (this._label) {
30+
this._label.text = v;
31+
}
32+
}
33+
private _title: string;
34+
35+
@property({ default: "Input text here" })
36+
get hint() {
37+
return this._hint;
38+
}
39+
set hint(v: string) {
40+
this._hint = v;
41+
if (this.edit) {
42+
this.edit.hint_tooltip = v;
43+
this.edit.placeholder_text = v;
44+
}
45+
}
46+
private _hint: string;
47+
48+
get label(): godot.Label {
49+
return this._label;
50+
}
51+
protected _label: godot.Label;
52+
53+
// call get_node('LineEdit') and assign the returned value to 'this.edit' automatically when the node is ready
54+
@onready("LineEdit")
55+
edit: godot.LineEdit;
56+
57+
get text(): string {
58+
return this.edit?.text;
59+
}
60+
61+
_ready() {
62+
// get first child with the type of godot.Label
63+
this._label = this.get_node(godot.Label);
64+
65+
// Apply the inspector filled values with property setters
66+
this.title = this.title;
67+
this.hint = this.hint;
68+
69+
this.edit.connect(godot.LineEdit.text_changed, (text: string) => {
70+
this.emit_signal(InputLine.OnTextChanged, text);
71+
});
72+
}
73+
}
74+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[`godot-ts`](https://github.com/godotjs/godot-ts) is a cli tool for using GodotJS with Typescript.
2+
3+
See the [Intro](intro.md) how you initialize a new GodotTS project.
4+
5+
Afterward, you can run ``godot-ts --help`` to see additional helping.
6+
7+
## Further information
8+
9+
The cli tool handles the compilation of `.ts` files into `.mjs` files.
10+
11+
There are two commands `godot-ts build` & `godot-ts watch` which compile those files. Use `build` to compile your files once for production (with minification) and `watch` for developing.
12+
13+
Both commands will compile every `*.ts` file as it is (without bundling), to remain the required class structures for GodotJS.
14+
15+
Sometimes you require to bundle a `*.ts` file e.g. if you need something from `node_modules` ([example](npm-modules.md)).
16+
In this case name your file `*.bundle.ts`, this won't generate a `.mjs` file, instead it generates a `.js` file as bundled code.
Loading
Loading
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
1. [Download the editor](https://github.com/godotjs/javascript/releases)
2+
2. Rename the downloaded file to `godot` and [add Godot to your path](https://docs.godotengine.org/en/stable/tutorials/editor/command_line_tutorial.html#path)
3+
3. Open a terminal
4+
4. Test if you can use Godot via CLI and run `godot --version`
5+
5. Run `npx -y @godot-js/godot-ts init` (new project will be crated at your current terminal path)
6+
6. Follow the prompts
7+
7. Run `cd <your-project>`
8+
8. Run `npm i`
9+
9. Run `npm run dev` - this will enable typescript watch mode and opens the editor
10+
10. Run the menu command inside the editor `Project > Tools > JavaScript > Generate TypeScript Declaration File` and overwrite file `godot.d.ts`
11+
12+
## Features
13+
14+
- By running `npm run dev` you compile your `.ts` files into `.mjs` automatically
15+
- Use the generated `.mjs` files inside your editor
16+
- To open the `.ts` origin file for a `.mjs` file you need to enable [External Editor](#open-scripts-from-the-editor)
17+
- If you want to start the game without editor run `npm run start`
18+
- For more information check out [`godot-ts`](godot-ts.md)
19+
20+
## Open scripts from the editor
21+
22+
Inside the editor you are normally able to open a script inside the build in text editor.
23+
24+
![Open script](images/open-script.png)
25+
26+
But we use a compiled `.mjs` file for our scripts.
27+
`.ts` files can't be open in the editor.
28+
29+
So we need to open the `.ts` files inside an external editor:
30+
31+
1. Goto `Editor/Editor Settings/Text Editor/External`
32+
2. Check the `Use External Editor`
33+
3. Add your path your desired editor to `Exec Path` - Check [this](https://docs.godotengine.org/en/stable/tutorials/editor/external_editor.html) for more information
34+
4. Paste `{project} --line {line} {file}` (JetBrains products) to `Exec Flags`
35+
5. If you click on a `.mjs` script it should read the `banner` on top of the file like `//generatedPath=<absolute-path>.ts` and opens this file
36+
37+
![enable external editor](images/enable-external-editor.png)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
You can import ES5/ES6 modules installed from e.g. `npm`. But you need to bundle them first.
2+
3+
We recommend that you create a `npm.bundle.ts` file where you locate all of your dependencies.
4+
5+
## Example
6+
7+
Install dependency:
8+
9+
```shell title="terminal"
10+
npm i dayjs
11+
```
12+
13+
Export the dependency from your `npm.bundle.ts` file.
14+
15+
```ts title="npm.bundle.ts"
16+
export { default as dayjs } from "dayjs";
17+
```
18+
19+
Import the dependency in another class
20+
21+
```ts title="my-class.ts"
22+
import { dayjs } from "./npm-modules.bundle";
23+
24+
export default class MyClass extends Node {
25+
_ready(): void {
26+
console.log(dayjs().toString());
27+
}
28+
}
29+
```
30+
31+
> **Note:** If you use `godot-ts build/watch` the `npm.bundle.ts` file will be bundled with every dependency from `node_modules`, while regular `*.ts` files will preserve their imports. This is required for GodotJS to work.

mkdocs.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ extra_css:
88
nav:
99
- Home: index.md
1010
- Docs:
11-
- Getting Started: documentation/getting-started.md
12-
- API: documentation/api.md
13-
- TypeScript: documentation/typescript.md
14-
- Gotchas: documentation/gotchas.md
11+
- Getting Started: documentation/getting-started.md
12+
- API: documentation/api.md
13+
- TypeScript:
14+
- Intro: documentation/typescript/intro.md
15+
- GodotTS: documentation/typescript/godot-ts.md
16+
- Decorators: documentation/typescript/decorators.md
17+
- NPM Modules: documentation/typescript/npm-modules.md
18+
- Gotchas: documentation/gotchas.md
1519
- Examples:
1620
- Load JSON in Singleton: examples/load-json-in-singleton.md
1721
- Read file local: examples/read-file-local.md

0 commit comments

Comments
 (0)