How to keep track of variables even after you closed the program?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By KripC2160

I am thinking of creating a program where you can keep track of things. But I don’t really know how to keep the stats after you close the program

:bust_in_silhouette: Reply From: jtarallo

Hi, I leave you a little snippet of how to save an encrypted file with a dictionary. Basically I have a newPlayer var which stores every initial value at 0, etc. So the first time the game loads and it doesn’t find the save file, it uses that variable to start the player and saves it. From there, remember to call the save function every time you change something of your player. One last note: encryption with OS.get_unique_id() doesn’t work on html5.

More info:
https://docs.godotengine.org/en/stable/tutorials/io/encrypting_save_games.html

func _init():
	self.loadData()
	if typeof(player) != TYPE_DICTIONARY:
		player = newPlayer
		self.save()

# saved data 
const FILE_NAME = "user://savedata.bin"

func save():
	var file = File.new()
	file.open_encrypted_with_pass(FILE_NAME, File.WRITE, OS.get_unique_id())
	file.store_var(self.player)
	file.close()

func loadData():
	var file = File.new()
	if file.file_exists(FILE_NAME):
		file.open_encrypted_with_pass(FILE_NAME, File.READ, OS.get_unique_id())
		var data = file.get_var()
		file.close()
		if typeof(data) == TYPE_DICTIONARY:
			self.player = data
		else:
			printerr("Corrupted data!")
	else:
		printerr("No saved data!")