Function spawn

pub fn spawn(future: impl Future<Output = ()> + 'static) -> TaskHandle
Expand description

Create a new async background task.

This function allows creating a new async task in which Godot signals can be awaited, like it is possible in GDScript. The TaskHandle that is returned provides synchronous introspection into the current state of the task.

Refer to Signal::to_future and Signal::to_fallible_future for details on how to await a signal.

§Panics

  • If called from any other thread than the main-thread.

§Example

let node = Node::new_alloc();
let signal = Signal::from_object_signal(&node, "signal");

godot::task::spawn(async move {
    println!("starting task...");

    signal.to_future::<()>().await;

    println!("node has changed: {}", node.get_name());
});