Skip to content

Commit 587b9ae

Browse files
committed
Initial commit
0 parents  commit 587b9ae

13 files changed

+703
-0
lines changed

Diff for: .github/workflows/gh_pages.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: 📑 GitHub Pages
2+
on:
3+
push:
4+
branches:
5+
- "main"
6+
7+
permissions:
8+
contents: write
9+
10+
jobs:
11+
deploy:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
- uses: actions/setup-python@v4
16+
with:
17+
python-version: 3.x
18+
- run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV
19+
- uses: actions/cache@v3
20+
with:
21+
key: mkdocs-material-${{ env.cache_id }}
22+
path: .cache
23+
restore-keys: |
24+
mkdocs-material-
25+
- run: pip install mkdocs-material
26+
- run: mkdocs gh-deploy --force

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

Diff for: README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Documentation for GodotJS
2+
3+
This project uses [Material for MkDocs](https://squidfunk.github.io/mkdocs-material/) to serve all docs.
4+
5+
6+
## Install
7+
8+
````pycon
9+
pip install mkdocs-material
10+
````
11+
12+
## Start
13+
14+
````shell
15+
mkdocs serve
16+
````
17+
18+
Goto: ``http://localhost:8000``
19+
20+
## Contribute
21+
You can change the documentation inside the `docs` folder.
22+
To add new pages you need to update `mkdocs.yml`.
23+
24+

Diff for: docs/assets/images/favicon.ico

15 KB
Binary file not shown.

Diff for: docs/documentation/api.md

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
All of Godot's APIs are defined within the `godot` namespace.
2+
3+
No API names have been renamed or changed, so you shouldn't need to change your habits.
4+
5+
| GDScript | JavaScript |
6+
| ---------------------- | ---------------------------- |
7+
| null | null |
8+
| int | number |
9+
| float | number |
10+
| String | string |
11+
| Array | Array |
12+
| Dictionary | Object |
13+
| NodePath | string |
14+
| Object | godot.Object |
15+
| Resource | godot.Resource |
16+
| Vector2 | godot.Vector2 |
17+
| Color | godot.Color |
18+
| sin(v) | godot.sin(v) |
19+
| print(v) | godot.print(v) |
20+
| PI | godot.PI |
21+
| Color.black | godot.Color.black |
22+
| Control.CursorShape | godot.Control.CursorShape |
23+
| Label.Align.ALIGN_LEFT | godot.Label.Align.ALIGN_LEFT |
24+
25+
## API specification:
26+
27+
- Keys of Dictionary are converted to String in JavaScript
28+
- Signals are defined as constants to their classes
29+
```
30+
godot.Control.resized === 'resized' // true
31+
```
32+
33+
### Additional functions
34+
35+
- `godot.register_signal(cls, signal_name)` to register signals
36+
- `godot.register_property(cls, name, default_value)` to define and export properties
37+
- `godot.register_class(cls, name)` to register named class manually
38+
- `godot.set_script_tooled(cls, tooled)` to set `tooled` of the class
39+
- `godot.set_script_icon(cls, path)` to set icon of the class
40+
- `godot.get_type(val)` Returns the internal type of the given `Variant` object, using the `godot.TYPE_*`
41+
- `godot.yield(target, signal)` Returns a Promise which will be resolved when the signal emitted
42+
- `requestAnimationFrame(callback)` registers a callback function to be called every frame, returns a request ID.
43+
- `cancelAnimationFrame(request_id)` to cancel a previously scheduled frame request
44+
- `require(module_id)` to load a CommonJS module or load a resource file
45+
- `$` is the alias of `Node.get_node`
46+
47+
### Using signals
48+
49+
Allow passing functions for `godot.Object.connect`, `godot.Object.disconnect`, and `godot.Object.is_connected`:
50+
51+
```js
52+
this.panel.connect(godot.Control.resized, (size) => {
53+
console.log("The size of the panel changed to:", size);
54+
});
55+
```
56+
57+
Using `await` to wait for signals
58+
59+
```js
60+
await godot.yield(
61+
this.get_tree().create_timer(1),
62+
godot.SceneTreeTimer.timeout
63+
);
64+
console.log("After one second to show");
65+
```
66+
67+
Preload resources with ECMAScript import statement
68+
69+
```js
70+
import ICON from "res://icon.png";
71+
```
72+
73+
### Multi-threading
74+
75+
Multi-threading with minimal [Worker API](https://developer.mozilla.org/en-US/docs/Web/API/Worker) (**This is an experimental feature**)
76+
77+
Start a new thread with Worker:
78+
79+
```js
80+
const worker = new Worker("worker.js"); // Run worker.js in a new thread context
81+
worker.postMessage({ type: "load_dlc", value: "dlc01.pck" });
82+
worker.onmessage = function (msg) {
83+
console.log("[MainThread] received message from worker thread:", msg);
84+
};
85+
```
86+
87+
Transfer value in different thread context with `godot.abandon_value` and `godot.adopt_value`:
88+
89+
```js
90+
// In worker thread
91+
let id = godot.abandon_value(object);
92+
postMessage({ type: "return_value", id: id });
93+
94+
// In the host thread
95+
worker.onmessage = function (msg) {
96+
if (typeof msg === "object" && msg.type === "return_value") {
97+
let value_from_worker = godot.adopt_value(msg.id);
98+
}
99+
};
100+
```

Diff for: docs/documentation/getting-started.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
### How to export script class to Godot
2+
3+
1. Define your JavaScript class and inherit from a Godot class, then export it as the **default** entry:
4+
5+
```javascript title="my-sprite.mjs"
6+
// The default export entry is treated as an exported class to Godot
7+
export default class MySprite extends godot.Sprite {
8+
// this is _init() in GDScript
9+
constructor() {
10+
super();
11+
}
12+
13+
_ready() {}
14+
15+
_process(delta) {}
16+
}
17+
```
18+
19+
2. Save the script with extension `.mjs`
20+
3. Attach the script file to the node or resource object like you do with GDScript
21+
22+
### How to export signals
23+
24+
```javascript title="my-sprite.mjs"
25+
export default class MySprite extends godot.Sprite {}
26+
// register game_over signal to MySprite class
27+
godot.register_signal(MySprite, "game_over");
28+
```
29+
30+
### How to export properties
31+
32+
```javascript title="my-sprite.mjs"
33+
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+
}
38+
}
39+
// export 'direction' properties to MySprite Godot inspector
40+
godot.register_property(MySprite, "direction", new godot.Vector2(1, 0));
41+
```
42+
43+
There are 2 ways of using the `godot.register_property`. The third parameter can either be a default value for the property you're trying to export or an object giving a more detailed description of how the editor should show it.
44+
45+
```js
46+
function register_property(target: GodotClass | godot.Object, name: string, value: any);
47+
function register_property(target: GodotClass | godot.Object, name: string, info: PropertyInfo);
48+
```
49+
50+
So calling the `register_property` like this:
51+
52+
```js
53+
godot.register_property(MyClass, "number_value", 3.14);
54+
```
55+
56+
Is the simplified version of:
57+
58+
```js
59+
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,
64+
});
65+
```
66+
67+
For more detail on how to use it, [click here](https://github.com/Geequlim/ECMAScript/issues/24#issuecomment-655584829).

Diff for: docs/documentation/gotchas.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Gotchas and limitations
2+
3+
Some common mistakes and limitations.
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 { 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+
30+
## Position.x is immutable
31+
32+
You cannot change `this.position.x` try to change `this.position`:
33+
34+
```javascript title="player.mjs"
35+
export default class Player extends godot.KinematicBody2D {
36+
constructor() {
37+
super();
38+
this.direction = new godot.Vector2(1, 0);
39+
}
40+
_ready() {}
41+
_process(delta) {
42+
this.position.x += this.direction.x; // <- breaks
43+
this.position += this.direction; // <- works
44+
}
45+
}
46+
godot.register_property(Player, "direction", new godot.Vector2(1, 0));
47+
```
48+
49+
## ``register_property`` has to be a target
50+
51+
You cannot change `this.position.x` try to change `this.position`:
52+
53+
```javascript title="player.mjs"
54+
export default class Player extends godot.KinematicBody2D {
55+
}
56+
// This works
57+
godot.register_property(Player, "directionWorks", new godot.Vector2(1, 0));
58+
// This breaks because `player` isn't a correct target
59+
godot.register_property(player, "directionBreaks", new godot.Vector2(1, 0));
60+
```

Diff for: docs/documentation/typescript.md

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
- Run the menu command `Project > Tools > JavaScript > Generate TypeScript Project` from the Godot editor to generate a TypeScript project
2+
- Run `tsc -w -p .` under your project folder in the terminal to compile scripts
3+
4+
## Code completion
5+
6+
- Code completion in TS will automatically work once the TypeScript project is generated by the above steps.
7+
- Code completion in VSCode is achieved by the property `"types": "./godot.d.ts"` in the generated package.json file of the TypeScript project. The `godot.d.ts` file can be generated alone via the `Project > Tools > ECMAScript > Generate TypeScript Declaration File` editor menu option and added to a `package.json` file manually to achieve this without a full TypeScript project.
8+
9+
## Example
10+
11+
Compile your `ts` script to a `.mjs` file then we can attach it to a node in godot editor.
12+
13+
Most of the `register` functions are available as various decorators as seen below.
14+
15+
```ts
16+
import { signal, property, tool, onready, node } from "./decorators";
17+
18+
@tool // make the script runnable in godot editor
19+
export default class InputLine extends godot.HBoxContainer {
20+
// define a signal
21+
@signal
22+
static readonly OnTextChanged: string;
23+
24+
// expose a node property
25+
@node
26+
icon: godot.Sprite;
27+
28+
// register offset property with the godot inspector with default value of Vector2(0, 0)
29+
@property({ default: godot.Vector2.ZERO })
30+
offset: godot.Vector2;
31+
32+
// register properties for godot editor inspector
33+
@property({ type: godot.VariantType.TYPE_STRING })
34+
get title() {
35+
return this._title;
36+
}
37+
set title(v: string) {
38+
this._title = v;
39+
if (this._label) {
40+
this._label.text = v;
41+
}
42+
}
43+
private _title: string;
44+
45+
@property({ default: "Input text here" })
46+
get hint() {
47+
return this._hint;
48+
}
49+
set hint(v: string) {
50+
this._hint = v;
51+
if (this.edit) {
52+
this.edit.hint_tooltip = v;
53+
this.edit.placeholder_text = v;
54+
}
55+
}
56+
private _hint: string;
57+
58+
get label(): godot.Label {
59+
return this._label;
60+
}
61+
protected _label: godot.Label;
62+
63+
// call get_node('LineEdit') and assign the returned value to 'this.edit' automatically when the node is ready
64+
@onready("LineEdit")
65+
edit: godot.LineEdit;
66+
67+
get text(): string {
68+
return this.edit?.text;
69+
}
70+
71+
_ready() {
72+
// get first child with the type of godot.Label
73+
this._label = this.get_node(godot.Label);
74+
75+
// Apply the inspector filled values with property setters
76+
this.title = this.title;
77+
this.hint = this.hint;
78+
79+
this.edit.connect(godot.LineEdit.text_changed, (text: string) => {
80+
this.emit_signal(InputLine.OnTextChanged, text);
81+
});
82+
}
83+
}
84+
```

0 commit comments

Comments
 (0)