Read & Write

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Chr_
:warning: Old Version Published before Godot 3 was released.

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

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/<PROJECT_NAME>/

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

ColbyWanShinobi | 2019-02-08 20:54

:bust_in_silhouette: Reply From: Zylann

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/…”

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

Chr_ | 2016-07-28 13:49

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

Zylann | 2016-07-28 17:55

Yes, in the save_file.sav are the correct numbers

Chr_ | 2016-07-28 19:14

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()

Robster | 2017-04-17 04:38

Did it print “Error opening file”?

Zylann | 2017-04-17 19:08

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

Robster | 2017-04-17 21:36

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?

Zylann | 2017-04-17 21:55

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…

Robster | 2017-04-18 09:42

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

Danicano | 2017-06-22 12:03

Thank you so much sir you saved my life

reguig | 2017-09-29 15:22

is it possible to change the file path?

Xian | 2019-06-30 05:36

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.

Zylann | 2019-06-30 15:33

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

########Problem############

Get the data

var data = {}
data.parse_json(file.get_line())

######Solution########

Get the data

var data = parse_json(file.get_line())
##########################################################

Happy Game making !

Darc | 2020-03-09 19:31