-1

What would be the best way to create a save select screen with more than 1 selectable save file, so far i have managed to get one save file working but i don't know how i would manage more than one of them, saving to it and loading that particular one when needed

here's the code for my saving and loading system

const FILE_NAME = "user://game-data1.json"

var player = {
    "collected_level_one":false,
    
}

func save():
    var file = File.new()
    file.open(FILE_NAME, File.WRITE)
    file.store_string(to_json(player))
    file.close()

func load():
    var file = File.new()
    if file.file_exists(FILE_NAME):
        file.open(FILE_NAME, File.READ)
        var data = parse_json(file.get_as_text())
        file.close()
        if typeof(data) == TYPE_DICTIONARY:
            player = data
        else:
            printerr("Corrupted data!")
    else:
        printerr("No saved data!")

    
zak_250
  • 9
  • 1
  • 1

1 Answers1

0

If you want to have multiple save files. You would need use multiple selectable slots and a save and load button. That get your selected item and then either save or load, when they are clicked.

Calling save or load:

# Clicked save button.
func _on_Button_pressed_save() -> void:
    # Get the selected save/load slot.
    var index
    # Save into the selected file.
    SaveSystem.save(index)

# Clicked load button.
func _on_Button_pressed_save() -> void:
    # Get the selected save/load slot.
    var index
    # Load the selected file.
    SaveSystem.load(index)

You would also need to add your Savesystem script as a Singleton (Autoload). So that you can access it easily in other scripts.


After you've added the call to the functions you need to change them accordingly to be able to manage multiple save files.

Managing multiple save files:

# Clicked save button.
const FILE_NAME = "user://game-data"
const FILE_EXTENSION = ".json"

var player = {
    "collected_level_one":false,
}

# Saves into the file with the given index.
func save(index : int) -> void:
    var file := File.new()
    var file_name := FILE_NAME + to_str(index) + FILE_EXTENSION
    file.open(file_name, File.WRITE)
    file.store_string(to_json(player))
    file.close()

# Loads the file with the given index.
func load(index : int) -> void:
    var file := File.new()
    var file_name := FILE_NAME + to_str(index) + FILE_EXTENSION
    if file.file_exists(file_name):
        file.open(FILE_NAME, File.READ)
        var data := parse_json(file.get_as_text())
        file.close()
        if typeof(data) == TYPE_DICTIONARY:
            player = data
        else:
            printerr("Corrupted data!")
    else:
        printerr("No saved data!")
IndieGameDev
  • 2,905
  • 3
  • 16
  • 29