Skip to main content

try_save

Function try_save 

pub fn try_save<T>(
    obj: &Gd<T>,
    path: impl AsArg<GString>,
) -> Result<(), IoError>
where T: Inherits<Resource>,
Expand description

Saves a Resource-inheriting object into the file located at path.

This function can fail if ResourceSaver can’t save the resource to file, as it is a simplified version of ResourceSaver::save(). The underlying method can be used for more advances scenarios.

On failure, Godot error printing is temporarily suppressed, so no console error appears.

§Note

Target path must be presented in Godot-recognized format, mainly the ones beginning with res:// and user://. Saving to res:// is possible only when working with unexported project - after its export only user:// is viable.

§Example

use godot::prelude::*;

#[derive(GodotClass)]
#[class(base=Resource, init)]
struct SavedGame {
  // Exported properties are saved in `.tres` files.
  #[export]
  level: u32
};

let save_state = SavedGame::new_gd();
let res = try_save(&save_state, "user://save.tres");

assert!(res.is_ok());