Skip to content

ColinWttt/godot_await

Repository files navigation

godot_await

A simple wrapper for awaiting Godot built-in signals in an async context.

It only depends on godot by default.

Usage

Add this to your Cargo.toml:

[dependencies]
godot_await =  { git = "https://github.com/ColinWttt/godot_await"}

Examples

Wait for a Duration

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;

Wait for tween finished

Using godot_await:

  tween.finished().await;

Equivalent to

  # GDScript
  await tween.finished
  // Rust (without godot_await)
  tween.signals().finished().to_future().await;

The _fallible suffix

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", &[]);

Crate Features

godot_await has no features enabled by default.

Optionally, the following dependencies can be enabled:

    // 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;

License

Licensed under either of

at your option.

About

A simple wrapper for awaiting Godot built-in signals

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published