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:

func _init():
	if typeof(player) != TYPE_DICTIONARY:
		player = newPlayer

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

func save():
	var file =
	file.open_encrypted_with_pass(FILE_NAME, File.WRITE, OS.get_unique_id())

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