+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

in Engine by (91 points)
recategorized by

I know this is old, but it was one of the best search results I found and still very useful:

As of Godot 3.0.6 these files are now located here on MacOS Mojave (10.14):
/Users//Library/Application Support/Godot/app_userdata//

So, take for example:
-Save file of: "user://SaveGame.sav"
-Local User Name of "JHenry"
-A Godot project called "MyGame"

would find the save game file in this location:

/Users/JHenry/Library/Application Support/Godot/app_userdata/MyGame/SaveGame.sav

1 Answer

+10 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:
(This was in Godot 2.1, but it's not much different in 3.1, use to_json(data) instead of data.to_json())

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/..."

by (29,088 points)
edited by

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

is it possible to change the file path?

You can choose a raw path if you like, for example saves/thing.save which will be next to the executable, or C:/game/thing.save, provided those folders exist or were created by the game. However, it is recommended to use the path in user:// because other paths might not be allowed on some platforms.

I ran into some issues with the parsing function in godot V3.2

########Problem############
# Get the data
var data = {}
data.parsejson(file.getline())

######Solution########
# Get the data
var data = parsejson(file.getline())
##########################################################

Happy Game making !

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.