Skip to content

Clarify Node::duplicate() semantics on #[var] and #[export] fields #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

Merged
merged 2 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions itest/rust/src/object_tests/property_template_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ fn property_template_test(ctx: &TestContext) {
"mismatch in property {name}:\n GDScript: {gdscript_prop:?}\n Rust: {rust_prop:?}"
));
}
/*else { // Keep around for debugging.
println!(
"good property {name}:\n GDScript: {gdscript_prop:?}\n Rust: {rust_prop:?}"
);
}*/
}

rust_properties.free();
Expand Down
58 changes: 58 additions & 0 deletions itest/rust/src/object_tests/property_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,61 @@ fn test_var_with_renamed_funcs() {

obj.free();
}

// ----------------------------------------------------------------------------------------------------------------------------------------------

#[derive(GodotClass)]
#[class(init, base=Node)]
struct Duplicator {
// #[export] would also make tests pass, but #[export(storage)] additionally hides the properties from the editor.
// See https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_exports.html#export-storage.
#[export(storage)]
int_export: i32,

// Low-level #[var] should also work.
#[var(usage_flags = [STORAGE])]
int_var: i32,

// Not copied because not marked as serialize ("storage").
#[var]
int_ignored: i32,

#[export(storage)]
optional_node: Option<Gd<Node>>,

#[export(storage)]
oneditor_node: OnEditor<Gd<Node>>,
}

#[itest]
fn test_duplicate_retains_properties() {
let optional_node = Node::new_alloc();
let oneditor_node = Node::new_alloc();

// Set up original node.
let mut original = Duplicator::new_alloc();
{
let mut original = original.bind_mut();
original.int_export = 5;
original.int_var = 7;
original.int_ignored = 9; // Will not be copied.
original.optional_node = Some(optional_node.clone());
original.oneditor_node.init(oneditor_node.clone());
}

// Create duplicate and verify all properties are copied correctly.
let duplicated: Gd<Duplicator> = original.duplicate().unwrap().cast();
{
let duplicated = duplicated.bind();
assert_eq!(duplicated.int_export, 5);
assert_eq!(duplicated.int_var, 7);
assert_eq!(duplicated.int_ignored, 0); // Not copied.
assert_eq!(duplicated.optional_node.as_ref().unwrap(), &optional_node);
assert_eq!(&*duplicated.oneditor_node, &oneditor_node);
}

optional_node.free();
oneditor_node.free();
duplicated.free();
original.free();
}
Loading