+3 votes

is there any project out there of saving vars to file? and how to read them ?

I read the doc but i didn't figure it out

asked Jul 28, 2016 in Engine by Chr_ (63 points)
recategorized Aug 11, 2016 by Bojidar Marinov

1 Answer

+4 votes
Best answer

The documentation explain both how to read and write data and how to use it in a way it restores the game to its previous state.

Here is a simplified example on how to save and load variables in a file:

Save variables

# Construct a dictionary with whatever data you want
var data = {
    player_level = 42,
    last_item = "sword"
}

# Open a file
var file = File.new()
if file.open("user://saved_game.sav", File.WRITE) != 0:
    print("Error opening file")
    return

# Save the dictionary as JSON (or whatever you want, JSON is convenient here because it's built-in)
file.store_line(data.to_json())
file.close()

Load variables

# Check if there is a saved file
var file = File.new()
if not file.file_exists("user://saved_game.sav"):
    print("No file saved!")
    return

# Open existing file
if file.open("user://saved_game.sav", File.READ) != 0:
    print("Error opening file")
    return

# Get the data
var data = {}
data.parse_json(file.get_line())

# Then do what you want with the data

The path begins with "user://" so it targets a standard writable folder in the home of the user no matter which platform/OS the game runs on. For example on Windows it's "AppData/Local/Godot/..."

answered Jul 28, 2016 by Zylann (14,406 points)
selected Jul 28, 2016 by Chr_

Thank you for the answer. The saving is working very well but when i call the loading function on the start is not reading the vars, they start from beginning

For example i have :

var data = {apple = 1, pear = 1}

i press one button and the fruits increase by one and on the saving file i have
{"apple":2, "pear":2}

when i start the game again i have one apple and one pear

Did you saved the vars before quitting the game after you increased the numbers?

Yes, in the save_file.sav are the correct numbers

I wonder, has Godot changed since this was written?

I've just done an exact copy/paste of this and nothing happens. No error logging, no file created, no data written etc.

func _on_ButtonSave_pressed():

    # Construct a dictionary with whatever data you want
    var data = {
        player_level = 42,
        last_item = "sword"
    }

    # Open a file
    var file = File.new()
    if file.open("user://saved_game.txt", File.WRITE) != 0:
        print("Error opening file")
        return

    # Save the dictionary as JSON (or whatever you want, JSON is convenient here because it's built-in)
    file.store_line(data.to_json())
    file.close()

Did it print "Error opening file"?

No output of any kind. I'm on the latest Godot build from the website and running OSX if that makes any difference?

Are you checking under the same folder as usual? (it won't output next to the executable, but in a user writable directory. I don't know where it is on OSX though).
Otherwise it might be a bug, are you using 2.1.3? Is your code working in 2.1.2?

2.1.3 and I found it!
The files ARE being written and are being saved to:
/Users/username/.godot/app_userdata/

It's a hidden directory so I had to show hidden files to see it.

Thanks for your suggestion and hopefully that helps anyone in the future...

I have implemented a working savegame system on my game thanks to this answer!!!
I've learned a lot!
Thank you very much Zylann!!!!!!!!

Thank you so much sir you saved my life

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.