Skip to content

Initial test of Godot duplicator with Area2D #1141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 84 additions & 2 deletions itest/rust/src/object_tests/property_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

use godot::builtin::{dict, Color, Dictionary, GString, Variant, VariantType};
use godot::classes::{INode, IRefCounted, Node, Object, RefCounted, Resource, Texture};
use godot::builtin::{dict, Color, Dictionary, GString, StringName, Variant, VariantType};
use godot::classes::{Area2D, INode, IRefCounted, Node, Object, RefCounted, Resource, Texture};
use godot::global::{PropertyHint, PropertyUsageFlags};
use godot::meta::{GodotConvert, PropertyHintInfo, ToGodot};
use godot::obj::{Base, EngineBitfield, EngineEnum, Gd, NewAlloc, NewGd, OnEditor};
Expand Down Expand Up @@ -537,3 +537,85 @@ fn test_var_with_renamed_funcs() {

obj.free();
}

#[derive(GodotClass)]
#[class(base=Node)]
struct SomeDuplicator {
#[var]
int_val: i32,

#[var]
optional_node: Option<Gd<Node>>,

#[export]
export_area: OnEditor<Gd<Area2D>>,
}

#[godot_api]
impl INode for SomeDuplicator {
fn init(_base: Base<Node>) -> Self {
SomeDuplicator {
int_val: 0,
optional_node: None,
export_area: OnEditor::default(),
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this uses default values for every field, it can be generated.

This can be done on the struct declaration above, by adding init:

#[derive(GodotClass)]
#[class(init, base=Node)]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.


#[itest]
fn test_some_duplicator() {
let mut obj = SomeDuplicator::new_alloc();
assert_eq!(obj.bind().int_val, 0);
assert_eq!(obj.bind().optional_node, None);

obj.bind_mut().int_val = 5;
assert_eq!(obj.bind().int_val, 5);

let some_node = Some(Node::new_alloc());
let some_id = some_node.as_ref().unwrap().instance_id();
obj.bind_mut().optional_node = some_node.clone();
let obj_id = obj.bind().optional_node.as_ref().unwrap().instance_id();
assert_eq!(obj_id, some_id);

obj.bind_mut()
.optional_node
.as_mut()
.unwrap()
.set_name("renamed");
assert_eq!(
obj.bind().optional_node.as_ref().unwrap().get_name(),
StringName::from("renamed")
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, just tests what was set. The idea of adding this test is to check if Node::duplicate() copies the values, not if manually setting them works.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood. Addressed.


let mut some_area = Area2D::new_alloc();
some_area.set_collision_layer(1);
some_area.set_collision_mask(1);
obj.bind_mut().export_area.init(some_area);
assert_eq!(obj.bind().export_area.get_collision_layer(), 1);
assert_eq!(obj.bind().export_area.get_collision_mask(), 1);

let duplicated_obj = obj.duplicate();
let duplicated_obj: Gd<SomeDuplicator> = duplicated_obj.unwrap().cast();

assert_eq!(duplicated_obj.bind().int_val, 5);
assert_eq!(
duplicated_obj
.bind()
.optional_node
.as_ref()
.unwrap()
.instance_id(),
obj_id
);
assert_eq!(
duplicated_obj
.bind()
.optional_node
.as_ref()
.unwrap()
.get_name(),
StringName::from("renamed")
);
assert_eq!(duplicated_obj.bind().export_area.get_collision_layer(), 1);
assert_eq!(duplicated_obj.bind().export_area.get_collision_mask(), 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can extract this into a variable, and then use everywhere:

let duplicated = duplicated_obj.bind();

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}