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

Reparent node, maintaining transform #4008

Closed
kirilledelman opened this issue Mar 10, 2016 · 8 comments
Closed

Reparent node, maintaining transform #4008

kirilledelman opened this issue Mar 10, 2016 · 8 comments

Comments

@kirilledelman
Copy link

I would like to request a method added to CanvasItem object:

CanvasItem.reparent( new_parent, keep_world_transform=false )

The method would un-parent node from its current parent, and add itself as child to new_parent. If keep_world_transform is true, it would maintain its transform relative to scene root ( so get_global_transform() before the call and after would return the same transform ). After this call, with keep_world_transform true, the node would not appear to move, but switch parent.

Additionally, this method would NOT call _exit_tree or _enter_tree methods unless new_parent is not part of display tree.

I suspect it could be a pretty easy thing to add to engine itself, and kind of a pain to do in GD Script.

As an example, I'm using similar functionality to transplant player character from "room" to "room". Each "room" is dynamically loaded / discarded, and has own transform.

@neikeq
Copy link
Contributor

neikeq commented Mar 10, 2016

Related to issue #2284, which is about an option for the editor.

@bojidar-bg
Copy link
Contributor

@neikeq ~ I guess that solving that other issue would require making this function.

@kirilledelman ~ I guess it would be hard to not call _{exit,enter}_tree. Maybe you want to add a was_called_before boolean to your scripts? 😄

@neikeq
Copy link
Contributor

neikeq commented Mar 11, 2016

@bojidar-bg the other issue is already solved, but it's just an editor option :P

@bojidar-bg
Copy link
Contributor

@neikeq ~ whoops, did't notice it is closed... And about editor options... 😉

@kirilledelman
Copy link
Author

If it's a pain to prevent _enter_tree and _exit_tree let's omit it, but reparent( keep_transform ) could still be quite useful, even though it's possible to do it yourself via GDscript.

@bojidar-bg
Copy link
Contributor

Why close? This is still not done, and a nice function to do it wouldn't harm

@kirilledelman
Copy link
Author

Well I thought about it, and realized that without _exit_tree and _enter_tree it's trivial to do yourself. All you have to do is save get_world_transform, unparent, reparent, and set_world_transform. It's 4 statements. Not really something I wish to add to a looooong list of requested features.

@Diaspater
Copy link

I feel there is still a need for this function. In my game I want to have multiple rooms that are dynamically loaded and ran at runtime and the player can transition through them in an 'open world' (no loading screens) manner. While using the following code if functional, there is a noticeable flicker as the player being moved is first placed locally and then the position is fixed globally.
func set_room( node):
var parent = self.get_parent()
var transformation = get_global_transform()
parent.remove_child(self)
node.add_child(self)
self.set_owner(node)
set_global_transform(transformation)
Unfortunately due to the graphics requirements for the game, the player must be moved between room nodes in order for the ZY sorting to work.

@akien-mga akien-mga changed the title Request: Reparent node, maintaining transform Reparent node, maintaining transform Nov 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants