Skip to main content

godot_async

Macro godot_async 

Source
macro_rules! godot_async {
    ($($block: tt)+) => { ... };
}
Expand description

Convert a block of Rust async code into a Godot task whose completion can safely be awaited from GDScript.

This macro returns a Variant which can be awaited on the GDScript side. If this macro is the entire function body, your function must return Variant.

It is strongly recommended that functions which use this macro take this: Gd<Self> via #[func(gd_self)] (as opposed to &self or &mut self) in order to avoid holding a Gd binding across an await point.

ยงExample Usage

// Rust

#[derive(GodotClass)]
#[class(init, base=Node)]
struct MyRustNode {}

#[godot_api]
impl MyRustNode {
  #[func(gd_self)]
  pub fn wait_three_secs(this: Gd<Self>) -> Variant {
    godot_async! {
      godot_print!("Before Timer! (2)");
      let timer = this.get_tree().create_timer(3.0);
      timer.signals().timeout().to_future().await;
      godot_print!("After Timer! (3)");
    }
  }
}
# GDScript
extends Node

func _ready():
    var node = MyRustNode.new()
    add_child(node)
    print("Before Timer! (1)")
    await node.wait_three_secs()
    print("After Timer! (4)")