Skip to content

Commit 648d585

Browse files
committed
Make use of type hint, use load() in examples
1 parent d512dcc commit 648d585

File tree

3 files changed

+7
-19
lines changed

3 files changed

+7
-19
lines changed

examples/native-plugin/src/lib.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Rust GDNative implementation of this Godot tutorial:
22
// https://docs.godotengine.org/en/stable/tutorials/plugins/editor/making_plugins.html#a-custom-node
33

4-
use gdnative::api::{EditorPlugin, Resource, Script, Texture};
4+
use gdnative::api::{EditorPlugin, Script, Texture};
55
use gdnative::prelude::*;
66

77
#[derive(NativeClass)]
@@ -18,10 +18,8 @@ impl CustomNode {
1818
fn _enter_tree(&self, #[base] owner: TRef<EditorPlugin>) {
1919
// Initialization of the plugin goes here.
2020
// Add the new type with a name, a parent type, a script and an icon.
21-
let script = unsafe { load::<Script>("res://my_button.gdns", "Script").unwrap() };
22-
let texture = unsafe {
23-
load::<Texture>("res://making_plugins-custom_node_icon.png", "Texture").unwrap()
24-
};
21+
let script = load::<Script>("res://my_button.gdns").unwrap();
22+
let texture = load::<Texture>("res://making_plugins-custom_node_icon.png").unwrap();
2523
owner.add_custom_type("MyButton", "Button", script, texture);
2624
}
2725

@@ -56,15 +54,6 @@ impl MyButton {
5654
}
5755
}
5856

59-
unsafe fn load<T>(path: &str, hint: &str) -> Option<Ref<T, Shared>>
60-
where
61-
T: GodotObject<Memory = RefCounted> + SubClass<Resource>,
62-
{
63-
let resource = ResourceLoader::godot_singleton().load(path, hint, false)?;
64-
let resource = resource.assume_safe().claim();
65-
resource.cast::<T>()
66-
}
67-
6857
fn init(handle: InitHandle) {
6958
handle.add_tool_class::<CustomNode>();
7059
handle.add_tool_class::<MyButton>();

examples/scene-create/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,9 @@ fn init(handle: InitHandle) {
105105
}
106106

107107
pub fn load_scene(path: &str) -> Option<Ref<PackedScene, ThreadLocal>> {
108-
let scene = ResourceLoader::godot_singleton().load(path, "PackedScene", false)?;
109-
108+
let scene = load::<PackedScene>(path)?;
110109
let scene = unsafe { scene.assume_thread_local() };
111-
112-
scene.cast::<PackedScene>()
110+
Some(scene)
113111
}
114112

115113
/// Root here is needs to be the same type (or a parent type) of the node that you put in the child

gdnative/src/globalscope.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ pub fn load<T>(path: &str) -> Option<Ref<T>>
5656
where
5757
T: SubClass<Resource> + GodotObject<Memory = RefCounted>,
5858
{
59+
let type_hint = T::class_name();
5960
ResourceLoader::godot_singleton()
60-
.load(path, "", false)
61+
.load(path, type_hint, false)
6162
.and_then(|res| res.cast::<T>())
6263
}

0 commit comments

Comments
 (0)