From aac4dbb1d9e0b38bdc0704a122f2fd8c33375264 Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Thu, 27 Mar 2025 21:42:14 -0400 Subject: [PATCH] Initial work toward autosaves --- .../autoloads/save_game_manager.gd | 27 +++++++++++++++++-- .../resources/save_game_settings_resource.gd | 2 ++ scenes/world/world.gd | 10 +++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/addons/save_load_system/autoloads/save_game_manager.gd b/addons/save_load_system/autoloads/save_game_manager.gd index b0b5f15..95f2761 100644 --- a/addons/save_load_system/autoloads/save_game_manager.gd +++ b/addons/save_load_system/autoloads/save_game_manager.gd @@ -18,6 +18,8 @@ signal load_error(error_message: String) signal load_save(filename: String) ## Loads the save into memory. Does NOT apply the load as this allows for other actions (such as resetting the levle/world).[br]Don't forget to run `apply_save` after loading signal save_complete signal save_error(error_message: String) +signal start_autosave +signal stop_autosave signal quick_save signal quick_load @@ -37,13 +39,15 @@ func _ready() -> void: load_save.connect(_on_load_game_save) quick_load.connect(_on_quick_load) quick_save.connect(_on_quick_save) + start_autosave.connect(_on_start_autosave) + stop_autosave.connect(_on_stop_autosave) toggle_save_icon_generation.connect(_on_toggle_save_icon_generation) func _unhandled_input(event: InputEvent) -> void: if event.is_action_pressed("quick_save"): - _on_quick_save() + quick_save.emit() if event.is_action_pressed("quick_load"): - _on_quick_load() + quick_load.emit() func list_saves(include_quick_saves: bool = true, include_auto_saves: bool = true) -> Array[SaveFileDetailsResource]: @@ -231,3 +235,22 @@ func _on_save_game_as_resource(save_name: String) -> void: func _on_toggle_save_icon_generation(toggled: bool) -> void: _enable_save_icon_generation = toggled + + +func _on_start_autosave() -> void: + if _save_game_settings == null: _load_save_settings() + + var autosave_timer: Timer = get_node_or_null("AutosaveTimer") + if autosave_timer == null: + autosave_timer = Timer.new() + autosave_timer.name = "AutosaveTimer" + autosave_timer.one_shot = false + autosave_timer.timeout.connect(_on_autosave_timer_timeout) + add_child(autosave_timer) + + autosave_timer.start(_save_game_settings.autosave_duration) + +func _on_stop_autosave() -> void: + var autosave_timer: Timer = get_node_or_null("AutosaveTimer") + if autosave_timer == null: return + autosave_timer.stop() diff --git a/addons/save_load_system/resources/save_game_settings_resource.gd b/addons/save_load_system/resources/save_game_settings_resource.gd index c08344a..8eb4ec9 100644 --- a/addons/save_load_system/resources/save_game_settings_resource.gd +++ b/addons/save_load_system/resources/save_game_settings_resource.gd @@ -10,6 +10,8 @@ extends Resource @export var save_game_data_path: String = "user://game_data/" @export var max_auto_saves: int = 5 +@export var autosave_duration: int = 60 ## How often, in seconds, should an autosave run[br]SaveGameManager will NOT autostart the autosave timer. This must be done by the world/scene. + @export var save_file_name_prepend: String = "save_" @export var quicksave_file_name_prepend: String = "quicksave_" @export var autosave_file_name_prepend: String = "autosave_" diff --git a/scenes/world/world.gd b/scenes/world/world.gd index 6009743..a2442a8 100644 --- a/scenes/world/world.gd +++ b/scenes/world/world.gd @@ -16,9 +16,12 @@ func _ready() -> void: EntityManager.reset_world.connect(clear_world) EntityManager.spawn_player.connect(spawn_player) SaveGameManager.load_complete.connect(_on_load_save_complete) + SignalManager.open_pause_menu.connect(_on_open_pause_menu) + SignalManager.resume_game.connect(_on_resume_game) clear_world() create_world() + SaveGameManager.start_autosave.emit() ## For resetting/emptying the world to allow for load_game or generating a new world func clear_world() -> void: @@ -87,3 +90,10 @@ func _on_reset_world() -> void: func _on_load_save_complete() -> void: clear_world() SaveGameManager.apply_save.emit() + SaveGameManager.start_autosave.emit() + +func _on_open_pause_menu() -> void: + SaveGameManager.stop_autosave.emit() + +func _on_resume_game() -> void: + SaveGameManager.start_autosave.emit()