Best way to duplicate a node with subnodes

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


I’m implementing a game where the player shoot lot of bullets per sec. Those bullets are dynamic (lot of different kinds of bullets) and can become a bit complex (with multiple children). What is the best way to create very quickly those bullets in GDScript? Knowing that there are some conditions to know which kind of bullet to shoot, it wouldn’t be performant to go through those conditions every time.
I’ve seen there’s a “duplicate” function in the Node. Is it more or less efficient than instancing every time?

Thanks in advance for your answers.

There is a “bullet shower” demo BTW…

Bojidar Marinov | 2016-03-25 14:09

Thanks, Bojidar. Alas I already use the trick in this demo, which is to use an Area instead of a RigidBody. What I need is a way to quickly create dozen instances of the same scene without having to reconfigure it every time. For the moment, I guess using a pool of pre-created instances would be the most efficient. I created them in advance and simply add them in the main scene when I need some. And I refill later the pool in a background process.

Gokudomatic2 | 2016-03-26 07:12

:bust_in_silhouette: Reply From: batmanasb

Have you tried preload instead of load so that the scene is loaded at the start. For my gore system I preload a gore scene which is a RigidBody2D with a script and other nodes attached. Then I spawn between 40 and 120 instantly when my character loses a life or dies. It seems to work fine. So something like:

var goreNode = preload("res://character/gore.tscn")

func spawnGore(amount):
	#spawn gore
	for i in range(amount):
		var gore = goreNode.instance()

Decided to run a test and added “spawnGore(1)” into _fixed_process and it only began dropping frames after it reached about 2000 objects. But it should be a lot better for Area2D nodes. Made a low res gif of the results here.