0 votes

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.

node = Node

I have a scene called "Cell", there is a "Node" node and several "Sprite" node inside that.

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?

asked Feb 4, 2019 in Engine by hashak (44 points)
edited Feb 6, 2019 by hashak

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?

Yes, but I thought there might be a better way without instance a scene. like a professional programmer. I'm beginner :P

2 Answers

0 votes

I don't know if you are referring to this:

var loadscene = load("res://scenes/branchscene.tscn")
var instancedscene= loadscene .instance()

you can "save branch as a scene" and call it in code,no? i don't know if thats what do you want xd

answered Feb 5, 2019 by crossbit (71 points)

I updated my question, hopefully my question will be clear.

I had te same problem as hashak, and this solution did good for me, thanks.

+1 vote


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

answered Jan 3 by frankiezafe (86 points)
edited Jan 3 by frankiezafe
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.