#godot #tokio #gdext #async #gdextention

godot_tokio

The tokio-async runtime wrapped in a gdextention object to be used as an engine singleton in your gdext project

6 releases

0.1.6 Dec 31, 2024
0.1.5 Dec 28, 2024
0.1.3 Nov 30, 2024

#406 in Asynchronous

Download history 248/week @ 2024-11-27 124/week @ 2024-12-04 6/week @ 2024-12-11 183/week @ 2024-12-25 71/week @ 2025-01-01

265 downloads per month

MIT license

6KB
50 lines

godot_tokio

This was made to prevent re-typing out the boilerplate for creating a tokio runtime godot object.

Example Use snippets

You can simply create an engine singleton that makes the Tokio runtime accessable by all godot objects.

#[gdextension]
unsafe impl ExtensionLibrary for Metaphy {
    fn on_level_init(level: InitLevel) {
        match level {
            InitLevel::Scene => {
                Engine::singleton().register_singleton(AsyncRuntime::SINGLETON, &AsyncRuntime::new_alloc());
            }
            _ => (),
        }
    }

    fn on_level_deinit(level: InitLevel) {
        match level {
            InitLevel::Scene => {
                let mut engine = Engine::singleton();

                if let Some(async_singleton) = engine.get_singleton(AsyncRuntime::SINGLETON) {
                    engine.unregister_singleton(AsyncRuntime::SINGLETON);
                    async_singleton.free();
                } else {
                    godot_warn!(
                        "Failed to find & free singleton -> {}",
                        AsyncRuntime::SINGLETON
                    );
                }
            }
            _ => (),
        }
    }
}

Then you can access the runtime/singleton like other non-builtin engine singletons.

let singleton_access = AsyncRuntime::singleton(); // returns an Option<Gd<AsyncRuntime>> if you need this.
let runtime_access = AsyncRuntime::runtime(); // returns an Option<Rc<Runtime>> if you want to get to the point.

Dependencies

~7–14MB
~211K SLoC