| Crates.io | godot_tokio |
| lib.rs | godot_tokio |
| version | 0.3.1 |
| created_at | 2024-11-30 17:40:26.138327+00 |
| updated_at | 2025-06-29 16:56:36.199062+00 |
| description | The tokio-async runtime wrapped in a gdextention object to be used as an engine singleton in your gdext project. |
| homepage | |
| repository | https://github.com/2-3-5-41/godot_tokio |
| max_upload_size | |
| id | 1466836 |
| size | 18,623 |
This was made to prevent re-typing out the boilerplate for creating a tokio runtime godot object.
Things that changed!
runtime() can now panic, but only if the Godot engine, is unable to register the AsyncRuntime singleton.
on_level_init instead of having AsyncRuntime::runtime() do it for you.To start, let's get godot_tokio in your crate, there are two options:
OR
Cargo.toml -> godot_tokio = { git = "https://github.com/2-3-5-41/godot_tokio" }.Next, you'll want to add the AsyncRuntime object to your engine singletons like so:
struct MyExtension;
#[gdextension]
unsafe impl ExtensionLibrary for MyExtension {
fn on_level_init(level: InitLevel) {
match level {
InitLevel::Scene => {
let mut engine = Engine::singleton();
// This is where we register our async runtime singleton.
engine.register_singleton(AsyncRuntime::SINGLETON, &AsyncRuntime::new_alloc());
}
_ => (),
}
}
fn on_level_deinit(level: InitLevel) {
match level {
InitLevel::Scene => {
let mut engine = Engine::singleton();
// Here is where we free our async runtime singleton from memory.
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
);
}
}
_ => (),
}
}
}
All that's left is to use godot_tokio, here is a basic example:
...
impl INode for MyAsyncNode {
...
// You can use the tokio runtime in your base functions (i.e `init`, `enter_tree`, `ready`, etc...)
fn ready(&mut self) {
AsyncRuntime::spawn(async { todo!("Hello, world!\n From the tokio async runtime!") });
}
}
impl MyAsyncNode {
// You can also use the tokio runtime in gdscript accessible functions like so.
#[func]
fn async_hello_world() {
AsyncRuntime::spawn(async { todo!("Hello again, world!\n From the tokio async runtime!") });
}
// Even normal internal rust functions.
fn another_async_hello_world() {
AsyncRuntime::spawn(async { todo!("You get the point, I hope.") })
}
}
...
Note: Additional tokio features are not exposed through this crate, you'll want to add tokio >= 1.32.0 to your crate in order to enable additional tokio features.