I want to save my score, but I don't know how to do it.

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

Hello guys, I’ve made a game named Zombie Apocalypse. In this game, you have to kill as many zombies as you can. And everything works perfectly. But I just want to save my score after the game the ends. Every time I make a new high score, the game will ask me to enter my name and saves the score with that name. I just don’t know how to do it. Can you guys please help me on this? Or at least give me some idea on how to do it. Thank you.

[N.B: I’ve already read this document. https://docs.godotengine.org/en/3.2/tutorials/io/saving_games.html
But I still don’t get it.]

:bust_in_silhouette: Reply From: bloodsign

It would help a lot if you could create a autoload script so you can save or load your data anywhere in the game. Like a Global.gd
From there you need a dictionary to store your data. For example, a dictionary named save_data:

var save_data = {
	"score": 0,
	"name": "NAME"

Then you need to create a file(by code) to store the data structure: “save_data”

func save():
	var cfgFile = File.new()
	cfgFile.open("user://save.cfg", File.WRITE)

You call this function everytime you need to save data. It stores “save_data” into a file named “save.cfg”, the “user” location can be found if you go to [Project>Open Project Data Folder]

Then in order to load this data:

func load():
	var cfgFile = File.new()
	if not cfgFile.fileExists("user://save.cfg", File.READ)
	cfgFile.open("user://save.cfg", File.READ)
	var data = parse_json(cfgFile.get_as_text())
	save_data.score = data.score
	save_data.name = data.name

First you check if the file exists, if not, then you call save() so the file can be created.
Then the file is opened and passed into a variable named data.

You then retrieve this data back into your save_data dictionary.

So how do you exactly use these functions to save data?

In your main game logic(script/gd)where you need to save the score, If you created an autoload Global.gd, basically you just call :

 Global.save_data.score = your_player_score
 Global.save_data.name = "SOME PLAYER NAME"

And retrieving the data is also simple:

	score = Global.save_data.score 
	name = Global.save_data.name

Disclaimer: In no way I’m saying this is the only way to handle saving data. While this works, it’s also a good way of introducing how to handle loading and writing into file.

The next step is learning groups and organizing your scene tree.
You can find groups on the [Node] Tab next to the [Inspector] tab.
Under [Node] Tab, there is \Signals\ , and right next to it is \Groups\

Then on your scene tree, select any node you wish to save. Once it is highlighted, go back to the \Groups\ and type a name in the textbox, and hit [Add]. You’ll notice your selected node has a new ICON and below the Manage Groups a new entry. It means your selected node is now in that group.
Once you have cross this bridge, go back to the documentation and learn more about saving.