|
| 1 | +/* |
| 2 | + * Copyright (c) godot-rust; Bromeon and contributors. |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | + |
| 8 | +use crate::builtin::NodePath; |
| 9 | +use crate::classes::{Engine, Node, SceneTree}; |
| 10 | +use crate::meta::error::ConvertError; |
| 11 | +use crate::obj::{Gd, Inherits, Singleton}; |
| 12 | + |
| 13 | +/// Retrieves an autoload by its type, using the class name as the autoload name. |
| 14 | +/// |
| 15 | +/// See [Godot docs] for an explanation of the autoload concept. Godot sometimes uses the term "autoload" interchangeably with "singleton"; |
| 16 | +/// we strictly refer to the former to separate from [`Singleton`][crate::obj::Singleton] objects. |
| 17 | +/// |
| 18 | +/// [Godot docs]: https://docs.godotengine.org/en/stable/tutorials/scripting/singletons_autoload.html |
| 19 | +/// |
| 20 | +/// This is a convenience function that calls [`try_get_autoload()`] with the class name |
| 21 | +/// derived from [`T::class_id()`][crate::obj::GodotClass::class_id]. |
| 22 | +/// |
| 23 | +/// # Panics |
| 24 | +/// If the autoload is not found or cannot be cast to type `T`. |
| 25 | +/// |
| 26 | +/// # Example |
| 27 | +/// ```no_run |
| 28 | +/// use godot::prelude::*; |
| 29 | +/// |
| 30 | +/// #[derive(GodotClass)] |
| 31 | +/// #[class(init, base=Node)] |
| 32 | +/// struct GlobalStats { |
| 33 | +/// base: Base<Node>, |
| 34 | +/// } |
| 35 | +/// |
| 36 | +/// // Assuming "Statistics" is registered as an autoload in `project.godot`, |
| 37 | +/// // this returns the one instance of type Gd<GlobalStats>. |
| 38 | +/// let stats = get_autoload::<GlobalStats>("Statistics"); |
| 39 | +/// ``` |
| 40 | +pub fn get_autoload<T>(name: &str) -> Gd<T> |
| 41 | +where |
| 42 | + T: Inherits<Node>, |
| 43 | +{ |
| 44 | + try_get_autoload::<T>(&name) |
| 45 | + .unwrap_or_else(|err| panic!("Failed to get autoload `{name}`: {err}")) |
| 46 | +} |
| 47 | + |
| 48 | +/// Retrieves an autoload by name (fallible). |
| 49 | +/// |
| 50 | +/// Autoloads are accessed via the `/root/{name}` path in the scene tree. The name is the one you used to register the autoload in |
| 51 | +/// `project.godot`. By convention, it often corresponds to the class name, but does not have to. |
| 52 | +/// |
| 53 | +/// See also [`get_autoload()`] for simpler function expecting the class name and non-fallible invocation. |
| 54 | +/// |
| 55 | +/// This function returns `Err` if: |
| 56 | +/// - No autoload is registered under `name`. |
| 57 | +/// - The autoload cannot be cast to type `T`. |
| 58 | +/// - There is an error fetching the scene tree. |
| 59 | +/// |
| 60 | +/// # Example |
| 61 | +/// ```no_run |
| 62 | +/// use godot::prelude::*; |
| 63 | +/// use godot::tools::try_get_autoload; |
| 64 | +/// |
| 65 | +/// #[derive(GodotClass)] |
| 66 | +/// #[class(init, base=Node)] |
| 67 | +/// struct GlobalStats { |
| 68 | +/// base: Base<Node>, |
| 69 | +/// } |
| 70 | +/// |
| 71 | +/// let result = try_get_autoload::<GlobalStats>("Statistics"); |
| 72 | +/// match result { |
| 73 | +/// Ok(autoload) => { /* Use the Gd<GlobalStats>. */ } |
| 74 | +/// Err(err) => eprintln!("Failed to get autoload: {err}"), |
| 75 | +/// } |
| 76 | +/// ``` |
| 77 | +pub fn try_get_autoload<T>(name: &str) -> Result<Gd<T>, ConvertError> |
| 78 | +where |
| 79 | + T: Inherits<Node>, |
| 80 | +{ |
| 81 | + let main_loop = Engine::singleton() |
| 82 | + .get_main_loop() |
| 83 | + .ok_or_else(|| ConvertError::new("main loop not available"))?; |
| 84 | + |
| 85 | + let scene_tree = main_loop |
| 86 | + .try_cast::<SceneTree>() |
| 87 | + .map_err(|_| ConvertError::new("main loop is not a SceneTree"))?; |
| 88 | + |
| 89 | + let autoload_path = NodePath::from(&format!("/root/{name}")); |
| 90 | + |
| 91 | + let root = scene_tree |
| 92 | + .get_root() |
| 93 | + .ok_or_else(|| ConvertError::new("scene tree root not available"))?; |
| 94 | + |
| 95 | + root.try_get_node_as::<T>(&autoload_path).ok_or_else(|| { |
| 96 | + let class = T::class_id(); |
| 97 | + ConvertError::new(format!( |
| 98 | + "autoload `{name}` not found or has wrong type (expected {class})", |
| 99 | + )) |
| 100 | + }) |
| 101 | +} |
0 commit comments