A simple wrapper for awaiting Godot built-in signals in an async context.
It only depends on godot
by default.
Add this to your Cargo.toml
:
[dependencies]
godot_await = { git = "https://github.com/ColinWttt/godot_await"}
Using godot_await
:
wait(1.0).await;
Equivalent to:
# GDScript
await get_tree().create_timer(1.0).timeout
// Rust (without godot_await)
let timer = Engine::singleton()
.get_main_loop().unwrap()
.cast::<SceneTree>().unwrap()
.create_timer(1.0).unwrap();
timer.signals().timeout().to_future().await;
Using godot_await
:
tween.finished().await;
Equivalent to
# GDScript
await tween.finished
// Rust (without godot_await)
tween.signals().finished().to_future().await;
Function names with the _fallible
suffix return FallibleSignalFuture<(...)>
.
FallibleSignalFuture
:The future might resolve to an error if the signal object is freed before the signal is emitted.gdext repo
task::spawn(async move {
let result = button.pressed_fallible().await;
assert!(result.is_err());
});
button.call_deferred("free", &[]);
godot_await
has no features enabled by default.
Optionally, the following dependencies can be enabled:
future
enableszip
,or
,try_zip
,wait_or
, using the pin-project-lite crate.
// Joins two futures, waiting for both to complete.
zip(tween.finished(),timer.timeout()).await;
// Returns the result of the future that completes first.
or(button.pressed(),wait(1.0)).await;
// Equivalent to
button.pressed().or(wait(1.0)).await;
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.