Skip to content

Commit 83670ad

Browse files
authored
docs: update examples to show js and ts code (#203)
1 parent 793db20 commit 83670ad

7 files changed

+202
-115
lines changed

CONTRIBUTING.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Local Development
1+
# Contributing
22

33
## Project Structure
44

@@ -7,10 +7,24 @@ TODO
77
## Documentation
88

99
This project uses [MKDocs](https://www.mkdocs.org/) to serve all docs. You can change the documentation inside the `docs` folder.
10+
Install mkdocs via ``pip install mkdocs``.
11+
Serve the docs via ``mkdocs serve``.
1012
To add new pages you need to update `mkdocs.yml`.
1113

1214
## Preparing your PR
1315

1416
The project is using [`clang-format`](https://clang.llvm.org/docs/ClangFormat.html) to format most files. You need to run `bash ./clang_format.sh` before your PR for a successful pipeline.
1517

1618
Furthermore, there is an `utf-8` and `LF` checker to fix file formats. Additionally, some spellchecks run inside the [pipeline](.github/workflows/static_checks.yml).
19+
20+
## Release library
21+
22+
As a maintainer you are able to create a new release.
23+
1. Go to [create new release](https://github.com/Geequlim/ECMAScript/releases/new)
24+
2. Next you should ``Choose a tag`` and create a new one in the format `v0.0.0`
25+
3. Select target ``master``
26+
4. Click ``Generate release notes``
27+
5. (Optional) You can edit the generated release notes
28+
6. Add a release title in the format ``[Godot_Version]-[Tag]-alpha-[Year][Month][Day]`` as an example ``4.1-v0.0.17-alpha-20231003``
29+
7. Press ``Publish release``
30+
+80-40
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,117 @@
11
# Load json in singleton
22

3-
This example shows how to load a file like a ``config`` inside a singleton to access it everywhere.
4-
We use ``TypeScript`` for this example all `.ts` files will be compiled as `.mjs` to the folder `scripts/generated/**`. If you use `TypeScript` you need to set `"resolveJsonModule": true` inside your `tsconfig.json`.
3+
This example shows how to load a file like a `config` inside a singleton to access it everywhere.
4+
5+
For the `TypeScript` examples, all `.ts` files will be compiled as `.mjs` to the folder `scripts/generated/**`.
6+
7+
> **Note:** If you use `TypeScript` you need to set `"resolveJsonModule": true` inside your `tsconfig.json`.
58
69
## 1. Create file
710

8-
We create a new folder named ``config`` and a new file `test.json`.
11+
We create a new folder named `config` and a new file `test.json`.
912

10-
Next we write this to the ``test.json``:
13+
Next we write this to the `test.json`:
1114

12-
````json title="test.json"
15+
```json title="test.json"
1316
{
1417
"test": true
1518
}
16-
````
19+
```
1720

1821
## 2. Create the singleton
1922

20-
We create a new file inside our `src` folder like ``read-config.ts`` and add this code to it:
23+
We create a new file inside our `src` folder like `read-config.(mjs|ts)` and add this code to it:
2124

22-
````ts title="read-config.ts"
23-
// @ts-ignore
24-
import TestJson from "res://config/test.json";
25+
=== "JavaScript"
2526

26-
type TestType = {
27-
test: boolean;
28-
};
27+
```javascript title="read-config.mjs"
28+
import TestJson from "res://config/test.json";
2929

30-
export default class ReadConfig extends godot.Node {
31-
static _singleton: ReadConfig;
30+
export default class ReadConfig extends godot.Node {
31+
static _singleton;
3232

33-
static get singleton() {
34-
return ReadConfig._singleton;
35-
}
33+
static get singleton() {
34+
return ReadConfig._singleton;
35+
}
3636

37-
constructor() {
38-
super();
39-
if (!ReadConfig._singleton) {
40-
ReadConfig._singleton = this;
37+
constructor() {
38+
super();
39+
if (!ReadConfig._singleton) {
40+
ReadConfig._singleton = this;
41+
}
42+
}
43+
44+
// This property is available for other classes
45+
config = TestJson;
4146
}
42-
}
43-
44-
// This property is available for other classes
45-
config: TestType = TestJson as TestType;
46-
}
47-
````
47+
```
48+
49+
=== "TypeScript"
50+
51+
```ts title="read-config.ts"
52+
// @ts-ignore
53+
import TestJson from "res://config/test.json";
54+
55+
type TestType = {
56+
test: boolean;
57+
};
58+
59+
export default class ReadConfig extends godot.Node {
60+
static _singleton: ReadConfig;
61+
62+
static get singleton() {
63+
return ReadConfig._singleton;
64+
}
65+
66+
constructor() {
67+
super();
68+
if (!ReadConfig._singleton) {
69+
ReadConfig._singleton = this;
70+
}
71+
}
72+
73+
// This property is available for other classes
74+
config: TestType = TestJson as TestType;
75+
}
76+
```
4877

4978
## 3. Autoload singleton in project
5079

51-
We need to update the ``[autoload]`` inside `project.godot`:
80+
We need to update the `[autoload]` inside `project.godot`:
5281

53-
````text title="project.godot"
82+
```text title="project.godot"
5483
...
5584
[autoload]
5685
5786
; Use the generated `.mjs` file instead of `.ts`
5887
ReadConfig="*res://scripts/generated/read-config.mjs"
5988
...
60-
````
89+
```
6190

6291
## 4. Use the singleton in other class
6392

64-
In another class e.g. ``main.ts`` you need to import the `ReadConfig` then you can access every public property and method from `ReadConfig` via `ReadConfig.singleton`:
93+
In another class e.g. `main.(mjs|ts)` you need to import the `ReadConfig` then you can access every public property and method from `ReadConfig` via `ReadConfig.singleton`:
6594

66-
````ts title="main.ts"
67-
import ReadConfig from "./read-config";
95+
=== "JavaScript"
6896

69-
export default class Main extends godot.Node {
70-
_ready(): void {
71-
console.log(ReadConfig.singleton.config.test); // prints "true"
72-
}
73-
}
97+
```ts title="main.mjs"
98+
import ReadConfig from "./read-config";
99+
100+
export default class Main extends godot.Node {
101+
_ready() {
102+
console.log(ReadConfig.singleton.config.test); // prints "true"
103+
}
104+
}
105+
```
74106

75-
````
107+
=== "TypeScript"
76108

109+
```ts title="main.ts"
110+
import ReadConfig from "./read-config";
77111

112+
export default class Main extends godot.Node {
113+
_ready(): void {
114+
console.log(ReadConfig.singleton.config.test); // prints "true"
115+
}
116+
}
117+
```

docs/examples/read-file-local.md

+33-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This example shows how to load any file as string from a local folder.
44

5-
We use ``TypeScript`` for this example all `.ts` files will be compiled as `.mjs`.
5+
For the `TypeScript` examples, all `.ts` files will be compiled as `.mjs` to the folder `scripts/generated/**`.
66

77
## 1. Create the file you want to read
88

@@ -19,19 +19,36 @@ HELLO,hello,hallo
1919

2020
We use [FileAccess](https://docs.godotengine.org/en/stable/classes/class_fileaccess.html) to read the file.
2121

22-
We create a new file ``read-local-file.ts``:
23-
24-
````ts title="read-local-file.ts"
25-
export default class ReadLocalFile extends godot.Node {
26-
_ready(): void {
27-
const file = new godot.FileAccess();
28-
file.open("res://resources/test.csv", godot.FileAccess.ModeFlags.READ);
29-
let fileContent: string = "";
30-
while (!file.eof_reached()) {
31-
fileContent += `${file.get_line()}\n`;
32-
}
33-
console.log(fileContent);
34-
}
35-
}
36-
````
22+
We create a new file ``read-local-file.(mjs|ts)``:
23+
24+
=== "JavaScript"
25+
26+
````ts title="read-local-file.mjs"
27+
export default class ReadLocalFile extends godot.Node {
28+
_ready() {
29+
const file = new godot.FileAccess();
30+
file.open("res://resources/test.csv", godot.FileAccess.ModeFlags.READ);
31+
let fileContent = "";
32+
while (!file.eof_reached()) {
33+
fileContent += `${file.get_line()}\n`;
34+
}
35+
console.log(fileContent);
36+
}
37+
}
38+
````
39+
=== "TypeScript"
40+
41+
````ts title="read-local-file.ts"
42+
export default class ReadLocalFile extends godot.Node {
43+
_ready(): void {
44+
const file = new godot.FileAccess();
45+
file.open("res://resources/test.csv", godot.FileAccess.ModeFlags.READ);
46+
let fileContent: string = "";
47+
while (!file.eof_reached()) {
48+
fileContent += `${file.get_line()}\n`;
49+
}
50+
console.log(fileContent);
51+
}
52+
}
53+
````
3754

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Use GDScript in TypeScript
2+
3+
This example shows how to use a class written in GDScript in another TS class.
4+
5+
For the `TypeScript` examples, all `.ts` files will be compiled as `.mjs` to the folder `scripts/generated/**`.
6+
7+
## 1. Create the GDScript file
8+
9+
First we create a simple class with GDScript inside a new file `scripts/gd/GDTest.gd`:
10+
11+
````gdscript title="GDTest.gd"
12+
extends Node
13+
14+
func _ready():
15+
pass
16+
17+
func print_in_gd_test():
18+
print("gd_test")
19+
20+
````
21+
22+
## 2. Create a declaration (*.d.ts) for GDScript (only TypeScript)
23+
24+
For proper TypeScript support we need to add a ``gdtest.d.ts`` file:
25+
26+
````ts title="gdtest.d.ts"
27+
declare module "res://scripts/gd/GDTest.gd" {
28+
class GDTest {
29+
call(func: "print_in_gd_test"): void;
30+
31+
static new() {
32+
return this;
33+
}
34+
}
35+
export = GDTest;
36+
}
37+
````
38+
39+
## 3. Use the class inside your TS file
40+
41+
In the end we need to call the ``GDTest.gd`` from another `(.mjs|.ts)` file, like `main.(mjs|ts)`:
42+
43+
=== "JavaScript"
44+
````ts title="main.mjs"
45+
import GDTest from "res://scripts/gd/GDTest.gd";
46+
47+
export default class Main extends godot.Node {
48+
_ready() {
49+
const gdTest = GDTest.new();
50+
gdTest.call("print_in_gd_test");
51+
}
52+
}
53+
54+
````
55+
=== "TypeScript"
56+
````ts title="main.ts"
57+
import GDTest from "res://scripts/gd/GDTest.gd";
58+
59+
export default class Main extends godot.Node {
60+
_ready(): void {
61+
const gdTest: GDTest = GDTest.new();
62+
gdTest.call("print_in_gd_test");
63+
}
64+
}
65+
66+
````
67+
68+
> **Note:** The important thing here is that you use `new()` to instantiate and `call` to execute the function

docs/examples/use-gdscript-in-ts.md

-55
This file was deleted.

docs/getting-started.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

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

5-
```
5+
```javascript title="my-sprite.mjs"
66
// The default export entry is treated as an exported class to Godot
77
export default class MySprite extends godot.Sprite {
88
// this is _init() in GDScript
99
constructor() {
1010
super();
1111
}
1212

13-
_reajavascript title="my-sprite.mjs"dy() {}
13+
_ready() {}
1414

1515
_process(delta) {}
1616
}

0 commit comments

Comments
 (0)