Skip to content

Commit fd0759e

Browse files
authored
docs: add examples and gotchas for docs (#200)
* docs: add examples and gotchas for docs * chore: update static_checks.yml * chore: update static_checks.yml * chore: update static_checks.yml * chore: update static_checks.yml * chore: update static_checks.yml * chore: update static_checks.yml * chore: update CONTRIBUTING.md * test: clang-format * test: clang-format
1 parent 8f06259 commit fd0759e

13 files changed

+313
-38
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<!--
2-
Please target the `master` branch in priority.
3-
PRs can target `3.x` if the same change was done in `master`, or is not relevant there.
1+
## Description
42

5-
Relevant fixes are cherry-picked for stable branches as needed by maintainers.
6-
You can mention in the description if the change is compatible with `3.x`.
7-
-->
3+
<!-- Describe the changes you've done -->
4+
5+
Make sure to follow the PR preparation steps in [CONTRIBUTING.md](../CONTRIBUTING.md#preparing-your-pr) before submitting your PR:
6+
7+
- [ ] format the codebase: from the root, run `bash ./clang_format.sh`.

.github/workflows/static_checks.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ on:
66
required: true
77
type: string
88

9-
109
concurrency:
1110
group: ci-${{github.actor}}-${{github.head_ref || github.run_number}}-${{github.ref}}-static
1211
cancel-in-progress: true
@@ -30,7 +29,9 @@ jobs:
3029
path: tmp
3130

3231
- name: Checkout workaround
33-
run: mv tmp/misc/scripts misc/scripts
32+
run: |
33+
mv tmp/misc/scripts misc/scripts
34+
cp clang_format.sh misc/scripts/clang_format.sh
3435
3536
- name: Install APT dependencies
3637
uses: awalsh128/cache-apt-pkgs-action@latest

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
*.gen.json
1010
.vscode
1111
.idea
12+
/site

CONTRIBUTING.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Local Development
2+
3+
## Project Structure
4+
5+
TODO
6+
7+
## Documentation
8+
9+
This project uses [MKDocs](https://www.mkdocs.org/) to serve all docs. You can change the documentation inside the `docs` folder.
10+
To add new pages you need to update `mkdocs.yml`.
11+
12+
## Preparing your PR
13+
14+
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.
15+
16+
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).

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,7 @@ Read this [documentation](https://geequlim.github.io/ECMAScript/getting-started)
4747
- [godot-ECMAScript-cookbook](https://github.com/why-try313/godot-ECMAScript-cookbook/wiki) - Tutorial
4848
- [godot-typescript-starter](https://github.com/citizenll/godot-typescript-starter) - Template
4949
- [godot-js-template](https://github.com/fukaraadam-workspace/godot-js-template) - Template
50+
51+
## Contributing
52+
53+
If you like to contribute to this project check the [CONTRIBUTING.md](https://github.com/Geequlim/ECMAScript/blob/master/CONTRIBUTING.md) file.

clang_format.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env bash
2+
3+
# This script runs clang-format and fixes copyright headers on all relevant files in the repo.
4+
# This is the primary script responsible for fixing style violations.
5+
6+
set -uo pipefail
7+
8+
if [ $# -eq 0 ]; then
9+
# Loop through all code files tracked by Git.
10+
files=$(git ls-files -- '*.c' '*.h' '*.cpp' '*.hpp' '*.cc' '*.hh' '*.cxx' '*.m' '*.mm' '*.inc' '*.java' '*.glsl' \
11+
':!:.git/*' ':!:thirdparty/*' ':!:*/thirdparty/*' ':!:platform/android/java/lib/src/com/google/*' \
12+
':!:*-so_wrap.*' ':!:tests/python_build/*')
13+
else
14+
# $1 should be a file listing file paths to process. Used in CI.
15+
files=$(cat "$1" | grep -v "thirdparty/" | grep -E "\.(c|h|cpp|hpp|cc|hh|cxx|m|mm|inc|java|glsl)$" | grep -v "platform/android/java/lib/src/com/google/" | grep -v "\-so_wrap\." | grep -v "tests/python_build/")
16+
fi
17+
18+
if [ ! -z "$files" ]; then
19+
clang-format --Wno-error=unknown -i $files
20+
fi
21+
22+
diff=$(git diff --color)
23+
24+
# If no diff has been generated all is OK, clean up, and exit.
25+
if [ -z "$diff" ] ; then
26+
printf "\e[1;32m*** Files in this commit comply with the clang-format style rules.\e[0m\n"
27+
exit 0
28+
fi
29+
30+
# A diff has been created, notify the user, clean up, and exit.
31+
printf "\n\e[1;33m*** The following changes must be made to comply with the formatting rules:\e[0m\n\n"
32+
# Perl commands replace trailing spaces with `·` and tabs with `<TAB>`.
33+
printf "%s\n" "$diff" | perl -pe 's/(.*[^ ])( +)(\e\[m)$/my $spaces="·" x length($2); sprintf("$1$spaces$3")/ge' | perl -pe 's/(.*[^\t])(\t+)(\e\[m)$/my $tabs="<TAB>" x length($2); sprintf("$1$tabs$3")/ge'
34+
35+
printf "\n\e[1;91m*** Please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\e[0m\n"
36+
exit 1
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Load json in singleton
2+
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`.
5+
6+
## 1. Create file
7+
8+
We create a new folder named ``config`` and a new file `test.json`.
9+
10+
Next we write this to the ``test.json``:
11+
12+
````json title="test.json"
13+
{
14+
"test": true
15+
}
16+
````
17+
18+
## 2. Create the singleton
19+
20+
We create a new file inside our `src` folder like ``read-config.ts`` and add this code to it:
21+
22+
````ts title="read-config.ts"
23+
// @ts-ignore
24+
import TestJson from "res://config/test.json";
25+
26+
type TestType = {
27+
test: boolean;
28+
};
29+
30+
export default class ReadConfig extends godot.Node {
31+
static _singleton: ReadConfig;
32+
33+
static get singleton() {
34+
return ReadConfig._singleton;
35+
}
36+
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: TestType = TestJson as TestType;
46+
}
47+
````
48+
49+
## 3. Autoload singleton in project
50+
51+
We need to update the ``[autoload]`` inside `project.godot`:
52+
53+
````text title="project.godot"
54+
...
55+
[autoload]
56+
57+
; Use the generated `.mjs` file instead of `.ts`
58+
ReadConfig="*res://scripts/generated/read-config.mjs"
59+
...
60+
````
61+
62+
## 4. Use the singleton in other class
63+
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`:
65+
66+
````ts title="main.ts"
67+
import ReadConfig from "./read-config";
68+
69+
export default class Main extends godot.Node {
70+
_ready(): void {
71+
console.log(ReadConfig.singleton.config.test); // prints "true"
72+
}
73+
}
74+
75+
````
76+
77+

docs/examples/read-file-local.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Read file local
2+
3+
This example shows how to load any file as string from a local folder.
4+
5+
We use ``TypeScript`` for this example all `.ts` files will be compiled as `.mjs`.
6+
7+
## 1. Create the file you want to read
8+
9+
In this example we try to read a ``.csv`` file, but it should work with any other file as well.
10+
11+
We create a folder ``resources`` and add a new file `test.csv`:
12+
13+
````csv title="test.csv"
14+
keys,en,de
15+
HELLO,hello,hallo
16+
````
17+
18+
## 2. Read the file in class
19+
20+
We use [FileAccess](https://docs.godotengine.org/en/stable/classes/class_fileaccess.html) to read the file.
21+
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+
````
37+

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Use GDScript in TypeScript
2+
3+
This example shows how to use a class written in GDScript in another TS class.
4+
5+
We use ``TypeScript`` for this example 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("bla")
19+
20+
````
21+
22+
## 2. Create a declaration (*.d.ts) for GDScript
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 `.ts` file, like `main.ts`:
42+
43+
````ts title="main.ts"
44+
import GDTest from "res://scripts/gd/GDTest.gd";
45+
46+
export default class Main extends godot.Node {
47+
_ready(): void {
48+
const bla: Bla = Bla.new();
49+
bla.call("run_in_bla");
50+
}
51+
}
52+
53+
````
54+
55+
> **Note:** The important thing here is that you use `new()` to instantiate and `call` to execute the function

docs/getting-started.md

Lines changed: 4 additions & 4 deletions
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-
```js
5+
```
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-
_ready() {}
13+
_reajavascript title="my-sprite.mjs"dy() {}
1414
1515
_process(delta) {}
1616
}
@@ -21,15 +21,15 @@ export default class MySprite extends godot.Sprite {
2121

2222
### How to export signals
2323

24-
```js
24+
```javascript title="my-sprite.mjs"
2525
export default class MySprite extends godot.Sprite {}
2626
// register game_over signal to MySprite class
2727
godot.register_signal(MySprite, "game_over");
2828
```
2929

3030
### How to export properties
3131

32-
```js
32+
```javascript title="my-sprite.mjs"
3333
export default class MySprite extends godot.Sprite {
3434
_process(delta) {
3535
// Yes! We can use operators in JavaScript like GDScript

0 commit comments

Comments
 (0)