Skip to content
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

Fix reparent() losing owner #81506

Merged
merged 1 commit into from
Jan 11, 2024

Conversation

twobitadder
Copy link
Contributor

Fixes #81480, but I am open to suggestions on any changes that need to be made - I just took the most obvious path suggested in the issue for a fix.

Thanks!

@twobitadder twobitadder requested a review from a team as a code owner September 10, 2023 04:18
@twobitadder twobitadder changed the title fix reparenting losing owner fix reparent() losing owner Sep 10, 2023
@AThousandShips AThousandShips changed the title fix reparent() losing owner Fix reparent() losing owner Sep 10, 2023
@AThousandShips AThousandShips added this to the 4.2 milestone Sep 10, 2023
@RandomShaper
Copy link
Member

RandomShaper commented Sep 12, 2023

I think this makes sense, but let me bring a couple of points up:

  • Setting the owner requires additional steps, like checking the new owner is valid (an ancestor). Let me suggest moving the code of Node::set_owner() to a private function _set_owner() that also takes a bool p_show_warning. The Node::set_owner() would just call through it with true. reparent() would instead call it with false to try to set the original owner, but not printing an error in case that one is no longer valid. Additionally, in cases like this we likely don't want owner_changed_notify() to be called (see _set_owner_nocheck()) when the original owner is successfully restored. Maybe this warrants a more comprehensive rewrite so all this is taken into account at a more elemental level instead of being that "patchy."
  • Documentation would need to be updated (haven't checked, but it likely says that reparent() is exactly like removing plus re-adding somewhere else; the owner-preserving behavior should be mentioned).

@twobitadder
Copy link
Contributor Author

twobitadder commented Sep 12, 2023

this doesn't feel much less patchy, but maybe we can include a bool p_pending_reparent in the Node class that will skip clearing owner on Node::_propagate_after_exit_tree() if true? that way there doesn't have to be a private Node::_set_owner() implementation. just set it to true before reparenting and then false immediately after

in the meantime i'll get the docs checked and updated properly

edit: on thinking about this it would also need to propagate this property to children before reparenting. i see what you mean about the changes on an elemental level. this feels like a pretty big thread to be pulling on.

@YuriSizov YuriSizov requested a review from KoBeWi September 15, 2023 16:58
@YuriSizov
Copy link
Contributor

this doesn't feel much less patchy, but maybe we can include a bool p_pending_reparent in the Node class that will skip clearing owner on Node::_propagate_after_exit_tree() if true? that way there doesn't have to be a private Node::_set_owner() implementation. just set it to true before reparenting and then false immediately after

That would be very hacky and prone to issues, like leaving pointers to the old owner when the nodes are no longer in a relationship. What Pedro suggests is much more robust and in line with how we often do stuff. And also much simpler.

@twobitadder
Copy link
Contributor Author

i guess the issue i'm seeing (and realizing my original solution is definitely inadequate for) is that children of the reparented node will lose their owner, which would mean that if the unique node is a child of the reparented node, the reference is lost. i can use the private _set_owner() call to set the owner of the reparented node, but any children will find their owner lost.

i also may have misunderstood the original intention here! i was under the impression that you would want to keep the owner during reparent, even if the new parent doesn't share a branch with the original parent.

@YuriSizov
Copy link
Contributor

i was under the impression that you would want to keep the owner during reparent, even if the new parent doesn't share a branch with the original parent.

Owner must be an ancestor of the node. If after reparenting this is no longer the case, then losing the owner is the correct behavior.

@twobitadder
Copy link
Contributor Author

my mistake! so i think the assessment of this is the nodes being reparented need to be checked for who their owner is and if they're being reparented to a descendant of that owner. then the owner can be reset on those nodes (and not any nodes that did not originally have that owner).

i apologize, i'm sure it's obvious that i'm also familiarizing myself with the idiomatic way of doing things as i go and want to make sure this is done correctly, but it definitely ended up being a bit more involved than i anticipated 😅

@RandomShaper
Copy link
Member

No need to apologize! You're trying, learning and addressing feedback. You can't be asked for more.

@KoBeWi
Copy link
Member

KoBeWi commented Sep 19, 2023

btw turns out the label "good first issue" was wrongly assigned on this one. It's more complex than I anticipated, sorry 🙃

@twobitadder
Copy link
Contributor Author

all good! i appreciate the patience everybody. once i'm back in town middle of next week i'll sit down and get this worked out.

@twobitadder twobitadder requested a review from a team as a code owner October 3, 2023 19:00
@twobitadder
Copy link
Contributor Author

here's a first pass at a way to resolve all of the needs of this fix. it's, admittedly, an extremely naive take - my c++ experience is about ten years out of use at this point and was already rusty as i was using it, haha. i'm welcome to any recommendations as a result, and if this needs to be a more fundamental change, then so be it! as it stands i'm a little uncertain how this might be taken as it involves three iterations in order to solve the original issue, which is a bit undesired, i feel like.

@twobitadder twobitadder force-pushed the reparent_keep_owner branch 2 times, most recently from 37ffe72 to 099c51a Compare October 3, 2023 19:18
@twobitadder
Copy link
Contributor Author

... once i fix all of the style issues, of course.

@twobitadder twobitadder force-pushed the reparent_keep_owner branch 2 times, most recently from 41ff19d to e9def5e Compare October 3, 2023 19:27
@RandomShaper
Copy link
Member

I will likely add new comments in the future (as soon as I can reason carefully about this). That said, your C++ is fine. (And if it's "rusty," well, it's trendy. 😃)

@twobitadder
Copy link
Contributor Author

hi! i just wanted to see if there was any chance to check this out a little more. if not, no problem, but i would love to be able to contribute a fix if possible.

@RandomShaper
Copy link
Member

This is still under my radar, but I have been too busy. I will come back very soon!

@twobitadder
Copy link
Contributor Author

no problem at all! i appreciate the update, i also know it's probably gotten quite a bit busier with all of the new people recently :)

@twobitadder
Copy link
Contributor Author

not a nitpick at all! i, uh, didn't realize i had been taking everybody else on the same journey i had been going on while updating this PR

@RandomShaper
Copy link
Member

Sorry for stepping in when everything looked settled. Just a few minor things I think can be improved.

@twobitadder
Copy link
Contributor Author

no problem! i'm definitely also learning as i go with this experience so it's useful for me as well

@akien-mga
Copy link
Member

akien-mga commented Oct 26, 2023

For the record, we discussed this together with other PR with Yuri and Clay from the production team and decided that this wasn't critical for 4.2 (which is nearing release candidate), and might need some testing to ensure everyone agrees with the behavior change, hence the milestone bump to 4.3.

It should likely be good to merge as soon as dev reopens for 4.3 in a few weeks.

@twobitadder
Copy link
Contributor Author

sounds good - i think that's completely reasonable given this is a fairly common action

@twobitadder
Copy link
Contributor Author

hi! just wanted to check in and see if this is still a good candidate for 4.3, and if there's anything else required of me to assess/change

@AThousandShips
Copy link
Member

AThousandShips commented Dec 21, 2023

The review comments you marked as resolved but didn't apply maybe? Or at least explain why not applying them since you marked them as resolved 🙂

@twobitadder
Copy link
Contributor Author

hmm, i'm not really sure... i applied all of the requested changes as far as i know, so i'm not certain why it's saying that there are more changes. maybe i missed one? reviewing the four requested changes i do see, i did enact them all before listing them as resolved (at the very least, the current version of the code reflects those changes)

@AThousandShips
Copy link
Member

Unresolved the ones you didn't apply 🙂

@twobitadder
Copy link
Contributor Author

ahh okay - the ones from KoBeWi were previously applied, but i had changed them out based on later requested changes by RandomShaper. they were resolved at the time though they do conflict now, is there a consensus on which direction i should go with those particular lines?

@AThousandShips
Copy link
Member

AThousandShips commented Dec 21, 2023

Then resolved, my bad, the change is correct 🙂 the interface is confusing when it considers code not outdated when it's changed back and forth 🙃

@twobitadder
Copy link
Contributor Author

twobitadder commented Dec 21, 2023

not a problem at all, and i appreciate the check! i just wanted to be sure i wasn't missing anything, haha

agreed on it not being intuitive as well, it'd be super cool if it noted that there were conflicting requested changes lol

@akien-mga
Copy link
Member

CC @RandomShaper

@akien-mga akien-mga merged commit f3fc35e into godotengine:master Jan 11, 2024
@akien-mga
Copy link
Member

akien-mga commented Jan 11, 2024

Thanks! And congrats for your first merged Godot contribution 🎉

@pc9098
Copy link

pc9098 commented Jan 25, 2024

This doesn't seem to be included in changelog

@twobitadder
Copy link
Contributor Author

correct me if i'm wrong, but i think this would only be in there if it is a part of a release. this missed the dev 2 window so it won't be in engine until dev 3 at earliest

@akien-mga
Copy link
Member

The interactive changelog isn't updated automatically, it was last synced when I released 4.2-dev2. So yeah changes merged after dev2 aren't reflected yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't access node by %UniqueName after .reparent()
7 participants