+1 vote

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?

in Engine by (54 points)
edited by

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

by (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

by (108 points)
edited by
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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.