Skip to content

Commit 3a064fb

Browse files
committed
Tidyup patch as provided by @karroffel
1 parent 9903b2c commit 3a064fb

File tree

5 files changed

+58
-87
lines changed

5 files changed

+58
-87
lines changed

examples/scene_create/Child.gdns

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

examples/scene_create/Child_scene.tscn

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
[gd_scene load_steps=3 format=2]
2-
3-
[ext_resource path="res://Child.gdns" type="Script" id=1]
1+
[gd_scene load_steps=2 format=2]
42

53
[sub_resource type="CubeMesh" id=1]
64

75
[node name="Child" type="Spatial"]
8-
script = ExtResource( 1 )
96

107
[node name="CSGMesh" type="CSGMesh" parent="."]
118
transform = Transform( 0.9, 0, 0, 0, 0.9, 0, 0, 0, 0.9, 0, 0, 0 )

examples/scene_create/Main.tscn

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
[gd_scene load_steps=4 format=2]
1+
[gd_scene load_steps=3 format=2]
22

33
[ext_resource path="res://Parent.gdns" type="Script" id=1]
44
[ext_resource path="res://Panel.gd" type="Script" id=2]
5-
[ext_resource path="res://Child_scene.tscn" type="PackedScene" id=3]
65

76
[node name="Main" type="Spatial"]
87

@@ -45,21 +44,5 @@ margin_top = 21.0
4544
margin_right = 580.0
4645
margin_bottom = 40.0
4746
text = "No Children have been created yet"
48-
49-
[node name="Child" parent="." instance=ExtResource( 3 )]
50-
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -5, 0, -5 )
51-
visible = false
52-
53-
[node name="Child2" parent="." instance=ExtResource( 3 )]
54-
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 5, 0, 5 )
55-
visible = false
56-
57-
[node name="Child3" parent="." instance=ExtResource( 3 )]
58-
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -5.21943, 0, 5 )
59-
visible = false
60-
61-
[node name="Child4" parent="." instance=ExtResource( 3 )]
62-
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 4.30119, 0, -4.46641 )
63-
visible = false
6447
[connection signal="pressed" from="Ui/Canvas/Panel/Add" to="Ui/Canvas/Panel" method="_on_Add_pressed"]
6548
[connection signal="pressed" from="Ui/Canvas/Panel/Remove" to="Ui/Canvas/Panel" method="_on_Remove_pressed"]

examples/scene_create/Panel.gd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ func _ready():
44
pass # Replace with function body.
55

66
# There is a signal set up on the Add button which calls
7-
# this method. We pass the call into Parent.spawn_1
7+
# this method. We pass the call into Parent.spawn_one
88
# (This calls Rust)
99
func _on_Add_pressed():
1010
var spawner_node = get_node("/root/Main/Parent")
11-
spawner_node.spawn_1("example string")
11+
spawner_node.spawn_one("example string")
1212

1313
# There is a signal set up on the Remove button which calls
14-
# this method. We pass the call into Parent.remove_1
14+
# this method. We pass the call into Parent.remove_one
1515
# (This calls Rust)
1616
func _on_Remove_pressed():
1717
var spawner_node = get_node("/root/Main/Parent")
18-
spawner_node.remove_1("Another example string")
18+
spawner_node.remove_one("Another example string")
1919

2020
# This function is called from Rust. All we need there is this
2121
# node and the name "set_num_children"

examples/scene_create/src/lib.rs

Lines changed: 52 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#[macro_use]
2-
extern crate gdnative as godot;
2+
extern crate gdnative;
33
extern crate euclid;
44

5-
use godot::init::{Property, PropertyHint, PropertyUsage};
6-
use godot::{GodotString, Variant, ResourceLoader, PackedScene, Spatial, Int32Array};
5+
use gdnative::{GodotString, Variant, ResourceLoader, PackedScene, Spatial};
76
use euclid::vec3;
87

98
#[derive(Debug, Clone, PartialEq)]
@@ -15,36 +14,32 @@ pub enum ManageErrs {
1514
#[derive(gdnative::NativeClass)]
1615
#[inherit(gdnative::Spatial)]
1716
struct SceneCreate{
18-
/// Store the loaded scene for a very slight performance boost but mostly to show you how.
17+
// Store the loaded scene for a very slight performance boost but mostly to show you how.
1918
template : Option<PackedScene>,
20-
children_spawned : i32,
19+
children_spawned : u32,
2120
}
2221

2322

24-
/// Demonstrates Scene creation, calling to/from gdscript
25-
///
26-
/// 1. Child scene is created when spawn_1 is called
27-
/// 2. Child scenes are deleted when remove_1 is called
28-
/// 3. Find and call functions in a node (Panel)
29-
/// 4. Call functions in GDNative (from panel into spawn/remove)
30-
///
31-
/// Note, the same mechanism which is used to call from panel into spawn_1 and remove_1 can be
32-
/// used to call other GDNative classes here in rust.
23+
// Demonstrates Scene creation, calling to/from gdscript
24+
//
25+
// 1. Child scene is created when spawn_one is called
26+
// 2. Child scenes are deleted when remove_one is called
27+
// 3. Find and call functions in a node (Panel)
28+
// 4. Call functions in GDNative (from panel into spawn/remove)
29+
//
30+
// Note, the same mechanism which is used to call from panel into spawn_one and remove_one can be
31+
// used to call other GDNative classes here in rust.
3332

3433
#[gdnative::methods]
3534
impl SceneCreate {
3635

3736
fn _init(_owner: gdnative::Spatial) -> Self {
3837
SceneCreate {
3938
template: None, // Have not loaded this template yet.
40-
children_spawned: -1,
39+
children_spawned: 0,
4140
}
4241
}
4342

44-
fn class_name() -> &'static str {
45-
"SceneCreate"
46-
}
47-
4843
#[export]
4944
fn _ready(&mut self, _owner: gdnative::Spatial) {
5045
self.template = load_scene("res://Child_scene.tscn");
@@ -55,9 +50,9 @@ impl SceneCreate {
5550
}
5651

5752
#[export]
58-
fn spawn_1(&mut self, mut owner: gdnative::Spatial, str : GodotString) {
53+
unsafe fn spawn_one(&mut self, mut owner: gdnative::Spatial, message: GodotString) {
5954

60-
godot_print!("Called spawn_1({})", str.to_string());
55+
godot_print!("Called spawn_one({})", message.to_string());
6156

6257
let template = if let Some(template) = &self.template {
6358
template
@@ -71,90 +66,91 @@ impl SceneCreate {
7166
match instance_scene::<Spatial>(template) {
7267
Ok(mut spatial) => {
7368
// Here is how you rename the child...
74-
self.children_spawned += 1;
7569
let key_str = format!("child_{}", self.children_spawned);
76-
unsafe{
77-
spatial.set_name(GodotString::from_str(&key_str));
70+
spatial.set_name(GodotString::from_str(&key_str));
7871

79-
let x = (self.children_spawned % 10) as f32;
80-
let z = (self.children_spawned / 10) as f32;
81-
spatial.translate(vec3(-10.0 + x * 2.0, 0.0, -10.0 + z * 2.0));
8272

73+
let x = (self.children_spawned % 10) as f32;
74+
let z = (self.children_spawned / 10) as f32;
75+
spatial.translate(vec3(-10.0 + x * 2.0, 0.0, -10.0 + z * 2.0));
8376

84-
// You need to parent the new scene under some node if you want it in the scene.
85-
// We parent it under ourselves.
86-
owner.add_child(Some(spatial.to_object()), false);
87-
};
8877

78+
// You need to parent the new scene under some node if you want it in the scene.
79+
// We parent it under ourselves.
80+
owner.add_child(Some(spatial.to_object()), false);
81+
self.children_spawned += 1;
8982

9083
},
9184
Err(err) => godot_print!("Could not instance Child : {:?}", err),
9285
}
9386

94-
let num_children = unsafe {owner.get_child_count()};
87+
let num_children = owner.get_child_count();
9588
update_panel(&mut owner, num_children);
9689
}
9790

9891
#[export]
99-
fn remove_1(&mut self, mut owner: gdnative::Spatial, str : GodotString) {
100-
godot_print!("Called remove_1({})", str.to_string());
101-
let num_children = unsafe {owner.get_child_count()};
92+
unsafe fn remove_one(&mut self, mut owner: gdnative::Spatial, str : GodotString) {
93+
godot_print!("Called remove_one({})", str.to_string());
94+
let num_children = owner.get_child_count();
10295
if num_children <= 0 {
10396
godot_print!("No children to delete");
10497
return;
10598
}
10699

107-
let mut last_child = unsafe{owner.get_child(num_children - 1)};
100+
assert_eq!(self.children_spawned as i64, num_children);
101+
102+
let last_child = owner.get_child(num_children - 1);
108103
if let Some(mut node) = last_child {
109-
unsafe{node.queue_free()}
104+
node.queue_free();
105+
self.children_spawned -= 1;
106+
110107
}
111108

112109
update_panel(&mut owner, num_children - 1);
113110
}
114111

115112
}
116113

117-
fn init(handle: godot::init::InitHandle) {
114+
fn init(handle: gdnative::init::InitHandle) {
118115
handle.add_class::<SceneCreate>();
119116
}
120117

121-
pub fn load_scene(name: &str) -> Option<PackedScene> {
118+
pub fn load_scene(path: &str) -> Option<PackedScene> {
122119
let scene = ResourceLoader::godot_singleton().load(
123-
GodotString::from_str(name),
124-
GodotString::from_str("PackedScene"), false);
125-
120+
GodotString::from_str(path), // could also use path.into() here
121+
GodotString::from_str("PackedScene"),
122+
false,
123+
);
126124

127125
scene.and_then(|s| s.cast::<PackedScene>())
128126
}
129127

130-
// Root here is needs to be the same type (or a parent type) of the node that you put in the child
131-
// scene as the root. For instance Spatial is used for this example.
132-
pub fn instance_scene<Root>(scene : &PackedScene) -> Result<Root, ManageErrs>
133-
where Root: godot::GodotObject {
134-
let mut inst_option = scene.instance(0); // 0 - GEN_EDIT_STATE_DISABLED
128+
/// Root here is needs to be the same type (or a parent type) of the node that you put in the child
129+
/// scene as the root. For instance Spatial is used for this example.
130+
unsafe fn instance_scene<Root>(scene : &PackedScene) -> Result<Root, ManageErrs>
131+
where Root: gdnative::GodotObject {
132+
let inst_option = scene.instance(0); // 0 - GEN_EDIT_STATE_DISABLED
135133

136-
if let Some(mut instance) = inst_option {
137-
if let Some(mut instance_root) = unsafe { instance.cast::<Root>() } {
134+
if let Some(instance) = inst_option {
135+
if let Some(instance_root) = instance.cast::<Root>() {
138136
Ok(instance_root)
139137
}
140138
else {
141-
Err(ManageErrs::RootClassNotSpatial(unsafe{instance.get_name()}.to_string()))
139+
Err(ManageErrs::RootClassNotSpatial(instance.get_name().to_string()))
142140

143141
}
144142
} else {
145143
Err(ManageErrs::CouldNotMakeInstance)
146144
}
147145
}
148146

149-
fn update_panel(owner: &mut gdnative::Spatial, num_children : i64) {
147+
unsafe fn update_panel(owner: &mut gdnative::Spatial, num_children : i64) {
150148
// Here is how we call into the panel. First we get its node (we might have saved it
151149
// from earlier)
152-
let panel_node_opt = unsafe {
153-
owner.get_parent().and_then(|parent|
150+
let panel_node_opt = owner.get_parent().and_then(|parent|
154151
parent.find_node(GodotString::from_str("Panel"), true, false)
155-
)};
156-
if let Some(panel_node) = panel_node_opt
157-
{
152+
);
153+
if let Some(panel_node) = panel_node_opt {
158154
// Put the Node
159155
let mut as_variant = Variant::from_object(&panel_node);
160156
match as_variant.call(&GodotString::from_str("set_num_children"),

0 commit comments

Comments
 (0)