The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

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.]

in Engine by (137 points)
edited by

1 Answer

+2 votes
Best answer

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)
    cfgFile.store_line(to_json(save_data))
    cfgFile.close()

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)
        save()
        return
    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"
 Global.save()

And retrieving the data is also simple:

    Global.load()
    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.

by (378 points)
selected by
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.