|
| 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 | +``` |
0 commit comments