The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+6 votes

Hello all. I'm having a strange problem with saving game progress. I originally saved each variable separately to a file. That worked fine. There were SO MANY variables though, that every time I added a new one, or changed something around, it got too crazy to manage. So I tried to save an OBJECT instead. That way, I change the object class, and everything saves properly -- in theory. It doesn't work though. I'm missing a step somewhere and I hope you can help me with it. Here's what I have:

extends Node

var MyVar = 0

func _init(_MyVar):
    MyVar = _MyVar

extends Node
var oVars = preload("res://")
var MyStuff = []

extends Node

onready var myAutoLoad  = get_node("/root/Vars")
const FILE_NAME = "user://saves.json"

var env = {
    "varObj": []

func saveAll():
    env.varObj = myAutoLoad.MyStuff
    var file =, File.WRITE)

Now when I look at what was actually saved, I see this:


{"varObj": ["[Node:1355]"]}

I could have 1000 variables in there, and it still condenses them all into "Node:1355" instead of saving each variable in the object as a dictionary. Since it doesn't save each variable independently, I can't load it properly. :( UGH!

Thanks in advance for your help with this issue.

in Engine by (26 points)

4 Answers

+2 votes

The way you're trying to do this is kinda strange, the way i save & load dictionaries tought is this:

func save(var path : String, var thing_to_save):
var file =, File.WRITE)


func loadDictionary (var path : String) -> Dictionary:
var file =, File.READ)
var theDict = file.get_var()
return theDict
by (56 points)

I'm trying to save an object filled with variables instead of an individual variable. This way I can change the object as much as I want, without having to change my save code. Ideally, my save code will be "universal" so I just need to feed it any object filled with variables.

0 votes
var pathSave="user://save_game_";

var passFile="fwegfuywe7r632r732fdjghfvjhfesedwfcdewqyhfewjf"
#Any number of variables of different types
var data:Dictionary={}

func saveGame(id):

    var file =

    var path=pathSave+id as String

    file.open_encrypted_with_pass (path, File.WRITE,passFile)
    file.store_var(to_json(data), true)

func loadGame(id):
    var file =

    var path=pathSave+id as String

    if file.file_exists(path):

        file.open_encrypted_with_pass(path, File.READ,passFile)
        var loadParam=parse_json(file.get_var(true));

        if loadParam!=null:

by (57 points)

Hi. This does that same thing I'm already doing. The only change is the encryption. My save file still doesn't have all the individual variables in it. :(

+1 vote

It seems it is impossible to save whole objects like this. If You want to save all of the custom variables You create for own class, I would recommend using getscriptpropertylist() for that matter, it is an array of dictionaries containing only variables defined in scipt, and their values. If You want to save more, like built-in properties, than You can use normal getpropertylist(). When You load it, You will need to create new instance of your node, and iterate through list with simple loop like:
var z = node.instance()
For x in loadedproperty
list :
z.set(x, loadedproperty_list[x])

by (8,188 points)

All my variables are on object classes. For example, I have a player class, which holds the health, score, gold, etc. If I try to save that object, or load to it, the system won't do it. I created those classes so I could have multiple players, and hopefully save them individually. Since those aren't nodes, could I adapt this code for my purposes without having to restructure my entire data storage scheme?

I'm sorry, but You simply can't use objects as data holders for the purpose of loading. As You saw, the only thing saved this way is objects ID. But my sollution is only a bit more complicated than saving whole object - You are supposed to save this objects script property_list. This is a dictionary containing only custom variables You created for that object and their values, it looks like this { "health : 100, "gold" : 12342, "score" : 666} and so on. You don't have to make it anew, it is already there built in your class script, so all You have to do is get() it, update keys for present values while saving, and than assign every key value to subsequent property after loading it.

This should be universal and save every piece of information about your node regardless of how many custom variables they have. However I do believe it is better to prepare own stable dictionary for storing variables.

+1 vote

Since this is an old thread, I'm sure you've already discovered this; but for those struggling, the answer is to use a dictionary (the original poster was using an array) in the object to store your data. Dictionaries are directly useable by to_json() and parse_json(), while arrays are not.

Do not store your data in individual variables, as you will then have to construct/deconstruct a dictionary manually for the save and load operations. To insulate your program from your internal data representation, use accessor methods that read/write your dictionary rather than directly referencing your dictionary components.

Using this approach, you can add and remove variable items without ever having to modify your save and load functions.

by (595 points)

Hi. Thanks for your comment. I tried that approach and that didn't work for me. It's possible that my code was faulty though. Is there some sample code you could share for the initialization, the saving, and the loading?

Here's my core code for my Player class:

extends Node

var m_aData = {}

func getPassword():
    return m_aData["password"]

func setPassword(a):
    m_aData["password"] = a

func getData():
    return m_aData

func setData(a):
    m_aData = a

Add additional accessor methods to add support for additional fields. These will get and set the m_aData dictionary. The initial dictionary is empty, and automatically creates new entries when a set*() method references one that doesn't already exist. This is the only file you will ever have to update when you add or remove fields from the Player class.

Reading and writing the JSON files will automatically read/write the new fields you add to Player, and you can follow this pattern for every persistent data type you want to add to your game.

Here's how I save the dictionay to a JSON file:

func createAccount(stAccountname,stUsername,stPassword,stPath):
    print("Preparing new account")
    var player = load("res://player/").new()
    var stData

    stData = player.getData()
    return ConfigurationWriter.write(stPath,to_json(stData))

ConfigurationWriter.write() is just a helper function that opens a file, writes the JSON text, and closes the file; with error handling built-in. You will never have to modify this function to add or remove Player data fields.

Here's an example of reading the player data to validate the password:

func authenticatePassword(stPath,stPassword):
    var ret = false

    print("Authenticating password")
    if stPath != null && stPassword != null:
        var stData =
        var aData

        if stData != null:
            aData = parse_json(stData)
            ret = aData["password"] == stPassword
            print("Can't read data from " + stPath)
    return ret;

Again, is just a convenience method to open a file, read it, and return the raw text data (saved in JSON format).

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 Frequently asked questions and 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 [email protected] with your username.