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

Node.replace_by() has strange behavior with instanced scenes #28746

Closed
Jeto143 opened this issue May 7, 2019 · 4 comments · Fixed by #55777
Closed

Node.replace_by() has strange behavior with instanced scenes #28746

Jeto143 opened this issue May 7, 2019 · 4 comments · Fixed by #55777

Comments

@Jeto143
Copy link

Jeto143 commented May 7, 2019

Godot version:

3.1

Issue description:

Using Node.replace_by to replace an instanced scene with another one created from script appears to merge the two subscenes instead of actually replacing.

Steps to reproduce:

  • Create three scenes: main, other, yetanother, each with a simple Node2D as base node. Then add different sprites to other and yetanother to distinguish between them.
  • In the main scene, instantiate the other scene (say, as Other) as a child with the editor.
  • Using script attached to main scene's base node, instantiate a yetanother scene with load('res://yetanother.scn') and call .instance() on it (call the resulting variable yetanother).
  • Add $Other.replace_by(yetanother) to the following line.

Instead of replacing the initially instanced scene node with the new one, this results in merging the two scenes together (which can be confirmed by looking at the Remote tab's scene tree during execution).

As an additional note, print($Other) will print [Object:null] then (because the node also took the new node's name), which imho is a bit counter-intuitive, though this is easily fixable with yetanother.name = 'Other'. I believe the replaced node should take the initial node's name by default, but that may be intended for reasons I'm missing.

Minimal reproduction project:

replace_by_bug.zip

@clayjohn clayjohn changed the title Node#replace_by has strange behavior with instanced scenes Node.replace_by has strange behavior with instanced scenes May 8, 2019
@clayjohn clayjohn changed the title Node.replace_by has strange behavior with instanced scenes Node.replace_by() has strange behavior with instanced scenes May 8, 2019
@bojidar-bg
Copy link
Contributor

Node.replace_by is used by the editor when changing the type of a node in the scene dock. As such, it makes little sense to lose children nodes.

The part of it which you want is just:

var parent = get_parent()
var pos_in_parent = get_position_in_parent()
parent.remove_child(self)
parent.add_child(other)
parent.move_child(other, pos_in_parent)

That being said, the documentation is quite lacking. Retagging.

@Jeto143
Copy link
Author

Jeto143 commented May 10, 2019

Yes, that's what I've been doing, and it's working fine, I was just curious why that method wouldn't do the same, considering what I thought it should do.

So I was wrong about that--still, replace_by seems a bit misleading as a term to describe a method that doesn't, technically, seem to replace the node it's called on with another :)

(In any case, documentation could at least make that more obvious indeed!)

@starry-abyss
Copy link
Contributor

If it's used by editor and works weirdly for games, maybe it should be made hidden/private from the user?

@berarma
Copy link
Contributor

berarma commented Oct 22, 2021

If it's used by editor and works weirdly for games, maybe it should be made hidden/private from the user?

Improving the documentation should be enough. If it's useful in the editor it can be useful in games and tools running in the editor.

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

Successfully merging a pull request may close this issue.

7 participants