@preeternal/react-native-godot 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/LICENSE +22 -0
  2. package/Package.swift +106 -0
  3. package/README.md +631 -2
  4. package/android/build.gradle +289 -0
  5. package/android/consumer-rules.pro +19 -0
  6. package/android/fix-prefab.gradle +54 -0
  7. package/android/src/main/AndroidManifest.xml +2 -0
  8. package/android/src/main/cpp/CMakeLists.txt +74 -0
  9. package/android/src/main/cpp/GodotModule.cpp +718 -0
  10. package/android/src/main/cpp/OnLoad.cpp +66 -0
  11. package/android/src/main/cpp/libgodot_android.h +79 -0
  12. package/android/src/main/cpp/libgodot_jni.cpp +585 -0
  13. package/android/src/main/cpp/libgodot_jni.h +130 -0
  14. package/android/src/main/cpp/native_godot_module_jni.cpp +86 -0
  15. package/android/src/main/cpp/native_godot_module_jni.h +58 -0
  16. package/android/src/main/java/com/preeternal/reactnativegodot/NativeGodotModuleModule.kt +83 -0
  17. package/android/src/main/java/com/preeternal/reactnativegodot/NativeGodotModulePackage.kt +41 -0
  18. package/android/src/main/java/com/preeternal/reactnativegodot/NativeLibraryLoader.kt +5 -0
  19. package/android/src/main/java/com/preeternal/reactnativegodot/RTNGodotView.kt +81 -0
  20. package/android/src/main/java/com/preeternal/reactnativegodot/RTNGodotViewManager.kt +34 -0
  21. package/android/src/main/java/com/preeternal/reactnativegodot/RTNLibGodot.kt +350 -0
  22. package/android/src/main/res/layout/downloading_expansion.xml +165 -0
  23. package/android/src/main/res/layout/godot_app_layout.xml +5 -0
  24. package/android/src/main/res/layout/snackbar.xml +27 -0
  25. package/android/src/main/res/layout/status_bar_ongoing_event_progress_bar.xml +108 -0
  26. package/android/src/main/res/mipmap/icon.png +0 -0
  27. package/android/src/main/res/mipmap/icon_background.png +0 -0
  28. package/android/src/main/res/mipmap/icon_foreground.png +0 -0
  29. package/android/src/main/res/mipmap/icon_monochrome.png +0 -0
  30. package/android/src/main/res/mipmap-anydpi-v26/icon.xml +8 -0
  31. package/android/src/main/res/mipmap-anydpi-v26/themed_icon.xml +11 -0
  32. package/android/src/main/res/mipmap-hdpi/icon.png +0 -0
  33. package/android/src/main/res/mipmap-hdpi/icon_background.png +0 -0
  34. package/android/src/main/res/mipmap-hdpi/icon_foreground.png +0 -0
  35. package/android/src/main/res/mipmap-hdpi/icon_monochrome.png +0 -0
  36. package/android/src/main/res/mipmap-mdpi/icon.png +0 -0
  37. package/android/src/main/res/mipmap-mdpi/icon_background.png +0 -0
  38. package/android/src/main/res/mipmap-mdpi/icon_foreground.png +0 -0
  39. package/android/src/main/res/mipmap-mdpi/icon_monochrome.png +0 -0
  40. package/android/src/main/res/mipmap-xhdpi/icon.png +0 -0
  41. package/android/src/main/res/mipmap-xhdpi/icon_background.png +0 -0
  42. package/android/src/main/res/mipmap-xhdpi/icon_foreground.png +0 -0
  43. package/android/src/main/res/mipmap-xhdpi/icon_monochrome.png +0 -0
  44. package/android/src/main/res/mipmap-xxhdpi/icon.png +0 -0
  45. package/android/src/main/res/mipmap-xxhdpi/icon_background.png +0 -0
  46. package/android/src/main/res/mipmap-xxhdpi/icon_foreground.png +0 -0
  47. package/android/src/main/res/mipmap-xxhdpi/icon_monochrome.png +0 -0
  48. package/android/src/main/res/mipmap-xxxhdpi/icon.png +0 -0
  49. package/android/src/main/res/mipmap-xxxhdpi/icon_background.png +0 -0
  50. package/android/src/main/res/mipmap-xxxhdpi/icon_foreground.png +0 -0
  51. package/android/src/main/res/mipmap-xxxhdpi/icon_monochrome.png +0 -0
  52. package/android/src/main/res/values/dimens.xml +9 -0
  53. package/android/src/main/res/values/strings.xml +61 -0
  54. package/android/src/main/res/values/styles.xml +25 -0
  55. package/android/src/main/res/values-fa/strings.xml +14 -0
  56. package/android/src/main/res/values-ko/strings.xml +54 -0
  57. package/android/src/main/res/xml/godot_provider_paths.xml +15 -0
  58. package/android/src/test/java/com/preeternal/reactnativegodot/NativeGodotModulePackageTest.kt +26 -0
  59. package/app.plugin.js +173 -0
  60. package/cpp/GodotModule.h +103 -0
  61. package/cpp/NativeGodotModule.cpp +825 -0
  62. package/cpp/NativeGodotModule.h +45 -0
  63. package/cpp/godot-log.h +64 -0
  64. package/ios/GodotModule.mm +661 -0
  65. package/ios/NativeGodotModuleProvider.h +35 -0
  66. package/ios/NativeGodotModuleProvider.mm +42 -0
  67. package/ios/RTNGodotView.h +42 -0
  68. package/ios/RTNGodotView.mm +711 -0
  69. package/ios/log.mm +38 -0
  70. package/lib/module/NativeNativeGodotModule.js +59 -0
  71. package/lib/module/NativeNativeGodotModule.js.map +1 -0
  72. package/lib/module/RTNGodotView.js +6 -0
  73. package/lib/module/RTNGodotView.js.map +1 -0
  74. package/lib/module/RTNGodotView.native.js +5 -0
  75. package/lib/module/RTNGodotView.native.js.map +1 -0
  76. package/lib/module/RTNGodotViewNativeComponent.ts +37 -0
  77. package/lib/module/index.js +30 -0
  78. package/lib/module/index.js.map +1 -0
  79. package/lib/module/package.json +1 -0
  80. package/lib/typescript/package.json +1 -0
  81. package/lib/typescript/src/NativeNativeGodotModule.d.ts +26 -0
  82. package/lib/typescript/src/NativeNativeGodotModule.d.ts.map +1 -0
  83. package/lib/typescript/src/RTNGodotView.d.ts +7 -0
  84. package/lib/typescript/src/RTNGodotView.d.ts.map +1 -0
  85. package/lib/typescript/src/RTNGodotView.native.d.ts +3 -0
  86. package/lib/typescript/src/RTNGodotView.native.d.ts.map +1 -0
  87. package/lib/typescript/src/RTNGodotViewNativeComponent.d.ts +11 -0
  88. package/lib/typescript/src/RTNGodotViewNativeComponent.d.ts.map +1 -0
  89. package/lib/typescript/src/index.d.ts +6 -0
  90. package/lib/typescript/src/index.d.ts.map +1 -0
  91. package/package.json +262 -4
  92. package/prebuilt-utils.rb +36 -0
  93. package/react-native-godot.podspec +41 -0
  94. package/react-native.config.js +15 -0
  95. package/scripts/check-16kb.sh +136 -0
  96. package/scripts/check-packaged-artifacts.sh +154 -0
  97. package/scripts/ci/verify-prebuilt-install.cjs +39 -0
  98. package/scripts/download-prebuilt.js +274 -0
  99. package/src/NativeNativeGodotModule.ts +90 -0
  100. package/src/RTNGodotView.native.tsx +2 -0
  101. package/src/RTNGodotView.tsx +12 -0
  102. package/src/RTNGodotViewNativeComponent.ts +37 -0
  103. package/src/index.tsx +28 -0
package/README.md CHANGED
@@ -1,3 +1,632 @@
1
- # Temporary Holding Version
1
+ # @preeternal/react-native-godot
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![npm version](https://img.shields.io/npm/v/@preeternal/react-native-godot.svg)](https://www.npmjs.com/package/@preeternal/react-native-godot)
4
+ [![npm downloads](https://img.shields.io/npm/dm/@preeternal/react-native-godot.svg)](https://www.npmjs.com/package/@preeternal/react-native-godot)
5
+ [![Patreon](https://img.shields.io/endpoint?url=https%3A%2F%2Fshieldsio-patreon.vercel.app%2Fapi%2F%3Fusername%3Dpreeternal%26type%3Dpatrons)](https://www.patreon.com/preeternal)
6
+
7
+ React Native Godot allows embedding the Godot Engine into React Native applications.
8
+
9
+ Preeternal React Native Godot is a maintained fork of the original React Native Godot integration.
10
+
11
+ ## Upstream / credits
12
+
13
+ The original implementation — including the native bridge, platform
14
+ integration and LibGodot integration — was developed and maintained by
15
+ [Migeran](https://migeran.com). Thank you to the Migeran team and contributors
16
+ for building the project and its underlying engine integration.
17
+
18
+ [Born](https://born.com) deserves credit for the original [React Native
19
+ adaptation](https://github.com/borndotcom/react-native-godot) and early product integration.
20
+
21
+ This fork is currently used in my own project. Long-term maintenance,
22
+ backward compatibility and response times are not guaranteed; support will
23
+ depend on my needs and available time.
24
+
25
+ ## Main Features
26
+
27
+ * Supports Android and iOS, built on [LibGodot](https://github.com/Preeternal/libgodot).
28
+ * Supports starting, stopping and restarting the Godot Engine. [(docs)](#initialize-the-godot-instance)
29
+ * When restarting, the engine can be reconfigured, so a different Godot app may be loaded each time. [(docs)](#stop-the-godot-instance)
30
+ * It is also possible to pause and resume the running Godot instance. [(docs)](#pause-the-godot-instance)
31
+ * Godot is running on a separate thread, so it does not affect the main thread of the application nor the React Native JavaScript thread. [(docs)](#threading-and-javascript-in-react-native)
32
+ * The Godot main window and any subwindows created by the Godot app may be embedded into the React Native application either on the same screen, or on separate screens (see [example app](example/)).
33
+ * The whole Godot API is accessible from TypeScript / JavaScript. It is possible to instantiate objects, call methods, get and set properties, attach JS functions to signals, provide JS functions as callables to Godot methods ... etc. [(docs)](#godot-api-usage)
34
+
35
+ ## Getting Started with the Example App
36
+
37
+ The [example app](example/) shows the main features of React Native Godot in action.
38
+
39
+ ## Install prerequisites
40
+
41
+ During development this repository uses [ASDF](https://asdf-vm.com/) for the
42
+ Node.js, Java and Ruby versions recorded in `.tool-versions`. ASDF is optional;
43
+ you may install the React Native prerequisites with any supported toolchain.
44
+ If you use ASDF, run:
45
+
46
+ ```sh
47
+ asdf install
48
+ ```
49
+
50
+ This installs the versions used in our development environment. Otherwise you
51
+ may install the React Native prerequisites using any supported method.
52
+
53
+ ## Export the Godot app
54
+
55
+ Run the following scripts for either platform you plan to test (or both):
56
+
57
+ ```sh
58
+ cd example
59
+ ./export_godot_GodotTest.sh android
60
+ ./export_godot_GodotTest.sh ios
61
+ ./export_godot_GodotTest2.sh android
62
+ ./export_godot_GodotTest2.sh ios
63
+ ```
64
+
65
+ The script is configured to look for Godot in the standard system wide installation folder on macOS. If your Godot is installed elsewhere, or you are on Linux, just point the `GODOT_EDITOR`
66
+ environment variable to your Godot editor prior to running the above scripts:
67
+
68
+ ```sh
69
+ export GODOT_EDITOR=/path/to/godot_editor
70
+ ```
71
+
72
+ ## Install dependencies and LibGodot
73
+
74
+ ```sh
75
+ yarn
76
+ ```
77
+
78
+ Installing the dependencies also downloads the prebuilt LibGodot release from GitHub. Existing prebuilt files are reused on subsequent installs.
79
+
80
+ ## Run on the iOS Simulator
81
+
82
+ ```sh
83
+ cd example
84
+ bundle install
85
+ cd ios
86
+ bundle exec pod install
87
+ cd ..
88
+ cd ..
89
+ yarn ios
90
+ ```
91
+
92
+ ## Run on the Android Emulator
93
+
94
+ ```sh
95
+ yarn android
96
+ ```
97
+
98
+ ## Use your native IDEs
99
+
100
+ You may use Xcode and Android Studio the same way as with any other project. Just open:
101
+
102
+ * ``ios/RTNGodotViewExample.xcworkspace`` from Xcode
103
+ * ``android`` from Android Studio
104
+
105
+ > [!note]
106
+ > If you are using ASDF to manage your Java and Node dependencies, you should start Android Studio from under the `react-native-godot` (or `example`) folder, so it can find these tools. For example on macOS:
107
+
108
+ ```sh
109
+ cd example
110
+ open -a "Android Studio"
111
+ ```
112
+
113
+ ## Convenience script for dependency management
114
+
115
+ There is an `update_deps.sh` script included in the example app's folder. It will execute all the setup commands for both iOS and Android in one step, so you may start your work immediately.
116
+
117
+ ```sh
118
+ cd example
119
+ ./update_deps.sh
120
+ yarn ios # or yarn android
121
+ ```
122
+
123
+ ## Your first React Native Godot App
124
+
125
+ This fork is being prepared for its first npm release under
126
+ `@preeternal/react-native-godot`. The installation commands below describe the
127
+ intended published package.
128
+
129
+ Just follow these steps to add it to your React Native application:
130
+
131
+ ## Compatibility
132
+
133
+ The example is a React Native Community CLI project aligned with React Native 0.88.0 and React 19.3.0. It uses the New Architecture, Hermes, Kotlin on Android, Swift app entrypoints on iOS, and `react-native-worklets` for Godot-thread calls.
134
+
135
+ On iOS, `RTNGodotView` initializes Fabric's `_props` with generated `RTNGodotViewProps` defaults before the first prop update, matching the Fabric contract used by React Native 0.88.
136
+
137
+ The Android integration requires Android 10 (API 29) because window embedding uses `SurfaceControl`. The bundled Godot binaries currently support `armeabi-v7a` and `arm64-v8a`; the example restricts `reactNativeArchitectures` to those ABIs so its APK never advertises an x86 variant that cannot load Godot.
138
+
139
+ ## Expo Dev Builds
140
+
141
+ The package works in bare React Native Community CLI apps and in Expo Dev
142
+ Builds (custom native builds). It cannot run in Expo Go because it contains
143
+ native Godot code.
144
+
145
+ Expo projects should register the package config plugin in `app.json` or
146
+ `app.config.js`:
147
+
148
+ ```json
149
+ {
150
+ "expo": {
151
+ "plugins": [
152
+ [
153
+ "@preeternal/react-native-godot",
154
+ {
155
+ "iosPacks": ["./assets/GodotTest.pck", "./assets/GodotTest2.pck"]
156
+ }
157
+ ]
158
+ ]
159
+ }
160
+ }
161
+ ```
162
+
163
+ The `iosPacks` option is optional. The plugin configures the Android minimum
164
+ SDK, supported ARM ABIs, local LibGodot Maven repository and the asset pattern
165
+ needed to retain hidden `.godot` resources, including after clean prebuilds. On iOS it
166
+ adds the listed PCK files to the Xcode project. Run `npx expo prebuild` after
167
+ changing the plugin configuration.
168
+
169
+ ## Android Godot assets
170
+
171
+ When an Android Godot project is bundled as an unpacked directory and started
172
+ with `--path`, its exported scenes and imported resources are stored under the
173
+ hidden `.godot` directory. Android Gradle Plugin excludes hidden asset paths by
174
+ default, so the application module must keep them in the APK:
175
+
176
+ ```groovy
177
+ android {
178
+ androidResources {
179
+ ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"
180
+ }
181
+ }
182
+ ```
183
+
184
+ Without this setting Godot may start without its exported `main.scn`; later
185
+ access to the scene tree can then fail natively. The setting belongs in the
186
+ consuming Community CLI application's `android/app/build.gradle` (the example
187
+ already has it). Expo projects receive this configuration from the package
188
+ config plugin; no manual changes to generated Gradle files are needed.
189
+
190
+ ## Update `package.json`
191
+
192
+ ```sh
193
+ yarn add @preeternal/react-native-godot @dr.pogodin/react-native-fs react-native-worklets
194
+ ```
195
+
196
+ For a React Native Community CLI project, the package is autolinked normally. Install CocoaPods after changing native dependencies and keep the Worklets Babel plugin enabled according to the `react-native-worklets` installation guide.
197
+
198
+ ## Prebuilt LibGodot packages
199
+
200
+ The LibGodot packages used by React Native Godot are not distributed on npm. The package downloads and verifies them automatically during installation.
201
+ The current package uses LibGodot `4.5.1.preeternal.1`, based on Godot `4.5.1`.
202
+
203
+ To repeat the download manually, for example after setting local LibGodot override paths, run:
204
+
205
+ ```sh
206
+ yarn download-prebuilt
207
+ ```
208
+
209
+ This keeps React Native Godot independent from LibGodot releases while still supporting local customized builds.
210
+
211
+ The bundled Android LibGodot artifacts currently contain `arm64-v8a` and `armeabi-v7a`. The Gradle module automatically limits its native build to those ABIs. Running the Godot view therefore requires an ARM Android device or ARM emulator.
212
+
213
+ ## Import React Native Godot in your App code
214
+
215
+ ```typescript
216
+ import { RTNGodot, RTNGodotView, runOnGodotThread } from "@preeternal/react-native-godot";
217
+ ```
218
+
219
+ ## Swift Package Manager (React Native 0.87+)
220
+
221
+ The package includes the `Package.swift` manifest required by React Native's
222
+ experimental opt-in SwiftPM integration. CocoaPods remains the default path.
223
+ To enable SwiftPM in a Community CLI app, run:
224
+
225
+ ```sh
226
+ cd ios
227
+ npx react-native spm --deintegrate
228
+ npx react-native spm
229
+ ```
230
+
231
+ The generated React Native package supplies the React headers and codegen
232
+ products referenced by this library. SwiftPM support requires iOS 15 or newer;
233
+ the manifest is intended to be consumed through React Native's SPM autolinker,
234
+ not as a standalone Swift package with independently resolved React headers.
235
+ It also depends on the `RNWorklets` product supplied by the installed
236
+ `react-native-worklets` package. All native dependencies in the app must have
237
+ compatible SwiftPM manifests.
238
+
239
+ ## Add the Godot View to your view, e.g.
240
+
241
+ ```tsx
242
+ const App = () => {
243
+ return (
244
+ <View>
245
+ <RTNGodotView style={...}/>
246
+ </View>
247
+ );
248
+ };
249
+ ```
250
+
251
+ If no `windowName` property is specified, that view is for the main window of Godot.
252
+
253
+ ## Initialize the Godot instance
254
+
255
+ ```typescript
256
+ import { MainBundlePath } from '@dr.pogodin/react-native-fs';
257
+
258
+ function initGodot() {
259
+ runOnGodotThread(() => {
260
+ 'worklet';
261
+ console.log("Initializing Godot");
262
+
263
+ if (Platform.OS === 'android') {
264
+ RTNGodot.createInstance([
265
+ "--verbose",
266
+ "--path", "/main",
267
+ "--rendering-driver", "opengl3",
268
+ "--rendering-method", "gl_compatibility",
269
+ "--display-driver", "embedded"
270
+ ]);
271
+ } else {
272
+ RTNGodot.createInstance([
273
+ "--verbose",
274
+ "--main-pack", `${MainBundlePath}/main.pck`,
275
+ "--rendering-driver", "opengl3",
276
+ "--rendering-method", "gl_compatibility",
277
+ "--display-driver", "embedded"
278
+ ]);
279
+ }
280
+ }
281
+ }
282
+ ```
283
+
284
+ A couple of things to note here:
285
+
286
+ * The usual Godot command line parameters can be passed to the initialization function.
287
+ * It is key to use the "embedded" display driver, which is required to embed Godot into the React Native application.
288
+ * It is possible to specify both a directory or a pack file.
289
+ * On Android, inside the main package the access of the pack file's contents is much slower than accessing pack files stored in the private area of the application. If the Godot app is stored inside the main package, then it should be stored as a folder of files in the `asset` folder.
290
+ * On iOS, there is no such limitation, so we use a pack file there.
291
+ * In many cases the best way is to download the Godot apps at runtime, which has many advantages, including:
292
+ * Smaller initial application size
293
+ * Deliver specialized app builds for different devices
294
+ * Update the Godot app without going through the whole app review process
295
+
296
+ ## Add the Godot initialization to the App component
297
+
298
+ ```tsx
299
+ const App = () => {
300
+ useEffect(() => {
301
+ initGodot()
302
+ return () => {};
303
+ }, []);
304
+
305
+ return (
306
+ <View>
307
+ <RTNGodotView style={...}/>
308
+ </View>
309
+ );
310
+ };
311
+ ```
312
+
313
+ In this Hello World app, we just initialize Godot when the App view is displayed.
314
+ Check out the [example app](example/) in this repository for a more elaborate example.
315
+
316
+ ## Stop the Godot instance
317
+
318
+ To stop a running Godot instance, call `RTNGodot.destroyInstance()` on the Godot thread:
319
+
320
+ ```typescript
321
+ function destroyGodot() {
322
+ runOnGodotThread(() => {
323
+ "worklet";
324
+ RTNGodot.destroyInstance();
325
+ });
326
+ }
327
+ ```
328
+
329
+ > [!note]
330
+ > After stopping an instance, you can start a new one by calling `RTNGodot.createInstance` again. The new instance can be started with different parameters including other Godot projects.
331
+
332
+ ## Pause the Godot instance
333
+
334
+ To pause a running Godot instance, call `RTNGodot.pause()` on the JavaScript main thread:
335
+
336
+ ```typescript
337
+ RTNGodot.pause();
338
+ ```
339
+
340
+ This won't shut down the running instance, just halt it's execution until further notice.
341
+
342
+ ## Resume the Godot instance
343
+
344
+ To resume a paused Godot instance, call `RTNGodot.resume()` on the JavaScript main thread:
345
+
346
+ ```typescript
347
+ RTNGodot.resume();
348
+ ```
349
+
350
+ ## Export your Godot project
351
+
352
+ You may use the usual export functionality of Godot Engine, just make sure to export to PCK or ZIP and not the whole application.
353
+
354
+ In the React Native Godot example application an `export_godot.sh` and an associated `export_godot_GodotTest.sh` script is provided, which can make the export process easier.
355
+
356
+ To export your project to iOS or Android, use our included `export_godot.sh` script with the following parameters:
357
+
358
+ ```
359
+ --target: Base directory where the project will be exported. Depending on the platform a pck file or a folder will be created here.
360
+ --project: The directory of the project that will be exported.
361
+ --name: The name of the exported project.
362
+ --preset: The export preset to be used.
363
+ --platform: The platform to export the project to (iOS or Android).
364
+ ```
365
+
366
+ Notes:
367
+
368
+ * Android projects will be exported into project folders while iOS project will be exported as PCK files.
369
+ * By default, `export_godot.sh` will look for an official Godot installation under the Applications folder. If your installation is somewhere else or you would like to use a custom Godot build, specify it's location in the `GODOT_EDITOR` environment variable.
370
+
371
+ ## Godot API Usage
372
+
373
+ After importing React Native Godot, you can access the Godot API through the `RTNGodot` class.
374
+
375
+ ### Get the API entry point
376
+
377
+ To access the Godot API, first call `RTNGodot.API()`:
378
+
379
+ ```typescript
380
+ let Godot = RTNGodot.API();
381
+ ```
382
+
383
+ ### Access Singletons
384
+
385
+ From the entry point, you can get the engine's singletons:
386
+
387
+ ```typescript
388
+ // Get Godot's Engine singleton.
389
+ let Godot = RTNGodot.API();
390
+ var engine = Godot.Engine;
391
+ ```
392
+
393
+ ### Instantiate Objects
394
+
395
+ You can instantiate Godot API objects using their defined constructors:
396
+
397
+ ```typescript
398
+ var vector = Godot.Vector2();
399
+ ```
400
+
401
+ ### Access properties
402
+
403
+ Object properties can be accessed directly:
404
+
405
+ ```typescript
406
+ var vector = Godot.Vector2();
407
+ vector.x = 1.0;
408
+ vector.y = 2.0;
409
+ ```
410
+
411
+ ### Call methods
412
+
413
+ Godot API methods can also be called:
414
+
415
+ ```typescript
416
+ let Godot = RTNGodot.API();
417
+ var engine = Godot.Engine;
418
+
419
+ // Call Godot API methods:
420
+ var sceneTree = engine.get_main_loop();
421
+ var root = sceneTree.get_root();
422
+ // NOTE: From here you can access nodes of the scene tree and manipulate them from TypeScript.
423
+ ```
424
+
425
+ ### Attach to signals
426
+
427
+ JS functions can be connected to Godot signals:
428
+
429
+ ```typescript
430
+ let Godot = RTNGodot.API();
431
+
432
+ var button = Godot.Button();
433
+ button.set_text("Button");
434
+
435
+ button.pressed.connect(function() {
436
+ console.log("Button pressed.")
437
+ })
438
+ ```
439
+
440
+ ### Pass JS functions to Godot
441
+
442
+ JS functions can be passed to Godot methods to be used as Callables:
443
+
444
+ Let's create a custom RNInterface Node, and add it to the Godot project's scene tree. It's GDScript should read:
445
+
446
+ ```gdscript
447
+ extends Node
448
+ class_name RNInterface
449
+
450
+ func test_callable(c: Callable) -> void:
451
+ c.call("Hello from Godot")
452
+ ```
453
+
454
+ From TypeScript we may access it with the following code snippet:
455
+
456
+ ```typescript
457
+ const Godot = RTNGodot.API();
458
+ const engine = Godot.Engine;
459
+ const sceneTree = engine.get_main_loop();
460
+ const root = sceneTree.get_root();
461
+
462
+ const iface = root.find_child("RNInterface", true, false);
463
+
464
+ iface.test_callable(function(s: string) {
465
+ console.log("Received text from Godot: " + s)
466
+ });
467
+ ```
468
+
469
+ ### Threading and JavaScript in React Native
470
+
471
+ In a React Native app, the main JavaScript thread, where the bulk of the JavaScript code of the application runs is separate from the Android or iOS apps's main thread.
472
+
473
+ This way the JS Thread's processing does not affect the main application UI. Following the same pattern, the Godot Engine is also running on its own thread that is separate from both the application's and React Native's main JavaScript thread. As JavaScript is single-threaded by design, to be able to communicate with the Godot thread from JavaScript, we use [react-native-worklets](https://docs.swmansion.com/react-native-worklets/), which allows us to run JS code in the Godot thread using worklets.
474
+
475
+ Worklets are JavaScript functions designated with a 'worklet' keyword.
476
+
477
+ ```typescript
478
+ function worklet() {
479
+ 'worklet'
480
+ }
481
+ ```
482
+
483
+ These functions and all their external dependencies are transformed so they can be executed in separate JS contexts associated with separate threads. For more information, refer to the [React Native Worklets documentation](https://docs.swmansion.com/react-native-worklets/).
484
+
485
+ React Native Godot provides a helper function called `runOnGodotThread()` which will allow you to execute such _workletized_ JS functions on the Godot thread.
486
+
487
+ In general, this is the recommended way of interacting with the Godot Engine, as shown in our example app.
488
+
489
+ While it is possible to interact with the Godot Engine directly from the main React Native thread, there are some caveats and it is generally not recommended:
490
+
491
+ * From the Godot Engine's view, such invocations will be executed on a "background" thread, which means, that for example the Scene Tree cannot be fully accessed from this thread directly. For more information on how background threads in Godot work, please refer to its [documentation](https://docs.godotengine.org/en/stable/tutorials/performance/thread_safe_apis.html).
492
+
493
+ * Godot object references that were obtained in the Main JS thread and in worklets are not interchangeable, because they are associated with separate JS contexts. It is possible to create object references that can work in multiple JS contexts, but these would add runtime overhead and are currently not supported in React Native Godot.
494
+
495
+ ## Advanced Topics
496
+
497
+ ### Using a custom LibGodot build
498
+
499
+ 1. To use a custom LibGodot build, you first need to clone the [Preeternal LibGodot project](https://github.com/Preeternal/libgodot), including all its submodules.
500
+
501
+ ```sh
502
+ git clone --recursive https://github.com/Preeternal/libgodot
503
+ ```
504
+
505
+ 1. Then build LibGodot from source:
506
+
507
+ ```sh
508
+ cd libgodot
509
+ ./build_prebuilt_release.sh # for release libraries
510
+ # OR
511
+ ./build_prebuilt_dev.sh # for development libraries
512
+ ```
513
+
514
+ NOTE: You may comment out the unnecessary lines in the build script if you're only building for one platform.
515
+
516
+ At the end of the process, the necessary files will be produced in the `build/prebuilt/release` or the `build/prebuilt/dev` folder.
517
+
518
+ 1. Once the build finished, set these environment variables:
519
+
520
+ ```sh
521
+ export LIBGODOT_XCFRAMEWORK_PATH=path/to/libgodot/build/prebuilt/release or dev/libgodot.xcframework.zip
522
+ export LIBGODOT_CPP_XCFRAMEWORK_PATH=path/to/libgodot/build/prebuilt/release or dev/libgodot-cpp.xcframework.zip
523
+ export LIBGODOT_ANDROID_PATH=path/to/libgodot/build/prebuilt/release or dev/libgodot-android.zip
524
+ export LIBGODOT_CPP_ANDROID_PATH=path/to/libgodot/build/prebuilt/release or dev/godot-cpp-android.zip
525
+ export SHASUM_CHECK=false
526
+ export REPLACE_EXISTING=true
527
+ ```
528
+
529
+ These environment variables override the download logic of the download-prebuilt script of `react-native-godot`:
530
+
531
+ * Use locally present files instead of the URLs specified in the `package.json` of `react-native-godot`.
532
+ * Do not check the provided SHA-256 hash.
533
+ * Force replacing the installed library, even if the same version was already installed.
534
+
535
+
536
+ 1. Then run the download-prebuilts script to install the custom built libraries:
537
+
538
+ ```sh
539
+ yarn download-prebuilt
540
+ ```
541
+
542
+ NOTE: An example usage of this script can be found in our example/update_deps.sh script.
543
+
544
+ 1. On Android, if using the development builds, in the `react-native-godot/android/build.gradle` the
545
+ following line needs to be updated to include `godot-dev` as the artifactId:
546
+
547
+ ```groovy
548
+ api "com.migeran.libgodot:godot-debug:${libGodotVersion}-SNAPSHOT"
549
+ ```
550
+
551
+ ### Debug Native Godot code
552
+
553
+ To debug native engine code (C++), you first need to build and install a development version of LibGodot as described above.
554
+
555
+ On iOS this allows you to set breakpoints into the Godot Engine itself in Xcode and debug it as easily as your own app.
556
+
557
+ Depending on the Xcode version, you might have to open these files separately in Xcode, and then move the editor from the new window into the window your application's Xcode project.
558
+
559
+ On Android a couple more steps will be required:
560
+
561
+ * In the Android Studio Run/Debug configurations you also need to add a Symbol directory, where `libgodot_android.so` with the included debug symbols is located. One good place is: `path/to/libgodot/godot/platform/android/java/lib/libs/dev/arm64-v8a`.
562
+
563
+ * Depending on the Android Studio version, you may have to include the Godot source tree under the Android Studio project to be able to set breakpoints. A way to do this is to create a symbolic link under the React Native Godot library as follows:
564
+
565
+ ```sh
566
+ cd /path/to/your/app/node_modules/@preeternal/react-native-godot/android/src/main/cpp
567
+ ln -s /path/to/libgodot/godot godot
568
+ ```
569
+
570
+ * Then reimport the project / resync the Gradle project files into Android Studio.
571
+
572
+ ### Debugging Godot Projects in the Godot Editor
573
+
574
+ #### Android
575
+
576
+ 1. For remote debugging, you first need to build and install a development version of LibGodot as described above.
577
+
578
+ 1. Then add these 2 extra parameters to your `RTNGodot.createInstance` call:
579
+
580
+ ```typescript
581
+ '--remote-debug',
582
+ 'tcp://127.0.0.1:6007'
583
+ ```
584
+
585
+ 1. To expose TCP port 6007 on your Android device via TCP port 6007 on your computer, open a terminal and call:
586
+
587
+ ```sh
588
+ adb reverse tcp:6007 tcp:6007
589
+ ```
590
+
591
+ NOTE: This needs to be called again every time you disconnect and reconnect your device.
592
+
593
+ 1. Now you can open your project in the Godot Editor. First enable 'Debug' -> 'Keep Debug Server Open' from the top menu then you can set your breakpoints. You can also set breakpoints while the app is running.
594
+
595
+ 1. Once your breakpoints are set, you can start debugging by launching your app from Android Studio.
596
+
597
+ #### iOS
598
+
599
+ 1. For remote debugging, you first need to build and install a development version of LibGodot as described above.
600
+
601
+ 1. Open the Godot Editor, then go to 'godot' -> 'Editor Settings' -> 'Network' -> 'Debug' -> 'Remote Host' and set your Mac's IP address.
602
+
603
+ 1. Also enable 'Debug' -> 'Keep Debug Server Open'.
604
+
605
+ 1. Then add these 2 extra parameters to your `RTNGodot.createInstance` call:
606
+
607
+ ```typescript
608
+ '--remote-debug',
609
+ 'tcp://<your_macs_ip_address>:6007'
610
+ ```
611
+
612
+ 1. Now you can start debugging by launching your app from Xcode. You can set your breakpoints both before running the app or at runtime.
613
+
614
+ NOTE: Your Mac and your iOS device must be connected to the same network/router in order to establish the connection.
615
+
616
+ ## Project
617
+
618
+ Issues and pull requests are welcome in the [GitHub repository](https://github.com/Preeternal/react-native-godot).
619
+
620
+ ## Contributing
621
+
622
+ Contributions are welcome.
623
+
624
+ - [Development workflow](CONTRIBUTING.md#development-workflow)
625
+ - [Sending a pull request](CONTRIBUTING.md#sending-a-pull-request)
626
+ - [Code of conduct](CODE_OF_CONDUCT.md)
627
+
628
+ ## License
629
+
630
+ MIT. See [LICENSE](LICENSE) for details.
631
+
632
+ Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob).