system
February 4, 2019, 10:05am
1
Attention
Topic was automatically imported from the old Question2Answer platform.
Asked By
hashak
sorry for bad English.
you can right click on a node and choose “merge from scene” and import a selected node from another scene. Is it possible do that with code?
– Update –
For example:
I have a scene called “World”, there is a “Node” node inside that.
world.tscn
node = Node
I have a scene called “Cell”, there is a “Node” node and several “Sprite” node inside that.
cell.tscn
node = Node
--- sprite = Sprite
--- sprite1 = Sprite
--- sprite2 = Sprite
How can i import “sprite1” from “Cell scene” to “World scene”, without instance “Cell scene” inside “World scene”?
In other words, how can I access a node from a scene without instance that?
I know i can instance “cell” inside “world”, then move “sprite1” to “world node” and remove “cell” node. But I want to know is there a better way to do that?
I don’t know if I can help you. Have you tried making an instance of a scene, then taking a node from that scene, and putting it into the tree of your desired scene?
Ertain | 2019-02-05 00:50
Yes, but I thought there might be a better way without instance a scene. like a professional programmer. I’m beginner
hashak | 2019-02-05 09:31
system
February 5, 2019, 4:18pm
2
Reply From:
crossbit
I don’t know if you are referring to this:
var loadscene = load(“res://scenes/branchscene.tscn”)
var instancedscene= loadscene .instance()
get_node(“PATH_NODE”).add_child(instancedscene)
you can “save branch as a scene” and call it in code,no? i don’t know if thats what do you want xd
I updated my question, hopefully my question will be clear.
hashak | 2019-02-06 07:14
I had te same problem as hashak, and this solution did good for me, thanks.
GreyMiller | 2020-03-10 08:50
system
January 3, 2020, 2:59pm
3
Reply From:
frankiezafe
Hello,
After a bit of research, i found a way that works to edit/duplicate scene in a tool script.
The script assumes that you already have a “template” .tscn somewhere in your tree.
load the template
var template_path = "res://path/to/your/scene.tscn"
var template = load( template_path )
create a temporary instance of your template
var tmpl_instance = template.instance()
modify your template
tmpl_instance.get_node( "cube" ).scale = Vector3(2,0.5,2)
tmpl_instance.get_node( "collider" ).scale = Vector3(2,0.5,2)
create a new PackedScene containing your modified instance
var ps = PackedScene.new()
var result = ps.pack(tmpl_instance)
if result == OK:
ResourceSaver.save( template_path, ps )
your template is overwritten, if you open the scene in the editor, you will see that changes have been applied
template = load( template_path )
you can reload the template directly after the operation and start using it
If you adapt the path, you can create copies of your template without overwriting it. Might be useful to produce variations of a basic scene.
This is an adaptation of the example provided in PackedScene documentation
Made on v3.1.1 stable official