Save 戈多保存选择屏幕

Save 戈多保存选择屏幕,save,godot,gdscript,Save,Godot,Gdscript,创建包含多个可选保存文件的“保存选择”屏幕的最佳方法是什么?到目前为止,我已设法使一个保存文件正常工作,但我不知道如何管理多个文件,保存到其中并在需要时加载该文件 这是我的保存和加载系统的代码 const FILE_NAME = "user://game-data1.json" var player = { "collected_level_one":false, } func save(): var file = File.

创建包含多个可选保存文件的“保存选择”屏幕的最佳方法是什么?到目前为止,我已设法使一个保存文件正常工作,但我不知道如何管理多个文件,保存到其中并在需要时加载该文件

这是我的保存和加载系统的代码

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!")

    

如果要保存多个文件。您需要使用多个可选插槽和一个保存和加载按钮。获取所选项目,然后在单击时进行保存或加载

调用保存或加载:

# 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)
# 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!")
您还需要将存储系统脚本添加为一个脚本。以便您可以在其他脚本中轻松访问它


添加对函数的调用后,需要相应地更改它们,以便能够管理多个保存文件

管理多个保存文件:

# 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)
# 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!")