+1 vote

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?

Godot version 3.4
in Engine by (79 points)

1 Answer

0 votes

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

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
by (79 points)
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.