refactoring save_game
This commit is contained in:
@@ -2,7 +2,7 @@ extends Node
|
||||
|
||||
const SAVE_PATH: String = "user://savegame.json"
|
||||
|
||||
var save_data: Dictionary = {}
|
||||
var save_data: SaveGameData = SaveGameData.new()
|
||||
var is_loaded: bool = false
|
||||
|
||||
func _ready() -> void:
|
||||
@@ -10,22 +10,12 @@ func _ready() -> void:
|
||||
|
||||
func save_game() -> void:
|
||||
var save_file = FileAccess.open(SAVE_PATH, FileAccess.WRITE)
|
||||
var save_nodes = get_tree().get_nodes_in_group("Saveable")
|
||||
var new_save_data: Dictionary = {}
|
||||
|
||||
for node in save_nodes:
|
||||
if not node.has_method("get_save_data"):
|
||||
continue
|
||||
|
||||
var node_data = node.get_save_data()
|
||||
var node_path = str(node.get_path())
|
||||
new_save_data[node_path] = node_data
|
||||
|
||||
var json_string = JSON.stringify(new_save_data)
|
||||
var json_string = JSON.stringify(save_data.to_dictionary())
|
||||
save_file.store_line(json_string)
|
||||
|
||||
func load_game() -> void:
|
||||
if not FileAccess.file_exists(SAVE_PATH):
|
||||
save_data = SaveGameData.new()
|
||||
is_loaded = true
|
||||
return
|
||||
|
||||
@@ -35,7 +25,8 @@ func load_game() -> void:
|
||||
var json = JSON.new()
|
||||
var parse_result = json.parse(json_string)
|
||||
|
||||
if parse_result == OK:
|
||||
save_data = json.data
|
||||
save_data = SaveGameData.new()
|
||||
if parse_result == OK and json.data is Dictionary:
|
||||
save_data = SaveGameData.from_dictionary(json.data)
|
||||
|
||||
is_loaded = true
|
||||
|
||||
Reference in New Issue
Block a user