beta-1.5 (#4)
Reviewed-on: #4
This commit was merged in pull request #4.
This commit is contained in:
@@ -1,16 +1,28 @@
|
||||
extends Node
|
||||
|
||||
var _save_dictionary: Dictionary[NodePath, Variant] = {}
|
||||
const DEFAULT_SAVE_FILE_PATH = "user://savegame.save"
|
||||
|
||||
var _save_file_path: String
|
||||
var _save_dictionary: Dictionary = {}
|
||||
|
||||
# Public Methods
|
||||
func UpdateData(node_path: NodePath, value: Variant) -> void:
|
||||
_save_dictionary[node_path] = value
|
||||
func UpdateData(key: String, value: Variant) -> void:
|
||||
_save_dictionary[key] = value
|
||||
|
||||
|
||||
func GetData(node_path: NodePath) -> Variant:
|
||||
if not _save_dictionary.has(node_path):
|
||||
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[node_path]
|
||||
return _save_dictionary[key]
|
||||
|
||||
|
||||
func GetNode(node_path: NodePath) -> Variant:
|
||||
var key := str(node_path)
|
||||
return GetData(key)
|
||||
|
||||
|
||||
func HasData(node_path: NodePath) -> bool:
|
||||
@@ -18,17 +30,29 @@ func HasData(node_path: NodePath) -> bool:
|
||||
|
||||
|
||||
func SaveToDisk() -> void:
|
||||
var save_file := FileAccess.open("user://savegame.save", FileAccess.WRITE)
|
||||
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() -> void:
|
||||
if not FileAccess.file_exists("user://savegame.save"):
|
||||
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("user://savegame.save", FileAccess.READ)
|
||||
|
||||
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)
|
||||
@@ -36,16 +60,7 @@ func LoadFromDisk() -> void:
|
||||
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
|
||||
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()
|
||||
_save_dictionary = data_dict
|
||||
|
||||
Reference in New Issue
Block a user