comment save.gd and add format version

This commit is contained in:
Haze Weathers 2023-07-08 11:31:35 -04:00
parent e524faba5f
commit 1f77bc4513

View file

@ -1,23 +1,27 @@
extends Node 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: class LevelSaveData:
# save file category to use for this level
var save_id: String var save_id: String
# scores
var score_any: int = 0 var score_any: int = 0
var score_100: int = 0 var score_100: int = 0
# times
var time_any: float = INF var time_any: float = INF
var time_100: float = INF var time_100: float = INF
# times died in that level
var deaths: int var deaths: int
# collectibles
var keys_collected: int = 0 var keys_collected: int = 0
var shards_collected: Array var shards_collected: Array
func _init(id: String) -> void: func _init(id: String) -> void:
save_id = id save_id = id
# initialize shards array
shards_collected = [] shards_collected = []
shards_collected.resize(8) shards_collected.resize(8)
for i in 8: for i in 8:
@ -55,9 +59,12 @@ class LevelSaveData:
for i in 8: for i in 8:
shards_collected[i] = file.get_value(save_id, "shard_%d" % i, false) shards_collected[i] = file.get_value(save_id, "shard_%d" % i, false)
# contains data of one save file
class SaveFile: class SaveFile:
# path of file to load/save from # path of file to load/save from
var file_path: String var file_path: String
# version of save file format
var version: int = FORMAT_VERSION
# name of the save file # name of the save file
var name: String = "" var name: String = ""
# difficulty chosen for the file # difficulty chosen for the file
@ -69,6 +76,7 @@ class SaveFile:
func _init(path: String) -> void: func _init(path: String) -> void:
file_path = path file_path = path
# initialize level save data dictionary
levels = {} levels = {}
for level in LevelData.levels: for level in LevelData.levels:
levels[level.save_id] = LevelSaveData.new(level.save_id) levels[level.save_id] = LevelSaveData.new(level.save_id)
@ -94,9 +102,12 @@ class SaveFile:
total += level.deaths total += level.deaths
return total return total
# loads data from the file at `path`
func load_from_file() -> void: func load_from_file() -> void:
var file = ConfigFile.new() var file = ConfigFile.new()
file.load(file_path) file.load(file_path)
# get save format version
version = file.get_value("options", "version", 0)
# load name # load name
name = file.get_value("options", "name", "SG") name = file.get_value("options", "name", "SG")
# load difficulty # load difficulty
@ -111,6 +122,8 @@ class SaveFile:
func save_to_file() -> void: func save_to_file() -> void:
var file = ConfigFile.new() var file = ConfigFile.new()
# use current format version
file.set_value("options", "version", FORMAT_VERSION)
# save name # save name
file.set_value("options", "name", name) file.set_value("options", "name", name)
# save difficulty # save difficulty
@ -124,6 +137,7 @@ class SaveFile:
level.save_to_file(file) level.save_to_file(file)
file.save(file_path) file.save(file_path)
## currently used save file
var current_file: SaveFile = null var current_file: SaveFile = null
func _ready() -> void: func _ready() -> void:
@ -131,10 +145,7 @@ func _ready() -> void:
current_file = load_file("user://file1.pr") current_file = load_file("user://file1.pr")
Game.difficulty = current_file.difficulty Game.difficulty = current_file.difficulty
func _on_letter_chosen(glyph: String): ## shortcut for loading a save file from specific path
if current_file != null:
current_file.name += glyph
func load_file(path: String) -> SaveFile: func load_file(path: String) -> SaveFile:
var file = SaveFile.new(path) var file = SaveFile.new(path)
file.load_from_file() file.load_from_file()