Perfect way to make save and load system

I do not know about game system
However I need to make save load system

I’ve tried to find many different ways
But I couldn’t figure out what was most appropriate

What method should I use to create a save load system that can contain a lot of data?

Json? Built-in functionality of Godot? Dictionary?

Could you help me?

Use json, here is the codes:

var data = {
   "Coins": 12,
   "Score": 15
}

func _ready():
   load_data()

func load_data():
   if !FileAccess.file_exists("user://data.dat"):
      save_data()
      return
   
   var file = FileAccess.open("user://data.dat", FileAccess.READ)
   data = JSON.parse_string(file.get_as_text())
   file.close()

func save_data():
    var file = FileAccess.open("user://data.dat", FileAccess.WRITE)
	file.store_string(str(data))
	file.close()
1 Like

JSON data is related to the dictionary, which uses dynamic types (the Variant type).

However, Godot recommends using Resource to record data, which brings type safety—you won’t have to struggle with the type of specific data—and saving advantages:

  1. ResourceLoader, which the load() function depends on.
  2. ResourceSaver, which contains a useful save(String path) method.
  3. ResourcePreloader, which the preload() function uses.

This means you can save custom resources with ResourceSaver.save(my_resource_path), load/preload them with load(my_resource_path)/preload(my_resource_path), and directly store them in variables to use.

1 Like

JSON are good for loading and managing databases.

Resources are good for saving/loading save files.

2 Likes

Aha! Thank ypu so much!! It helped me a lot

This is a really good solution, too. I’m sorry I didn’t adopt it T_T
Thank you so much!

1 Like

Oh! using json could be best way!
Thank you so much!

I have just been using store_var() and load_var() with my data. Is there any reason to serialize to string or json before saving?

1 Like

Both are allowed

Use resources for organized data, JSON for flexible data, or communicate with APIs that require JSON data.

For example, game data such as player progress, saved levels, and user settings are statically organized, considering resources here will take many advantages. Likewise, sending internet requests depends on JSON, which stands for “JavaScript Object Notation”.

Here’s an example:

# Dictionary is common when dealing with JSON data
var dict := {
    x: 12
    y: 73
    list: [
        "Hello",
        "to",
        "JSON",
        "with",
        "dictionary"
    ]
}

# Converting example
var data_string := JSON.stringify(dict)
var my_data_again := JSON.parse_string(data_string)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.