scene vs. node-tree cloning/copying/duplicating programmatically/gdscript

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By raould
:warning: Old Version Published before Godot 3 was released.

I have a canonical enemy that is an Area with 2 children, a mesh and a collision shape. I want to copy it programmatically to have a zillion enemies (or, you know, 7 of them).

I wanted to do it all in the same Scene to keep it simply understandable but I haven’t seen how to do it and i have failed to do it.

I guess I am supposed to make a whole new Scene just for the Enemy and then bring it in and new() it a bunch of times? that’s really truly the recommended way to do these things?

:bust_in_silhouette: Reply From: Zylann

I would say yes, you should make your enemy object a scene, and instance it with var enemy = preload("enemy.tscn").instance() and then world.add_child(enemy). Because, who knows, maybe you will want to use it in another level in the future?
If you want to see it the scene from the editor you can still put it here.

But FYI, it’s possible to duplicate a node by using the duplicate() method.