ConfigFile or Global

Godot Version

4.2

Question

Help me understand how to save game data. In Godot’s manual I found two ways: via ConfigFile and Global autoload. I don’t understand the difference. Which is better to use?

Autoload will not actually save data to the disk, some use it to keep data between scenes, but not restarts. ConfigFile is a pretty good way to save data.

2 Likes

Thank you! But both methods create save files in the user data folder. So I don’t understand. It works the same way. What’s the difference then?

Autoload does not create save files in the user data folder.

ConfigFile creates a file where specified, in the example they use this path “user://scores.cfg”. Any path starting with “user://” goes to the user data folder. You could use FileAccess to create your own save format too.

# Create new ConfigFile object.
var config = ConfigFile.new()

# Store some values.
config.set_value("Player1", "player_name", "Steve")
config.set_value("Player1", "best_score", 10)
config.set_value("Player2", "player_name", "V3geta")
config.set_value("Player2", "best_score", 9001)

# Save it to a file (overwrite if already exists).
config.save("user://scores.cfg")
1 Like

Of course, such a file is not created automatically in Godot. But the global.gd file can be created independently. Then everything works the same.
Here is an example:

No, global.gd is in “res://” a read-only folder once exported. global.gd is a script file like any other, any variables edited during gameplay are not saved. Autoload does nothing to preserve data between runs.

ConfigFile also saves if you specify the path, otherwise nothing will work. If you write the save path in global.gd, it will create a save file «user://» and everything will be ok. There are many such examples on the Internet. Therefore, I have a question, how to do it correctly, through global or ConfigFile

If you could show an example of this that would be great, maybe we are misunderstanding each other. It sounds like you believe that a Autoload with a specific variable or name will automatically be saved in the user directory, which is not true.

I suggest the 3rd way - use json. They are simple to understand and work with, also good at performance.

In that tutorial you can see their global.gd contains a save function using FileAccess

extends Node

var position = Vector2(64, 512)
const SAVE_PATH = "user://savefile.dat"

func save_data():
    var file = FileAccess.open(SAVE_PATH, FileAccess.WRITE)
    file.store_var(position)
    file = null # should be file.close() or removed

func load_data():
    if not FileAccess.file_exists(SAVE_PATH):
        position = Vector2.ZERO
        save_data()
        # should return here, no need to load the same data
    var file = FileAccess.open(SAVE_PATH, FileAccess.READ)
    position = file.get_var()
    file = null # should be file.close() or removed

It’s not because it’s an autoload, but because they added these functions using FileAccess to write the file to disk, like ConfigFile.

Yes, that’s true. But why do they use autoload? Does it give any advantage?

Not for saving, just their example.

This is a brilliant video that should help you, as he explains multiple methods:

Thank you all