Add generic Gd::duplicate_node + Gd::duplicate_resource #1492
Merged
Add generic Gd::duplicate_node + Gd::duplicate_resource #1492
Gd::duplicate_node + Gd::duplicate_resource #1492Conversation
041c28b to
327fa91
Compare
327fa91 to
9021ce5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds type-safe methods to duplicate
NodeandResource, handling all the tiny nuances across Godot versions.In contrast to existing
Node::duplicateandResource::duplicate, these methods are directly defined onGd<T>.This takes into account the generic type
Tand thus returns an object of the same type, rather than alwaysGd<Node>/Gd<Resource>. Furthermore, no moreOption<Gd<...>>is returned unless you explicitly want to handle rare fallible cases with*_ex().done_or_null().Closes #1421.
New API
Old methods are now deprecated and will be removed in godot-rust v0.6:
Node::duplicateNode::duplicate_exResource::duplicateResource::duplicate_exResource::duplicate_deepResource::duplicate_deep_exNuances
This took me much longer than I would have liked, because Godot's
Resource::duplicate*APIs are quite a mess.In fairness, PR #100673 made a lot better, but with backwards compatibility the resulting API surface is very confusing.
Resource::duplicate(subresources=false). All good so far.subresourcestodeep.Resource::duplicate_deep(mode=INTERNAL).duplicate(deep=true).DeepDuplicateMode::NONE, which one might think does not duplicate deeply. Except it does, just without subresources. Arrays/dicts are deep-copied, unlikeduplicate(deep=false)which references them.duplicate(deep=false)to reference arrays/dicts instead of shallow-copy them.I hope you never have to write a plugin that supports these versions and need duplicate.
Anyway, in case you do, I wrote a nice table that explains all the differences. And the Rust API is stable.