dynamic objects saving

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Ktotoetoia

hi, im making save system for my game,i need to save instance objects variable, but this code save only last object variable:

func _save():
var save_game = File.new()
save_game.open("res://saves/save.json", File.WRITE)
var save_nodes = get_tree().get_nodes_in_group("saved")
for node in save_nodes:
	if node.filename.empty():
		print("persistent node '%s' is not an instanced scene, skipped" % node.name)
		continue
		if !node.has_method("_save"):
			print("persistent node '%s' is missing a save() function, skipped" % node.name)
			continue
	var node_data = node.call("_save")
	save_game.store_line(to_json(node_data))
	save_game.close()

how can i save every object variable?

:bust_in_silhouette: Reply From: Ktotoetoia

я изменил код на это, и его работа:

func _save():
    var save_nodes = get_tree().get_nodes_in_group("saved")
    var data = {
        }
    for node in save_nodes:
        data[node.name] = node._save()
    FS.save_data('res://saves/txt_save/'+str(FS.OPN_LVL_ID),data)

func _load():
    var save_nodes = get_tree().get_nodes_in_group("saved")
    var data = FS.load_data('res://saves/txt_save/'+str(FS.OPN_LVL_ID))
    for node in save_nodes:
        node._load(data[node.name])

Глобальный

func save_data(filename, data):
    file.open(filename,file.WRITE)
    file.store_var(data)
    file.close()
func load_data(filename):
    file.open(filename, file.READ)
    var data = file.get_var()
    file.close()
    return data