Files
Archipelago-Game/Persistence/persistence_manager.gd
cgandeg 7cd34cb07e beta-1.5 (#4)
Reviewed-on: #4
2026-03-28 20:34:59 -06:00

67 lines
1.7 KiB
GDScript

extends Node
const DEFAULT_SAVE_FILE_PATH = "user://savegame.save"
var _save_file_path: String
var _save_dictionary: Dictionary = {}
# Public Methods
func UpdateData(key: String, value: Variant) -> void:
_save_dictionary[key] = value
func UpdateNode(node_path: NodePath, value: Variant) -> void:
UpdateData(str(node_path), value)
func GetData(key: String) -> Variant:
if not _save_dictionary.has(key):
return null
return _save_dictionary[key]
func GetNode(node_path: NodePath) -> Variant:
var key := str(node_path)
return GetData(key)
func HasData(node_path: NodePath) -> bool:
return _save_dictionary.has(node_path)
func SaveToDisk() -> void:
if !_save_file_path:
return
var save_file := FileAccess.open(_save_file_path, FileAccess.WRITE)
var json := JSON.stringify(_save_dictionary)
save_file.store_line(json)
func LoadFromDisk(save_file_path: String) -> void:
_save_file_path = save_file_path
if not FileAccess.file_exists(save_file_path):
print_rich("[color=yellow]Save file at path %s does not exist, creating new save file...[/color]" % save_file_path)
return
var save_file := FileAccess.open(save_file_path, FileAccess.READ)
print("Loading save file from path: %s" % save_file.get_path_absolute())
if save_file.get_position() >= save_file.get_length():
print("Save file was empty...")
return
var line := save_file.get_line()
var json = JSON.new()
var parse_result := json.parse(line)
if parse_result != OK:
print("Parsing result when loading file: [%s] in %s at line %d" % [json.get_error_message(), line, json.get_error_line()])
return
print("JSON data parsed: [%d]" % typeof(json.data))
var data_dict: Dictionary = json.data
_save_dictionary = data_dict