0 votes

When loading a previously saved PackedScene, I do not get the same scene as the original.

I have the following node tree ...
Spatial
- InstancedScenes

The following script is attached to the Spatial node

extends Spatial

const MyScene = preload("res://MyScene.tscn")

onready var node_to_be_packed = $InstancedScenes

func _ready():

#Create an arbitary sequence of instances of MyScene of different size
    for offset in range(0,4):
        var this_scene = MyScene.instance()
        this_scene.get_node("MeshInstance").translation = Vector3(0.0, 0.0, 1.5*float(offset))
        this_scene.get_node("MeshInstance").mesh.size = Vector3(0.5, 0.1, 0.1+0.2*float(offset))
        node_to_be_packed.add_child(this_scene)

#For convenience, to visualise the result
    var this_camera = Camera.new()
    add_child(this_camera)
    this_camera.translation = Vector3(1.0,3.0,6.0)
    this_camera.rotation_degrees = Vector3(-45.0, 0.0, 0.0)

#Now recursively assign 'node_to_be_packed' as the owner for all sub-nodes
    set_node_owner(node_to_be_packed, node_to_be_packed)

#And save the packed scene
    var package = PackedScene.new()
    var _result = package.pack(node_to_be_packed)
    var _error = ResourceSaver.save("res://packedscene.tscn", package)

#Recursive function to set node owner for all sub-nodes
func set_node_owner(this_node:Node, node_owner:Node)->void:
    for child in this_node.get_children():
        child.owner = node_owner
        set_node_owner(child, node_owner)

Where "MyScene" is simply a default CubeMesh, saved as a separate scene.

The resulting PackedScene, when saved, copied to a new project and instantiated via the editor, includes not only the desired instanced scenes, but also an instance of the original unmodified MyScene. I assume because I am not correctly assigning ownership, but I have not been able to solve the problem.

Any help appreciated, thanks

Drewton8

Godot version 3.3.2
in Engine by (18 points)

Please log in or register to answer this question.

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.