Skip to content

Commit 5a5157d

Browse files
committed
Update gdscript.rs
Update gdscript.rs Update gdscript.rs Update gdscript.rs added load function to globalscope Update gdscript.rs
1 parent cb2a30f commit 5a5157d

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

gdnative/src/gdscript.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use gdnative_bindings::{Resource, ResourceLoader};
2+
use gdnative_core::{
3+
core_types::NodePath,
4+
object::{memory::RefCounted, GodotObject, Ref, SubClass},
5+
};
6+
7+
/// Loads a resource from the filesystem located at `path`. The resource is loaded on the method call (unless it's referenced already elsewhere, e.g. in another script or in the scene), which might cause slight delay, especially when loading scenes.
8+
/// # Note:
9+
/// Resource paths can be obtained by right-clicking on a resource in the FileSystem dock and choosing "Copy Path" or by dragging the file from the FileSystem dock into the script.
10+
///
11+
/// Load a scene called main located in the root of the project directory and cache it in a variable. var main = load("res://main.tscn") main will contain a PackedScene resource.
12+
/// # Important:
13+
/// The path must be absolute, a local path will just return null.
14+
/// This method is a simplified version of `ResourceLoader.load()`, which can be used for more advanced scenarios.
15+
/// # Examples:
16+
/// ```no_run
17+
/// use gdnative::globalscope::load;
18+
/// use gdnative::prelude::PackedScene;
19+
///
20+
/// let scene = load::<PackedScene>("res://path").unwrap();
21+
/// ```
22+
#[inline]
23+
pub fn load<T>(path: impl Into<NodePath>) -> Option<Ref<T>>
24+
where
25+
T: SubClass<Resource> + GodotObject<Memory = RefCounted>,
26+
{
27+
ResourceLoader::godot_singleton()
28+
.load(path.into(), "", false)
29+
.unwrap()
30+
.cast::<T>()
31+
}

gdnative/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,14 @@
8181
// Items, which are #[doc(hidden)] in their original crate and re-exported with a wildcard, lose
8282
// their hidden status. Re-exporting them manually and hiding the wildcard solves this.
8383
#[doc(inline)]
84-
pub use gdnative_core::{core_types, export, globalscope, init, log, object, profiler};
84+
pub use gdnative_core::{core_types, export, init, log, object, profiler};
85+
86+
mod gdscript; // new methods from `@GDscript`, so far only `load`
87+
pub mod globalscope {
88+
pub use crate::gdscript::*;
89+
#[doc(inline)]
90+
pub use gdnative_core::globalscope::*;
91+
}
8592

8693
// Implementation details (e.g. used by macros).
8794
// However, do not re-export macros (on crate level), thus no wildcard

0 commit comments

Comments
 (0)