+1 vote

Hi I am a beginner on GoDot scripting and had a problem to save a custom generated scene.
Here my GdScript to save a custom generated scene.

extends Node 

func _ready():
    example()
    #generate_level()
    #load_level()
    pass


func  example():
    var roomTemplates = ResourceLoader.load("res://genarate/room_templates.tscn")
    if roomTemplates == null: # check for errors
        print("cant load room templates")
        return

    # get example room from template
    var root = roomTemplates.instance()
    var startRoom = root.get_node("start")

    # build new scene node and add room
    var level = Node.new()
    level.set_name("test")
    var parent = startRoom.duplicate()
    level.add_child(parent)
    parent.set_owner(level)

    # add children to parent
    # ? why is this necessayr becaus i use duplicate on original node?
    for child in parent.get_children():
        parent.add_child(child)
        child.set_owner(parent)
    print("scene to save")
    level.print_tree()

    # save scene
    print("save scene")
    var packed_scene = PackedScene.new()
    var result = packed_scene.pack(level)
    if result == OK:
        print("save doungen")
        print(ResourceSaver.save("res://test.tscn", packed_scene))

    print("load scene")
    var scene = ResourceLoader.load("res://test.tscn")
    if scene == null: # check for errors
        print("cant load scene")
        return
    print("scene loading success")
    scene.instance().print_tree()

The script works so far but the scene is not fully saved.
Here the sysout

scene to save
.
start
start/CollisionShape
start/floor
start/ceiling
start/wall
start/wall2
start/wall3
start/wall4
start/wall5
start/wall6
start/door
save scene
save doungen
0
load scene
scene loading success
.
start

The saved scene contains only the "start" node without any childs.
But when I save the parent node instead all childs are saved.

What I doing wrong?

in Engine by (46 points)
edited by

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.