React Native Godot - Embed Godot Engine in React Native apps
React Native Godot - Embed Godot Engine in React Native apps
React Native Godot allows embedding the Godot Engine into React Native applications.
Born React Native Godot was created by Born and developed by Migeran, in close collaboration between the two teams.
The example app shows the main features of React Native Godot in action.
During development we use ASDF to manage most external dependencies required for React Native development, like Node, Java, Gradle or Ruby. If you also use ASDF, just run:
asdf installThis will make sure that all the dependencies are the same like in our environment. Otherwise you may also install React Native prerequisites using any other method.
Run the following scripts for either platform you plan to test (or both):
cd example
./export_godot_GodotTest.sh android
./export_godot_GodotTest.sh ios
./export_godot_GodotTest2.sh android
./export_godot_GodotTest2.sh iosThe 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
environment variable to your Godot editor prior to running the above scripts:
export GODOT_EDITOR=/path/to/godot_editorcd example
yarn
yarn download-prebuiltThese commands will resolve all the React Native and other dependencies from npm. The second one will download the prebuilt LibGodot release from GitHub.
cd example/ios
bundle install
bundle exec pod install
cd ..
yarn ioscd example
yarn androidYou may use Xcode and Android Studio the same way as with any other project. Just open:
ios/GodotTest.xcworkspace from Xcodeandroid from Android Studio[!note] If you are using ASDF to manage your Java and Node dependencies, you should start Android Studio from under the
react-native-godot(orexample) folder, so it can find these tools. For example on macOS:
cd example
open -a "Android Studio"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.
cd example
./update_deps.sh
yarn ios # or yarn androidBorn React Native Godot is distributed on npm.
Just follow these steps to add it to your React Native application:
package.jsonyarn add @borndotcom/react-native-godotThe LibGodot packages used by React Native Godot are not distributed on npm. Instead, they are downloaded separately by issuing the following command:
yarn download-prebuiltThis way React Native Godot can be updated independently from LibGodot, and also local, customized builds of LibGodot are supported.
import { RTNGodot, RTNGodotView, runOnGodotThread } from "@borndotcom/react-native-godot";const App = () => {
return (
<View>
<RTNGodotView style={...}/>
</View>
);
};If no windowName property is specified, that view is for the main window of Godot.
We will also add Expo Filesystem module for handling file system paths from React Native easily.
yarn add expo-file-system…A couple of things to note here:
asset folder. const App = () => {
useEffect(() => {
initGodot()
return () => {};
}, []);
return (
<View>
<RTNGodotView style={...}/>
</View>
);
};In this Hello World app, we just initialize Godot when the App view is displayed. Check out the example app in this repository for a more elaborate example.
To stop a running Godot instance, call RTNGodot.destroyInstance() on the Godot thread:
function destroyGodot() {
runOnGodotThread(() => {
"worklet";
RTNGodot.destroyInstance();
});
}[!note] After stopping an instance, you can start a new one by calling
RTNGodot.createInstanceagain. The new instance can be started with different parameters including other Godot projects.
To pause a running Godot instance, call RTNGodot.pause() on the JavaScript main thread:
RTNGodot.pause();This won't shut down the running instance, just halt it's execution until further notice.
To resume a paused Godot instance, call RTNGodot.resume() on the JavaScript main thread:
RTNGodot.resume();You may use the usual export functionality of Godot Engine, just make sure to export to PCK or ZIP and not the whole application.
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.
To export your project to iOS or Android, use our included export_godot.sh script with the following parameters:
--target: Base directory where the project will be exported. Depending on the platform a pck file or a folder will be created here.
--project: The directory of the project that will be exported.
--name: The name of the exported project.
--preset: The export preset to be used.
--platform: The platform to export the project to (iOS or Android).Notes:
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.After importing React Native Godot, you can access the Godot API through the RTNGodot class.
To access the Godot API, first call RTNGodot.API():
let Godot = RTNGodot.API();From the entry point, you can get the engine's singletons:
// Get Godot's Engine singleton.
let Godot = RTNGodot.API();
var engine = Godot.Engine;You can instantiate Godot API objects using their defined constructors:
var vector = Godot.Vector2();Object properties can be accessed directly:
var vector = Godot.Vector2();
vector.x = 1.0;
vector.y = 2.0;Godot API methods can also be called:
let Godot = RTNGodot.API();
var engine = Godot.Engine;
// Call Godot API methods:
var sceneTree = engine.get_main_loop();
var root = sceneTree.get_root();
// NOTE: From here you can access nodes of the scene tree and manipulate them from TypeScript.JS functions can be connected to Godot signals:
let Godot = RTNGodot.API();
var button = Godot.Button();
button.set_text("Button");
button.pressed.connect(function() {
console.log("Button pressed.")
})JS functions can be passed to Godot methods to be used as Callables:
Let's create a custom RNInterface Node, and add it to the Godot project's scene tree. It's GDScript should read:
extends Node
class_name RNInterface
func test_callable(c: Callable) -> void:
c.call("Hello from Godot")From TypeScript we may access it with the following code snippet:
const Godot = RTNGodot.API();
const engine = Godot.Engine;
const sceneTree = engine.get_main_loop();
const root = sceneTree.get_root();
const iface = root.find_child("RNInterface", true, false);
iface.test_callable(function(s: string) {
console.log("Received text from Godot: " + s)
}); 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.
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 the well-known react-native-worklets-core library, which allows us running JS code in the Godot thread using worklets.
Worklets are JavaScript functions designated with a 'worklet' keyword.
function worklet() {
'worklet'
}These functions and all their external dependencies are transpiled into self contained JS bundles so they can be executed in separate JS contexts associated with separate threads. For more information on how this works, please refer to the React Native Worklets Core documentation.
React Native Godot provides a helper function called runOnGodotThread() which will allow you to execute such workletized JS functions on the Godot thread.
In general, this is the recommended way of interacting with the Godot Engine, as shown in
No open issues yet, or sync has not completed.