From 1f77bc451345de7800503b34634926ebae7358e0 Mon Sep 17 00:00:00 2001 From: Haze Weathers Date: Sat, 8 Jul 2023 11:31:35 -0400 Subject: [PATCH] comment save.gd and add format version --- autoloads/save.gd | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/autoloads/save.gd b/autoloads/save.gd index 760c916..f4cc78a 100644 --- a/autoloads/save.gd +++ b/autoloads/save.gd @@ -1,23 +1,27 @@ extends Node -## contains the save data for a single level +# currently-used compatible format version +const FORMAT_VERSION: int = 1 + +# contains the save data for a single level class LevelSaveData: + # save file category to use for this level var save_id: String - + # scores var score_any: int = 0 var score_100: int = 0 - + # times var time_any: float = INF var time_100: float = INF - + # times died in that level var deaths: int - + # collectibles var keys_collected: int = 0 var shards_collected: Array - func _init(id: String) -> void: save_id = id + # initialize shards array shards_collected = [] shards_collected.resize(8) for i in 8: @@ -55,9 +59,12 @@ class LevelSaveData: for i in 8: shards_collected[i] = file.get_value(save_id, "shard_%d" % i, false) +# contains data of one save file class SaveFile: # path of file to load/save from var file_path: String + # version of save file format + var version: int = FORMAT_VERSION # name of the save file var name: String = "" # difficulty chosen for the file @@ -69,6 +76,7 @@ class SaveFile: func _init(path: String) -> void: file_path = path + # initialize level save data dictionary levels = {} for level in LevelData.levels: levels[level.save_id] = LevelSaveData.new(level.save_id) @@ -94,9 +102,12 @@ class SaveFile: total += level.deaths return total + # loads data from the file at `path` func load_from_file() -> void: var file = ConfigFile.new() file.load(file_path) + # get save format version + version = file.get_value("options", "version", 0) # load name name = file.get_value("options", "name", "SG") # load difficulty @@ -111,6 +122,8 @@ class SaveFile: func save_to_file() -> void: var file = ConfigFile.new() + # use current format version + file.set_value("options", "version", FORMAT_VERSION) # save name file.set_value("options", "name", name) # save difficulty @@ -124,6 +137,7 @@ class SaveFile: level.save_to_file(file) file.save(file_path) +## currently used save file var current_file: SaveFile = null func _ready() -> void: @@ -131,10 +145,7 @@ func _ready() -> void: current_file = load_file("user://file1.pr") Game.difficulty = current_file.difficulty -func _on_letter_chosen(glyph: String): - if current_file != null: - current_file.name += glyph - +## shortcut for loading a save file from specific path func load_file(path: String) -> SaveFile: var file = SaveFile.new(path) file.load_from_file()