Function godot::tools::try_load

pub fn try_load<T>(path: impl Into<GString>) -> Result<Gd<T>, IoError>
where T: Inherits<Resource>,
Expand description

Loads a resource from the filesystem located at path.

The resource is loaded during the method call, unless it is already referenced elsewhere, e.g. in another script or in the scene. This might cause slight delay, especially when loading scenes.

This function can fail if resource can’t be loaded by ResourceLoader or if the subsequent cast into T fails.

This method is a simplified version of ResourceLoader::load(), which can be used for more advanced scenarios.

§Note

Resource paths can be obtained by right-clicking on a resource in the Godot editor (FileSystem dock) and choosing Copy Path, or by dragging the file from the FileSystem dock into the script.

The path must be absolute (typically starting with res://), a local path will fail.

§Example

Loads a scene called Main located in the path/to subdirectory of the Godot project and caches it in a variable. The resource is directly stored with type PackedScene.

use godot::prelude::*;

if let Ok(scene) = try_load::<PackedScene>("res://path/to/Main.tscn") {
    // all good
} else {
    // handle error
}