A way to copy a node from one scene to another?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Syl

If you know, cheeeers!

Do you want an actual copy where the original still exists, or just to move a node from one scene to a different scene?

Eric Ellingson | 2020-02-07 21:06

Yes, an actual copy, keepin the original in his scene.

Syl | 2020-02-07 21:10

:bust_in_silhouette: Reply From: JoelImgu

you can try to create a global script wich is always accesible and create a variable and store it the node there and then add a node that you get from there.
I think there should be a better way but this should work at least.

Ok, seems that actually it would be faster to create the node once again. For me at least, and my knowledge of scripts and autoloads…


Syl | 2020-02-08 07:53

:bust_in_silhouette: Reply From: Eric Ellingson

Can you give this a try?

func clone(node: Node) -> Node:
	var copy = node.duplicate()
	# see https://docs.godotengine.org/en/3.1/classes/class_object.html#id2
	var properties: Array = node.get_property_list()
	var script_properties: Array = []
	for prop in properties:
		# see https://docs.godotengine.org/en/3.1/classes/class_@globalscope.html#enum-globalscope-propertyusageflags
            # basically here we are getting any of the user-defined script variables that exist, since those apparently don't
            # get copied via `duplicate()`
	for prop in script_properties:
		copy.set(prop.name, node[prop.name])
	return copy

I would suggest putting that in an autoloaded script (Utils.gd or something) so that you can call it from anywhere like:

var friend_node = Utils.clone(lonely_node)


Apparently this is not what you were asking. If you just want to be able to duplicate a node in the editor, save the node you want to duplicate as its own scene. Then go to the scene you want it to be in, right-click the root node, select “Instance Child Scene”, and select the node/scene you want.

Erh… Where will the copy go into my other scene?

And you know, i’d like to clone it and use it, editin it as another node in my other scene, not havin it appear when loadin the game eh. Just like a duplicate, but in another scene.

Syl | 2020-02-10 16:04

Updated the answer

Eric Ellingson | 2020-02-10 16:12

Thxs, but that’s still a bit complicated, though i understand it. I’d prefer avoidin to create and instance new scenes just for duplicated nodes.

Syl | 2020-02-10 16:27

Well that is how it works in Godot

Eric Ellingson | 2020-02-12 01:24

Just wanna share if you want to delete the scene after duplicating the node:
after clicking “Instance Child Scene”, you can right click the instance → “make local” on both instance, then you can delete the scene

daniteer | 2020-04-22 12:20

True, that simple. :slight_smile:

Syl | 2020-04-22 14:50

This is exactly what I needed.

vickera | 2021-06-04 01:47