+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_ (73 points)
recategorized Aug 11, 2016 by Bojidar Marinov

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

+8 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")

# Save the dictionary as JSON (or whatever you want, JSON is convenient here because it's built-in)

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!")

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

# Get the data
var data = {}

# 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 (21,603 points)
edited Sep 19, 2019 by Zylann

2.1.3 and I found it!
The files ARE being written and are being saved to:

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.

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.