Browse Source

Fixing appending saves

pull/18/head
Ryan Reed 1 month ago
parent
commit
e5dbf84a57
1 changed files with 30 additions and 25 deletions
  1. +30
    -25
      addons/save_load_system/autoloads/save_game_manager.gd

+ 30
- 25
addons/save_load_system/autoloads/save_game_manager.gd View File

@ -25,7 +25,7 @@ signal toggle_save_icon_generation(toggled: bool) ## Enable/Disable the generati
var _enable_save_icon_generation: bool = true var _enable_save_icon_generation: bool = true
var _game_data_resource: SaveGameDataResource = SaveGameDataResource.new()
var _loaded_save_resource: SaveGameDataResource = SaveGameDataResource.new()
var _save_icon_size: Vector2i = Vector2i(896, 504) ## If Vector2.ZERO, uses the user's resolution var _save_icon_size: Vector2i = Vector2i(896, 504) ## If Vector2.ZERO, uses the user's resolution
var _save_level_data_component: SaveLevelDataComponent ## Contains the save paths and filenames var _save_level_data_component: SaveLevelDataComponent ## Contains the save paths and filenames
@ -91,6 +91,22 @@ func list_saves(include_quick_saves: bool = true, include_auto_saves: bool = tru
func _custom_save_file_sort(a: SaveFileDetailsResource, b: SaveFileDetailsResource) -> bool: func _custom_save_file_sort(a: SaveFileDetailsResource, b: SaveFileDetailsResource) -> bool:
return a.date_created > b.date_created return a.date_created > b.date_created
## Save the properties defined on the SaveDataComponents attached to various nodes (such as Block)
func _generate_save_game_resource() -> SaveGameDataResource:
var nodes: Array = get_tree().get_nodes_in_group("save_data_component")
if nodes == null: return
var _resource: SaveGameDataResource = SaveGameDataResource.new()
for node: Node in nodes:
if node is SaveDataComponent:
@warning_ignore("unsafe_method_access")
var save_data_resource: Node3DDataResource = node._save_data()
var save_final_resource: Node3DDataResource = save_data_resource.duplicate()
_resource.save_data_nodes.append(save_final_resource)
return _resource
## Generate the texture for use in the save file listing ## Generate the texture for use in the save file listing
func _generate_save_icon_texture(save_icon: String) -> Texture2D: func _generate_save_icon_texture(save_icon: String) -> Texture2D:
var _icon_texture: Texture2D = ImageTexture.new() var _icon_texture: Texture2D = ImageTexture.new()
@ -124,23 +140,26 @@ func _load_game_resource(resource_filename: String) -> void:
load_error.emit("Failed to load save. File does not exist: %s" % save_game_file_path) load_error.emit("Failed to load save. File does not exist: %s" % save_game_file_path)
return return
_game_data_resource = ResourceLoader.load(save_game_file_path)
if _game_data_resource == null:
_loaded_save_resource = SaveGameDataResource.new()
_loaded_save_resource = ResourceLoader.load(save_game_file_path)
if _loaded_save_resource == null:
load_error.emit("Failed to load save. Unknown format? %s" % save_game_file_path) load_error.emit("Failed to load save. Unknown format? %s" % save_game_file_path)
return return
load_complete.emit() load_complete.emit()
func _save_game_as_resource(resource_filename: String) -> void:
func _save_game_as_resource(save_name, resource_filename: String) -> void:
if not _load_save_level_data_component(): return if not _load_save_level_data_component(): return
if !DirAccess.dir_exists_absolute(_save_level_data_component.settings.save_game_data_path): if !DirAccess.dir_exists_absolute(_save_level_data_component.settings.save_game_data_path):
DirAccess.make_dir_absolute(_save_level_data_component.settings.save_game_data_path) DirAccess.make_dir_absolute(_save_level_data_component.settings.save_game_data_path)
var save_game_file_path: String = _save_level_data_component.settings.save_game_data_path + resource_filename var save_game_file_path: String = _save_level_data_component.settings.save_game_data_path + resource_filename
_save_node_data()
var result: int = ResourceSaver.save(_game_data_resource, save_game_file_path)
var _save_resource: SaveGameDataResource = _generate_save_game_resource()
_save_resource.save_name = save_name
var result: int = ResourceSaver.save(_save_resource, save_game_file_path)
if result != OK: if result != OK:
save_error.emit("Failed to save game (" , result, "): " + save_game_file_path) save_error.emit("Failed to save game (" , result, "): " + save_game_file_path)
return return
@ -148,19 +167,6 @@ func _save_game_as_resource(resource_filename: String) -> void:
_take_save_screenshot(save_game_file_path) _take_save_screenshot(save_game_file_path)
save_complete.emit() save_complete.emit()
## Save the properties defined on the SaveDataComponents attached to various nodes (such as Block)
func _save_node_data() -> void:
var nodes: Array = get_tree().get_nodes_in_group("save_data_component")
if nodes == null: return
for node: Node in nodes:
if node is SaveDataComponent:
@warning_ignore("unsafe_method_access")
var save_data_resource: Node3DDataResource = node._save_data()
var save_final_resource: Node3DDataResource = save_data_resource.duplicate()
_game_data_resource.save_data_nodes.append(save_final_resource)
## Takes a screenshot and saves next to the save file[br] ## Takes a screenshot and saves next to the save file[br]
## The icon utilizes the same filename as the save file, replacing `.tres` with `.png` ## The icon utilizes the same filename as the save file, replacing `.tres` with `.png`
func _take_save_screenshot(save_game_file_path: String) -> void: func _take_save_screenshot(save_game_file_path: String) -> void:
@ -177,10 +183,10 @@ func _take_save_screenshot(save_game_file_path: String) -> void:
## Apply the loaded save. Should be performed after load_save ## Apply the loaded save. Should be performed after load_save
func _on_apply_save() -> void: func _on_apply_save() -> void:
if _game_data_resource == null: return
if _loaded_save_resource == null: return
var root_node: Window = get_tree().root var root_node: Window = get_tree().root
for resource: Resource in _game_data_resource.save_data_nodes:
for resource: Resource in _loaded_save_resource.save_data_nodes:
if resource is Node3DDataResource: if resource is Node3DDataResource:
(resource as Node3DDataResource)._load_data(root_node) (resource as Node3DDataResource)._load_data(root_node)
@ -206,8 +212,7 @@ func _on_quick_load() -> void:
func _on_quick_save() -> void: func _on_quick_save() -> void:
if not _load_save_level_data_component(): return if not _load_save_level_data_component(): return
_game_data_resource.save_name = "Quick Save"
_save_game_as_resource(_save_level_data_component.settings.quicksave_file_name)
_save_game_as_resource("Quick Save", _save_level_data_component.settings.quicksave_file_name)
## Save the game, with a filename of `<save_prepend><current_date>.tres ## Save the game, with a filename of `<save_prepend><current_date>.tres
func _on_save_game_as_resource(save_name: String) -> void: func _on_save_game_as_resource(save_name: String) -> void:
@ -216,8 +221,8 @@ func _on_save_game_as_resource(save_name: String) -> void:
var current_date: String = Time.get_datetime_string_from_system().replace(":", "") var current_date: String = Time.get_datetime_string_from_system().replace(":", "")
var _filename: String = "save_" + current_date + ".tres" var _filename: String = "save_" + current_date + ".tres"
_game_data_resource.save_name = save_name
_save_game_as_resource(_filename)
_loaded_save_resource.save_name = save_name
_save_game_as_resource(save_name, _filename)
save_complete.emit() save_complete.emit()
func _on_toggle_save_icon_generation(toggled: bool) -> void: func _on_toggle_save_icon_generation(toggled: bool) -> void:


Loading…
Cancel
Save