I have a small Scene that is splashing a visual effect on each collision. Instead of having a spawner that spawns Scenes of this type on each collision, I want the Scene to auto-spawn clones of itself. This is easier for me in terms of code, num of scenes involved, configuration, and more.
How can one scene replicate itself from code?
I have found 3 solutions.
var instance = (load(scene_file_path) as PackedScene).instantiate()
var instance = self.duplicate()
(From the @zett42's comment)
A) gives you a clean scene for each new instance, while B) duplicates the scene in its current state.
If you do A) very often, it would be inefficient, as you are loading the scene from the file again and again.
Possible alternative:
Save scene to a script variable _self_scene
of type PackedScene
in _ready()
like this:
_self_scene = PackedScene.new()
_self_scene.pack(self)
Whenever you need to self-replicate:
var instance = _self_scene.instantiate().
In all cases, the new instance is born without a parent, so you must manually add it to a parent.