|
1 | 1 | # Load json in singleton
|
2 | 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`. |
| 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`. |
5 | 8 |
|
6 | 9 | ## 1. Create file
|
7 | 10 |
|
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`. |
9 | 12 |
|
10 |
| -Next we write this to the ``test.json``: |
| 13 | +Next we write this to the `test.json`: |
11 | 14 |
|
12 |
| -````json title="test.json" |
| 15 | +```json title="test.json" |
13 | 16 | {
|
14 | 17 | "test": true
|
15 | 18 | }
|
16 |
| -```` |
| 19 | +``` |
17 | 20 |
|
18 | 21 | ## 2. Create the singleton
|
19 | 22 |
|
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: |
21 | 24 |
|
22 |
| -````ts title="read-config.ts" |
23 |
| -// @ts-ignore |
24 |
| -import TestJson from "res://config/test.json"; |
| 25 | +=== "JavaScript" |
25 | 26 |
|
26 |
| -type TestType = { |
27 |
| - test: boolean; |
28 |
| -}; |
| 27 | + ```javascript title="read-config.mjs" |
| 28 | + import TestJson from "res://config/test.json"; |
29 | 29 |
|
30 |
| -export default class ReadConfig extends godot.Node { |
31 |
| - static _singleton: ReadConfig; |
| 30 | + export default class ReadConfig extends godot.Node { |
| 31 | + static _singleton; |
32 | 32 |
|
33 |
| - static get singleton() { |
34 |
| - return ReadConfig._singleton; |
35 |
| - } |
| 33 | + static get singleton() { |
| 34 | + return ReadConfig._singleton; |
| 35 | + } |
36 | 36 |
|
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; |
41 | 46 | }
|
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 | + ``` |
48 | 77 |
|
49 | 78 | ## 3. Autoload singleton in project
|
50 | 79 |
|
51 |
| -We need to update the ``[autoload]`` inside `project.godot`: |
| 80 | +We need to update the `[autoload]` inside `project.godot`: |
52 | 81 |
|
53 |
| -````text title="project.godot" |
| 82 | +```text title="project.godot" |
54 | 83 | ...
|
55 | 84 | [autoload]
|
56 | 85 |
|
57 | 86 | ; Use the generated `.mjs` file instead of `.ts`
|
58 | 87 | ReadConfig="*res://scripts/generated/read-config.mjs"
|
59 | 88 | ...
|
60 |
| -```` |
| 89 | +``` |
61 | 90 |
|
62 | 91 | ## 4. Use the singleton in other class
|
63 | 92 |
|
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`: |
65 | 94 |
|
66 |
| -````ts title="main.ts" |
67 |
| -import ReadConfig from "./read-config"; |
| 95 | +=== "JavaScript" |
68 | 96 |
|
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 | + ``` |
74 | 106 |
|
75 |
| -```` |
| 107 | +=== "TypeScript" |
76 | 108 |
|
| 109 | + ```ts title="main.ts" |
| 110 | + import ReadConfig from "./read-config"; |
77 | 111 |
|
| 112 | + export default class Main extends godot.Node { |
| 113 | + _ready(): void { |
| 114 | + console.log(ReadConfig.singleton.config.test); // prints "true" |
| 115 | + } |
| 116 | + } |
| 117 | + ``` |
0 commit comments