|
| 1 | +//! Port of selected GDScript built-in functions. |
| 2 | +//! |
| 3 | +//! This module contains _some_ of the functions available in the [@GDScript] documentation. |
| 4 | +//! |
| 5 | +//! Reasons why a GDScript function may _not_ be ported to Rust include: |
| 6 | +//! * they are in the Rust standard library (`abs`, `sin`, `floor`, `assert`, ...) |
| 7 | +//! * they are already part of a godot-rust API |
| 8 | +//! * `print` -> [`godot_print!`][crate::log::godot_print!] |
| 9 | +//! * `instance_from_id` -> [`GodotObject::from_instance_id()`][crate::object::GodotObject::from_instance_id] |
| 10 | +//! * ... |
| 11 | +//! * they have a private implementation, i.e. a Rust port would have different semantics |
| 12 | +//! * `randi`, `randf` etc. -- users should use `rand` crate |
| 13 | +//! * `str2var`, `bytes2var`, `hash` etc -- to be verified |
| 14 | +//! |
| 15 | +//! This above list is not a definitive inclusion/exclusion criterion, just a rough guideline. |
| 16 | +//! |
| 17 | +//! Other noteworthy special cases: |
| 18 | +//! * GDScript `fmod` corresponds to Rust's `%` operator on `f32` (also known as the `Rem` trait). |
| 19 | +//! |
| 20 | +//! [@GDScript]: https://docs.godotengine.org/en/stable/classes/[email protected] |
| 21 | +
|
| 22 | +use crate::api::{Resource, ResourceLoader}; |
| 23 | +use crate::object::{memory::RefCounted, GodotObject, Ref, SubClass}; |
| 24 | + |
| 25 | +#[doc(inline)] |
| 26 | +pub use gdnative_core::globalscope::*; |
| 27 | + |
| 28 | +/// Loads a resource from the filesystem located at `path`. |
| 29 | +/// |
| 30 | +/// The resource is loaded on the method call (unless it's referenced already elsewhere, e.g. in another script or in the scene), |
| 31 | +/// which might cause slight delay, especially when loading scenes. |
| 32 | +/// |
| 33 | +/// If the resource cannot be loaded, or is not of type `T` or inherited, this method returns `None`. |
| 34 | +/// |
| 35 | +/// This method is a simplified version of [`ResourceLoader::load()`][crate::api::ResourceLoader::load], |
| 36 | +/// which can be used for more advanced scenarios. |
| 37 | +/// |
| 38 | +/// # Note: |
| 39 | +/// Resource paths can be obtained by right-clicking on a resource in the Godot editor (_FileSystem_ dock) and choosing "Copy Path", |
| 40 | +/// or by dragging the file from the _FileSystem_ dock into the script. |
| 41 | +/// |
| 42 | +/// The path must be absolute (typically starting with `res://`), a local path will fail. |
| 43 | +/// |
| 44 | +/// # Example: |
| 45 | +/// Loads a scene called `Main` located in the `path/to` subdirectory of the Godot project and caches it in a variable. |
| 46 | +/// The resource is directly stored with type `PackedScene`. |
| 47 | +/// |
| 48 | +/// ```no_run |
| 49 | +/// use gdnative::prelude::*; |
| 50 | +/// |
| 51 | +/// let scene = load::<PackedScene>("res://path/to/Main.tscn").unwrap(); |
| 52 | +/// ``` |
| 53 | +// TODO generalize parameter to `impl Into<NodePath>` once MSRV >= 1.63 |
| 54 | +#[inline] |
| 55 | +pub fn load<T>(path: &str) -> Option<Ref<T>> |
| 56 | +where |
| 57 | + T: SubClass<Resource> + GodotObject<Memory = RefCounted>, |
| 58 | +{ |
| 59 | + ResourceLoader::godot_singleton() |
| 60 | + .load(path, "", false) |
| 61 | + .and_then(|res| res.cast::<T>()) |
| 62 | +} |
0 commit comments