Skip to content

Commit d3511ed

Browse files
committed
Simplify module setup around gdnative::globalscope + docs
Changes: * Add load() to prelude * Rename internal module gdscript -> globalscope * Module documentation for globalscope now visible again (moved from gdnative_core::globalscope) * Smaller doc tweaks
1 parent 6f4279d commit d3511ed

File tree

6 files changed

+68
-59
lines changed

6 files changed

+68
-59
lines changed

gdnative-bindings/src/utils.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use gdnative_core::object::{SubClass, TInstance, TRef};
77
use super::generated::{Engine, Node, SceneTree};
88

99
/// Convenience method to obtain a reference to an "auto-load" node, that is a child of the root
10-
/// node. Returns `None` if the node does not exist or is not of the correct type.
10+
/// node.
11+
///
12+
/// Returns `None` if the node does not exist or is not of the correct type.
1113
///
1214
/// # Safety
1315
///

gdnative-core/src/globalscope.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,3 @@
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-
221
use std::f32::consts::TAU;
232
use std::ops::Rem;
243
use std::ops::{Range, RangeInclusive};

gdnative/src/gdscript.rs

Lines changed: 0 additions & 31 deletions
This file was deleted.

gdnative/src/globalscope.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

gdnative/src/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,7 @@
8383
#[doc(inline)]
8484
pub use gdnative_core::{core_types, export, init, log, object, profiler};
8585

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-
}
86+
pub mod globalscope;
9287

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

gdnative/src/prelude.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pub mod user_data {
3434
Aether, ArcData, LocalCellData, MutexData, RwLockData,
3535
};
3636
}
37+
#[doc(inline)]
38+
pub use crate::globalscope::load;
3739

3840
// Deprecated symbols. Keep them only in prelude, as all the other paths have changed anyway.
3941
// This way, old symbol names are still discoverable and users who used prelude won't have (as many) breaking changes.

0 commit comments

Comments
 (0)