Files
Archipelago-Game/Persistence/persistence_manager.gd
2026-03-19 13:27:37 -06:00

52 lines
1.4 KiB
GDScript

extends Node
var _save_dictionary: Dictionary[NodePath, Variant] = {}
# Public Methods
func UpdateData(node_path: NodePath, value: Variant) -> void:
_save_dictionary[node_path] = value
func GetData(node_path: NodePath) -> Variant:
if not _save_dictionary.has(node_path):
return null
return _save_dictionary[node_path]
func HasData(node_path: NodePath) -> bool:
return _save_dictionary.has(node_path)
func SaveToDisk() -> void:
var save_file := FileAccess.open("user://savegame.save", FileAccess.WRITE)
var json := JSON.stringify(_save_dictionary)
save_file.store_line(json)
func LoadFromDisk() -> void:
if not FileAccess.file_exists("user://savegame.save"):
return
var save_file = FileAccess.open("user://savegame.save", FileAccess.READ)
print("Loading save file from path: %s" % save_file.get_path_absolute())
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
var data_dict: Dictionary = json.data
var node_dict: Dictionary[NodePath, Variant]
for key in data_dict:
var value: Variant = data_dict[key]
var node_path := NodePath(key)
node_dict[node_path] = value
_save_dictionary = node_dict
# Private Methods
func _ready() -> void:
LoadFromDisk()