#Saving Running Scene as a Resource

4 messages · Page 1 of 1 (latest)

azure hearth
#

Hello, i try to save a running level as a Resource/Scene.

My code gives the error:
E 0:00:12:0665 game.gd:56 @ QuitGame(): Can't save empty resource to path 'user://level.tscn'.
<C++-Fehler> Condition "p_resource.is_null()" is true. Returning: ERR_INVALID_PARAMETER
<C++-Quelle> core/core_bind.cpp:153 @ save()
<Stacktrace> game.gd:56 @ QuitGame()
GameMenu.gd:25 @ _on_quit_button_pressed()

Although i have the scene loaded in the variable:
LevelStallhouse:<Node2D#2393873935485>

This is my code:
ResourceSaver.save(res, save_path)

res is the node2d, so the instanced package, which gets modified in game and then i want to just say it as a tscn

little reef
azure hearth
#

Hello,
ty for answering.

Hm that seemed to be the answer with saving, but now im struggling with reading it:

So i try to save a dictionary that holds ImageTextures and want to save it as a scene and then load it, atm the dictionary is alway empty after loading:

My Code to Save:

func SaveRegisteredMasks():
for mask in registered_masks:
print(mask.get_instance_id())
dictionary[mask.get_instance_id()] = mask.canvas_sprite.texture
var updated_database = bloodmask_database.instantiate() #its just an empty node2d with a dictionary
updated_database.dictionary = dictionary
print("updated database: ", updated_database.dictionary)
print("dictionary: ", dictionary) #gives eveyrthing back as i wanted
var new_scene = PackedScene.new()
var result = new_scene.pack(updated_database)
print(result)
print(new_scene, " ")
ResourceSaver.save(new_scene, filename)

My Code To Read:
if ResourceLoader.exists(filename):
print("bloodmask it does exist")
state_summary = ResourceLoader.load(filename)
print(state_summary)
var instance = state_summary.instantiate()
add_child(instance)
print(instance.dictionary, " vs ", instance) #this is alway empty 😦

I added this as reading, but its always empty also:
print(instance.dictionary, " vs ", instance)
var test = load(filename).instantiate()
print(test.dictionary)

azure hearth
#

Coding is hard, but for anyone stumbling upon this, i think all i want is working with this solution:

extends Node
class_name BloodMaskLastStateManager

var filename = "user://bms_state.tres"

@export var dictionary = {}

func _ready():
    if ResourceLoader.exists(filename):
        var db = ResourceLoader.load(filename)
        dictionary = db.dictionary
        print(db.dictionary)

func RegisterMask(mask):
    dictionary[mask.id] = mask.canvas_sprite.texture

func SaveMasksState():
    var updated_db = db_bm_state.new()
    updated_db.dictionary = dictionary
    ResourceSaver.save(updated_db, filename)