extends Node #region Game Options Signals signal game_options_autosaves_changed(autosaves_enabled: bool) signal game_options_block_highlight_changed(block_highlight_enabled: bool) signal game_options_creative_mode_changed(creative_mode_enabled: bool) signal game_options_held_block_ui_changed(held_block_enabled: bool) signal game_options_quickslots_ui_changed(quickslots_enabled: bool) signal game_options_screenshot_saves_changed(screenshots_enabled: bool) signal game_options_waila_changed(waila_enabled: bool) #endregion #region Graphics Settings Signals signal apply_graphics_settings signal graphics_fov_changed(fov: int) signal graphics_resolution_changed(resolution: Vector2i, id: int) signal graphics_vsync_changed(vsync_enabled: bool) signal graphics_window_mode_changed(mode_id: int) #endregion signal load_game_settings signal save_game_settings ## See documentation to where this path is: https://docs.godotengine.org/en/stable/tutorials/io/data_paths.html#accessing-persistent-user-data-user[br][br] ## Default Paths:[br] ## * Windows: %APPDATA%\Godot\app_userdata\[project_name][br] ## * macOS: ~/Library/Application Support/Godot/app_userdata/[project_name][br] ## * Linux: ~/.local/share/godot/app_userdata/[project_name][br] var save_data_path: String = "user://game_data/" var settings_file_name: String = "game_settings.tres" var settings_file_path: String = save_data_path + settings_file_name var settings: GameSettingsResource = GameSettingsResource.new() func _init() -> void: game_options_autosaves_changed.connect(_on_game_options_autosaves_changed) game_options_block_highlight_changed.connect(_on_game_options_block_highlight_changed) game_options_creative_mode_changed.connect(_on_game_options_creative_mode_changed) game_options_held_block_ui_changed.connect(_on_game_options_held_block_ui_changed) game_options_quickslots_ui_changed.connect(_on_game_options_quickslots_ui_changed) game_options_screenshot_saves_changed.connect(_on_game_options_screenshot_saves_changed) game_options_waila_changed.connect(_on_game_options_waila_changed) graphics_fov_changed.connect(_on_graphics_fov_changed) graphics_resolution_changed.connect(_on_graphics_resolution_changed) graphics_window_mode_changed.connect(_on_graphics_window_mode_changed) graphics_vsync_changed.connect(_on_graphics_vsync_changed) apply_graphics_settings.connect(_on_apply_graphics_settings) load_game_settings.connect(load_settings) save_game_settings.connect(save_settings) #region Game Option Settings func _on_game_options_autosaves_changed(autosaves_enabled: bool) -> void: settings.game_options.enable_autosaves = autosaves_enabled save_settings() func _on_game_options_block_highlight_changed(highlight_enabled: bool) -> void: settings.game_options.enable_block_highlight = highlight_enabled save_settings() func _on_game_options_creative_mode_changed(creative_mode_enabled: bool) -> void: settings.game_options.enable_creative_mode = creative_mode_enabled save_settings() func _on_game_options_held_block_ui_changed(held_block_enabled: bool) -> void: settings.game_options.enable_held_block = held_block_enabled save_settings() func _on_game_options_quickslots_ui_changed(quickslots_enabled: bool) -> void: settings.game_options.enable_quickslots_ui = quickslots_enabled save_settings() func _on_game_options_screenshot_saves_changed(screenshots_enabled: bool) -> void: settings.game_options.enable_save_screenshots = screenshots_enabled save_settings() func _on_game_options_waila_changed(waila_enabled: bool) -> void: settings.game_options.enable_waila = waila_enabled save_settings() #endregion #region Graphics Settings func _on_apply_graphics_settings() -> void: DisplayServer.window_set_mode(settings.graphics.window_mode) get_window().size = settings.graphics.resolution if settings.graphics.vsync: DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_ENABLED) else: DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_DISABLED) save_settings() func _on_graphics_fov_changed(fov: int) -> void: settings.graphics.fov = fov func _on_graphics_resolution_changed(resolution: Vector2i, id: int) -> void: settings.graphics.resolution = resolution settings.graphics.resolution_id = id func _on_graphics_vsync_changed(vsync_enabled: bool) -> void: settings.graphics.vsync = vsync_enabled func _on_graphics_window_mode_changed(id: int) -> void: settings.graphics.window_mode = id as DisplayServer.WindowMode #endregion #region Saving and Loading Settings func load_settings(apply_after_load: bool = true) -> void: if !FileAccess.file_exists(settings_file_path): printerr("Failed to load game settings. File does not exist: ", settings_file_path) return settings = ResourceLoader.load(settings_file_path) if settings == null: printerr("Failed to load game settings. Unknown format? ", settings_file_path) return if apply_after_load: apply_graphics_settings.emit() func save_settings() -> void: if !DirAccess.dir_exists_absolute(save_data_path): DirAccess.make_dir_absolute(save_data_path) var result: int = ResourceSaver.save(settings, settings_file_path) if result != OK: printerr("Failed to save game settings: ", result) #endregion