xur_

> module

std::stub::godot

Godot (.NET) engine attach, lifecycle and scene tree

use std::stub::godot; feature godot native only

std::stub::godot attaches to the CoreCLR runtime of a Godot .NET game and returns it as a std::stub::coreclr Runtime. It also sets up a per-frame pump on the game's main thread, so scripts can run code there (on_process(), dispatch(), dispatch_call()), walk the scene tree, and read texture pixels.

Godot's API is main-thread only. The script's own threads (stub_main()) must go through dispatch() or dispatch_call() to touch nodes. The pump has to be set up first, with hook_scene_tree() (any Godot game) or intercept_process() (a specific game method); until then dispatching raises.

Nodes and textures are coreclr::ManagedObject values (raw object pointers). Errors are GodotError: NullPointer, NotInitialized or Failed(message).

> functions

# child(node: ManagedObject, index: i64) -> Result<ManagedObject, GodotError>

The indexth child of node.

# child_count(node: ManagedObject) -> Result<i64, GodotError>

How many children node has.

# dispatch(callback: fn())

Queue callback to run on the main thread on the next frame and return at once. Raises if no pump is running. Errors in callback are logged.

# dispatch_call(callback: fn() -> T) -> T

Run callback on the main thread and wait for its result. Raises if no pump is running, or if callback raises.

# hook_scene_tree()

Ask for a per-frame pump on GodotSharp's SceneTree, which works for any Godot .NET game. Call it from ctor() so the GodotSharp assembly can be rewritten as it loads; if it was already loaded (e.g. the stub was injected), the pump falls back to patching native code when init() attaches.

# init(major: i64) -> Result<Runtime, GodotError>

Attach to the game's CoreCLR, where major is the game's .NET major version (e.g. 9). Blocks until the game has initialised the CLR (immediately if it already has), then starts the SceneTree pump if hook_scene_tree() asked for it. Returns the coreclr::Runtime for finding and calling managed methods.

# intercept_process(assembly: str, type_name: str, method: str)

Pump from a specific game method instead: rewrite method of type_name (e.g. "Game.MainNode", "_Process") in the assembly file assembly (e.g. "game.dll") to tick the pump each time it runs. Must be called before that assembly loads. Only on x86_64 Unix; a no-op elsewhere.

# is_shutting_down() -> bool

Whether shutdown() has been called.

# node_name(node: ManagedObject) -> Result<str, GodotError>

The node's ToString() (Godot's GodotObject string form), or "?" if that is null.

# on_process(callback: fn())

Run callback once per frame on the game's main thread, from whichever pump is set up. Errors it raises are logged.

# resolve_image_bindings() -> Result<(), str>

Resolve the Godot texture, image and rendering-server methods the texture readers use. Call it once from the script thread (not the main thread) before texture_to_rgba() or texture_to_rgba_direct().

# root() -> Result<ManagedObject, GodotError>

The scene tree's root Window node.

# scene_tree() -> Result<ManagedObject, GodotError>

The game's SceneTree (from Engine.GetMainLoop).

# shutdown()

Stop all per-frame callbacks. Call it when the game is exiting, so no callback runs during engine teardown.

# texture_to_rgba(tex: ManagedObject) -> Result<(i64, i64, bytes), str>

Read a Texture2D back from the GPU as (width, height, rgba) with 4 bytes per pixel. An AtlasTexture is cropped from its atlas, which is read once and cached; anything else falls back to texture_to_rgba_direct().

# texture_to_rgba_direct(tex: ManagedObject) -> Result<(i64, i64, bytes), str>

Read the whole texture back from the GPU as (width, height, rgba), without atlas handling.

> types

┌ enum GodotError
GodotError::NullPointer
GodotError::NotInitialized
GodotError::Failed(str)

> functions

These call Godot and so must run on the main thread.

> examples

use std::stub::godot;
use std::log;

fn ctor() {
    godot::hook_scene_tree();
}

fn stub_main() -> Result<()> {
    let rt = godot::init(9)?;

    godot::on_process(|| {
        // runs every frame on the game thread
    });

    let n = godot::dispatch_call(|| {
        let root = godot::root().unwrap();
        godot::child_count(root).unwrap()
    });
    log::info("root has {} children", n);
    Ok(())
}