Reusable node without PackedScene?

Godot Version

4.3

Question

I’ve currently developed a system where the main way I create characters in my game is through scenes, so I can conveniently use the editor whenever possible.

So let’s say I have a Character Charlie, and Charlie has three orbs (red blue and green). These orbs can be used up and should be reset at a certain point.

So I have:

  • Charlie (Scene of class Character)
    • Red orb (Orb scene of class Orb, exported resource sets color to red)
    • Blue orb (…)
    • Green orb (…)

Now let’s say these orbs have been used up (queue_free()) or altered in some way (maybe blue has turned to purple through an item, etc.) and I want to reset to the initial state of the scene, is this possible?

in other words: I’ve stored the composition information in the scene tree itself, not in code, and I want to restore the initial composition of my nodes.

If I store the Scene just as a PackedScene, I don’t have the information which resource was used. Also as I understand there is no real way of “duplicating” a instanciated node, so I could save them as “initial_nodes” and put them in the tree later on.

Can someone help me in that regard?

var scene:=PackedScene.new()
scene.pack(NodePathToOrb)

creates new packed scene resource named scene
@export properties will be saved.

Save reference anywhere and instantiate it as usual from scene resource

different way is just to make invisible storing scene. Then Node.duplicate() it. Makes a copy with all subnodes and metas and properties. I use similar for non-unique items…

Thank you, yeah I didn’t know .duplicate() existed! I actually got it working with duplicate, I think it’s a bit more elegant in my case as I don’t need the path.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.