React Native Godot

repository·master·Indexed 25 days ago

https://github.com/borndotcom/react-native-godot

A library for embedding the Godot Engine into React Native applications on Android and iOS. It provides a bridge to access the full Godot API from TypeScript/JavaScript, allowing for high-performance game or 3D content within native mobile apps. Features include the RTNGodotView component for rendering, lifecycle control (Pause, Resume, Stop), and the ability to connect JS functions to Godot signals or pass them as Callables.

Tokens
3.7K
Snippets
11
Records
20
Agent score
82%

What's inside @borndotcom/react-native-godot

  1. Install @borndotcom/react-native-godot

    master

    To add React Native Godot to your existing React Native project, follow these steps:

    1. Install the npm package:
    yarn add @borndotcom/react-native-godot
    1. Download the prebuilt LibGodot packages (required as they are not distributed via npm):
    yarn download-prebuilt
    yarn add @borndotcom/react-native-godot
    yarn download-prebuilt
  2. Remote debug Godot projects on Android

    master

    To debug your Godot project using the Godot Editor on Android:

    1. Build and install a development version of LibGodot.
    2. Pass the following arguments to RTNGodot.createInstance:
      • --remote-debug
      • tcp://127.0.0.1:6007
    3. Expose the port on your device using ADB:
      adb reverse tcp:6007 tcp:6007
    4. In the Godot Editor, go to Debug -> Keep Debug Server Open.
    5. Launch the app from Android Studio to start debugging.
    // Inside RTNGodot.createInstance
    '--remote-debug',
    'tcp://127.0.0.1:6007'
  3. Remote debug Godot projects on iOS

    master

    To debug your Godot project using the Godot Editor on iOS:

    1. Build and install a development version of LibGodot.
    2. In the Godot Editor, go to godot -> Editor Settings -> Network -> Debug -> Remote Host and enter your Mac's IP address.
    3. Enable Debug -> Keep Debug Server Open in the Godot Editor.
    4. Pass the following arguments to RTNGodot.createInstance:
      • --remote-debug
      • tcp://<your_macs_ip_address>:6007
    5. Launch the app from Xcode. Ensure your Mac and iOS device are on the same network.
    // Inside RTNGodot.createInstance
    '--remote-debug',
    'tcp://<your_macs_ip_address>:6007'
  4. Export a Godot project for React Native

    master

    To prepare your Godot project for use with this library, export it as a PCK or ZIP file rather than a full application.

    If using the provided export_godot.sh script, use the following arguments:

    • --target: Base directory for the exported files.
    • --project: Directory of the Godot project.
    • --name: Name of the exported project.
    • --preset: The export preset.
    • --platform: ios or android.

    Note: Android exports result in project folders, while iOS exports result in .pck files. If your Godot editor is not in the default location, set the GODOT_EDITOR environment variable.

  5. Use a custom LibGodot build

    master

    If you need to use a custom build of LibGodot instead of the prebuilt binaries, follow these steps:

    1. Clone LibGodot: Clone the repository on the libgodot_migeran_45 branch with submodules.
      git clone --recursive https://github.com/migeran/libgodot -b libgodot_migeran_45
    2. Build from source: Run the build script for either release or development libraries.
      cd libgodot
      ./build_prebuilt_release.sh   # for release
      # OR
      ./build_prebuilt_dev.sh      # for development
    3. Set Environment Variables: Set the following variables to point to your local build artifacts. This overrides the default download logic.
      • LIBGODOT_XCFRAMEWORK_PATH
      • LIBGODOT_CPP_XCFRAMEWORK_PATH
      • LIBGODOT_ANDROID_PATH
      • LIBGODOT_CPP_ANDROID_PATH
      • SHASUM_CHECK=false
      • REPLACE_EXISTING=true
    4. Install: Run the download script to link your custom builds.
      yarn download-prebuilt
    5. Android Dev Configuration: If using development builds, update react-native-godot/android/build.gradle to use godot-debug as the artifactId:
      api "com.migeran.libgodot:godot-debug:${libGodotVersion}-SNAPSHOT"
  6. Debug native C++ Godot code

    master

    To debug the Godot Engine's C++ source code:

    iOS: Build and install a development version of LibGodot. You can then set breakpoints directly in Xcode.

    Android:

    1. In Android Studio, add a Symbol directory in the Run/Debug configurations pointing to where libgodot_android.so (with debug symbols) is located (e.g., path/to/libgodot/godot/platform/android/java/lib/libs/dev/arm64-v8a).
    2. To enable breakpoints in the Godot source, create a symbolic link in your project's native directory:
      cd /path/to/your/app/node_modules/@borndotcom/react-native-godot/android/src/main/cpp
      ln -s /path/to/libgodot/godot godot
    3. Reimport/resync the Gradle project in Android Studio.
    cd /path/to/your/app/node_modules/@borndotcom/react-native-godot/android/src/main/cpp
    ln -s /path/to/libgodot/godot godot
  7. Download prebuilt Godot binaries

    master

    Use the download-prebuilt script to fetch and extract pre-built Godot zip files required by the library. The script reads configuration from the @borndotcom/react-native-godot package.json and handles downloading, checksum verification, and extraction.

    Key behaviors:

    • Automatic Extraction: Unzips files into the configured destination directory.
    • Checksum Verification: Automatically verifies SHA-256 checksums using shasum to ensure file integrity.
    • Local Overrides: If a specific environment variable (defined in package.json) is set, the script will use a local archive file instead of downloading from the network.
    • Directory Management: If the target extraction folder already exists and is not empty, the script skips that entry unless forced to replace it.
    yarn download-prebuilt
  8. Connect JS functions to Godot signals

    master

    You can attach JavaScript functions to Godot signals using the .connect() method on the signal object.

    const Godot = RTNGodot.API();
    const button = Godot.Button();
    button.set_text("Button");
    
    button.pressed.connect(function() {
      console.log("Button pressed.");
    });
  9. Pass JS functions as Callables to Godot

    master

    You can pass JavaScript functions to Godot methods to be used as Callable objects. This allows Godot (GDScript) to trigger logic back in your React Native environment.

    const Godot = RTNGodot.API();
    const engine = Godot.Engine;
    const sceneTree = engine.get_main_loop();
    const root = sceneTree.get_root();
    
    // Find a node in the scene tree
    const iface = root.find_child("RNInterface", true, false);
    
    // Pass a JS function as a Callable to a Godot method
    iface.test_callable(function(s: string) {
      console.log("Received text from Godot: " + s);
    });
  10. Execute JavaScript on the Godot thread using runOnGodotThread()

    master

    To interact with the Godot Engine safely, it is recommended to run JavaScript code on the Godot thread rather than the main React Native thread. This prevents issues with Godot's Scene Tree access and thread safety.

    To do this, you must use react-native-worklets-core. Define your function as a 'worklet' and then use the runOnGodotThread() helper function provided by React Native Godot.

    Note: Godot object references obtained in the Main JS thread and in worklets are not interchangeable because they belong to separate JS contexts.

  11. Initialize a Godot instance

    master

    To start the Godot engine, use RTNGodot.createInstance inside a runOnGodotThread block. You must pass the --display-driver parameter as embedded to allow Godot to be embedded within the React Native application.

    Important Platform Notes:

    • Android: It is recommended to store Godot assets in the asset folder of the main package for better performance. You can pass a --path to a directory.
    • iOS: You can pass a --main-pack path to a .pck file.
    import { RTNGodot, runOnGodotThread } from "@borndotcom/react-native-godot";
    import * as FileSystem from 'expo-file-system/legacy';
    
    function initGodot() {
      runOnGodotThread(() => {
        'worklet';
        if (Platform.OS === 'android') {
          RTNGodot.createInstance([
            "--verbose",
            "--path", "/main",
            "--rendering-driver", "opengl3",
            "--rendering-method", "gl_compatibility",
            "--display-driver", "embedded"
          ]);  
        } else {
          RTNGodot.createInstance([
            "--verbose",
            "--main-pack", FileSystem.bundleDirectory + "main.pck",
            "--rendering-driver", "opengl3",
            "--rendering-method", "gl_compatibility",
            "--display-driver", "embedded"
          ]);  
        }
      });
    }