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
# JavaScript language binding for Godot game engine
1
+
# GodotJS - JavaScript language binding for Godot game engine
2
2
3
3
This module implements JavaScript/TypeScript language support for the Godot game engine using [QuickJS](https://bellard.org/quickjs/) as the JavaScript engine.
4
4
@@ -13,275 +13,32 @@ This module implements JavaScript/TypeScript language support for the Godot game
13
13
- Full code completion support for all Godot APIs including signals and enumerations
14
14
- Debug in Visual Studio Code with the [plugin](https://marketplace.visualstudio.com/items?itemName=geequlim.godot-javascript-debug) - currently not available for 4.x
15
15
16
-
##Installation
16
+
### Getting started
17
17
18
-
No installation or setup necessary. The binaries for download are the complete, usable Godot editor and engine with JavaScript/TypeScript language support.
19
-
20
-
## Download
21
-
22
-
You can try the pre-compiled binaries from the [release page](https://github.com/GodotExplorer/ECMAScript/releases) or get the binaries with the latest commits from the [GitHub build action result](https://github.com/GodotExplorer/ECMAScript/actions).
23
-
24
-
## Compilation
25
-
26
-
1. Clone the source code of [godot](https://github.com/godotengine/godot)
27
-
2. Clone this module and put it into `godot/modules/` and make sure the folder name of this module is `javascript`
28
-
3.[Recompile the godot engine](https://docs.godotengine.org/en/4.1/development/compiling/index.html) - Use ``scons`` with those additional options ``warnings=extra werror=yes module_text_server_fb_enabled=yes`` to show all potential errors
29
-
30
-

31
-
32
-
## Usage
33
-
34
-
### How to export script class to Godot
35
-
36
-
1. Define your JavaScript class and inherit from a Godot class, then export it as the **default** entry:
37
-
38
-
```js
39
-
// The default export entry is treated as an exported class to Godot
40
-
exportdefaultclassMySpriteextendsgodot.Sprite {
41
-
// this is _init() in GDScript
42
-
constructor() {
43
-
super();
44
-
}
45
-
46
-
_ready() {}
47
-
48
-
_process(delta) {}
49
-
}
50
-
```
51
-
52
-
2. Save the script with extension `.mjs`
53
-
3. Attach the script file to the node or resource object like you do with GDScript
54
-
55
-
### How to export signals
56
-
57
-
```js
58
-
exportdefaultclassMySpriteextendsgodot.Sprite {}
59
-
// register game_over signal to MySprite class
60
-
godot.register_signal(MySprite, "game_over");
61
-
```
62
-
63
-
### How to export properties
64
-
65
-
```js
66
-
exportdefaultclassMySpriteextendsgodot.Sprite {
67
-
_process(delta) {
68
-
// Yes! We can use operators in JavaScript like GDScript
69
-
this.position+=this.direction* delta;
70
-
}
71
-
}
72
-
// export 'direction' properties to MySprite Godot inspector
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.
console.log("[MainThread] received message from worker thread:", msg);
174
-
};
175
-
```
176
-
- Transfer value in different thread context with`godot.abandon_value` and `godot.adopt_value`
177
-
178
-
```js
179
-
// In worker thread
180
-
let id = godot.abandon_value(object);
181
-
postMessage({ type: "return_value", id: id });
182
-
183
-
// In the host thread
184
-
worker.onmessage = function (msg) {
185
-
if (typeof msg === "object" && msg.type === "return_value") {
186
-
let value_from_worker = godot.adopt_value(msg.id);
187
-
}
188
-
};
189
-
```
190
-
191
-
### TypeScript support
192
-
193
-
- Run the menu command `Project > Tools > JavaScript > Generate TypeScript Project` from the Godot editor to generate a TypeScript project
194
-
- Run `tsc -w -p .` under your project folder in the terminal to compile scripts
195
-
196
-
#### Code completion
197
-
198
-
- Code completion inTS will automatically work once the TypeScript project is generated by the above steps.
199
-
- 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.
200
-
201
-
#### Example TypeScript Usage
202
-
203
-
Compile your `ts` script to a `.mjs` file then we can attach it to a node in godot editor.
204
-
205
-
Most of the `register` functions are available as various decorators as seen below.
206
-
207
-
```ts
208
-
import { signal, property, tool, onready, node } from "./decorators";
209
-
210
-
@tool // make the script runnable in godot editor
211
-
export default class InputLine extends godot.HBoxContainer {
212
-
// define a signal
213
-
@signal
214
-
static readonly OnTextChanged: string;
215
-
216
-
// expose a node property
217
-
@node
218
-
icon: godot.Sprite;
219
-
220
-
// register offset property with the godot inspector with default value of Vector2(0, 0)
// call get_node('LineEdit') and assign the returned value to 'this.edit' automatically when the node is ready
256
-
@onready("LineEdit")
257
-
edit: godot.LineEdit;
258
-
259
-
get text(): string {
260
-
return this.edit?.text;
261
-
}
262
-
263
-
_ready() {
264
-
// get first child with the type of godot.Label
265
-
this._label = this.get_node(godot.Label);
22
+
No installation or setup necessary. The binaries for download are the complete, usable Godot editor and engine with JavaScript/TypeScript language support.
266
23
267
-
// Apply the inspector filled values with property setters
268
-
this.title = this.title;
269
-
this.hint = this.hint;
24
+
### Binary downloads
25
+
Download the binaries from the [release page](https://github.com/GodotExplorer/ECMAScript/releases).
-[Recompile the godot engine](https://docs.godotengine.org/en/4.1/development/compiling/index.html)
35
+
- Use ``scons`` with those additional options ``warnings=extra werror=yes module_text_server_fb_enabled=yes`` to show all potential errors
277
36
278
-
## Demo
37
+
## Documentation, Tutorials & Demos
279
38
280
-
You can try demos in the [ECMAScriptDemos](https://github.com/Geequlim/ECMAScriptDemos)
39
+
Read this [documentation](https://geequlim.github.io/ecmascript/getting-started) or look at the tutorials or demos:
281
40
282
-
## Developer notes
283
41
284
-
- This package is not compatible withMSVC, you will get build errors in quickjs.
285
-
- The script also build the `on_tag.yml` script which automates the github release publishing.
286
-
\*\* The `on_tag.yml` functionality tries to sleep until all jobs with the same `sha` are completed. It should be fine to run whenever, but depending on how github actions culls long running jobs you might want to make sure that all/(most of) the builds look good before tagging.
287
-
- They should definitely be fixed & enabled at some point, **so please submit a PRif you have any ideas of how to do that!**
0 commit comments