You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/documentation/getting-started.md
+25-17Lines changed: 25 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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
2
10
3
11
1. Define your JavaScript class and inherit from a Godot class, then export it as the **default** entry:
4
12
5
13
```javascript title="my-sprite.mjs"
6
14
// The default export entry is treated as an exported class to Godot
7
15
exportdefaultclassMySpriteextendsgodot.Sprite {
8
-
// this is _init() in GDScript
9
-
constructor() {
10
-
super();
11
-
}
16
+
// this is _init() in GDScript
17
+
constructor() {
18
+
super();
19
+
}
12
20
13
-
_ready() {}
21
+
_ready() {}
14
22
15
-
_process(delta) {}
23
+
_process(delta) {}
16
24
}
17
25
```
18
26
19
27
2. Save the script with extension `.mjs`
20
28
3. Attach the script file to the node or resource object like you do with GDScript
21
29
22
-
###How to export signals
30
+
## How to export signals
23
31
24
32
```javascript title="my-sprite.mjs"
25
33
exportdefaultclassMySpriteextendsgodot.Sprite {}
26
34
// register game_over signal to MySprite class
27
35
godot.register_signal(MySprite, "game_over");
28
36
```
29
37
30
-
###How to export properties
38
+
## How to export properties
31
39
32
40
```javascript title="my-sprite.mjs"
33
41
exportdefaultclassMySpriteextendsgodot.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
+
}
38
46
}
39
47
// export 'direction' properties to MySprite Godot inspector
Copy file name to clipboardExpand all lines: docs/documentation/gotchas.md
+2-28Lines changed: 2 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,31 +2,6 @@
2
2
3
3
Some common mistakes and limitations.
4
4
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 { defaultasdayjs } from"dayjs";
13
-
exportdefault { dayjs };
14
-
```
15
-
16
-
In your class you can use the dependency like this:
17
-
18
-
```ts title="main.ts"
19
-
importnpmfrom"./npm-modules.bundle";
20
-
21
-
exportdefaultclassMainextendsgodot.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
-
30
5
## Position.x is immutable
31
6
32
7
You cannot change `this.position.x` try to change `this.position`:
@@ -46,13 +21,12 @@ export default class Player extends godot.KinematicBody2D {
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).
[`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.
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
+

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:
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
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 { defaultasdayjs } 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
+
exportdefaultclassMyClassextendsNode {
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.
0 commit comments