try_get_autoload_by_name

Function try_get_autoload_by_name 

pub fn try_get_autoload_by_name<T>(
    autoload_name: &str,
) -> Result<Gd<T>, ConvertError>
where T: Inherits<Node>,
Expand description

Retrieves an autoload by name (fallible).

Autoloads are accessed via the /root/{name} path in the scene tree. The name is the one you used to register the autoload in project.godot. By convention, it often corresponds to the class name, but does not have to.

If the autoload can be resolved, it will be cached and returned very quickly the second time.

See also get_autoload_by_name() for simpler function expecting the class name and non-fallible invocation.

This function returns Err if:

  • No autoload is registered under name.
  • The autoload cannot be cast to type T.
  • There is an error fetching the scene tree.

ยงExample

use godot::prelude::*;
use godot::tools::try_get_autoload_by_name;

#[derive(GodotClass)]
#[class(init, base=Node)]
struct GlobalStats {
    base: Base<Node>,
}

let result = try_get_autoload_by_name::<GlobalStats>("Statistics");
match result {
    Ok(autoload) => { /* Use the Gd<GlobalStats>. */ }
    Err(err) => eprintln!("Failed to get autoload: {err}"),
}