This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+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 (92 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,510 points)
edited by

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.