class_name GameSettings extends RefCounted ## The player's settings, kept in user://settings.cfg (ConfigFile) apart from the save game, and ## applied at start and on every change (SettingsMenu): ## * Audio: the volume of each audio bus (game/resources/audio_buses.tres). ## * Controls: the dead zones of the move stick (the move_* actions) and the aim stick ## (GameConfig.aim_dead_zone). Mouse aim follows the cursor directly, so it has no ## sensitivity to set. ## * Display: fullscreen, VSync, the pixel size (the 3D view renders that many times smaller ## and scales back up with sharp pixels, for ToonLook's pixel-art look; 1 = off), and the 3D ## resolution scale, used when the pixel size is 1 (the HUD stays sharp either way). ## * Gameplay: whether the tips show on the first day and night (Tutorial). ## * Co-op: your player name, and whether your microphone is sent in voice chat. ## * Touch (phones): the interface size, times the scale the screen's size calls for ## (MobilePort.ui_scale; it doesn't change desktop). ## Values missing from the file or out of range fall back to the defaults, so an old or ## hand-edited file never breaks the game. const PATH := "user://settings.cfg" const BUSES: Array[String] = ["Master", "Music", "SFX", "Ambience", "Voice"] const BUS_NAMES := {"Master": "Master volume", "Music": "Music and stingers", "SFX": "Sound effects", "Ambience": "Ambience", "Voice": "Voice chat (your friend)"} const DEFAULT_VOLUME := {"Master": 0.8, "Music": 0.7, "SFX": 0.8, "Ambience": 0.6, "Voice": 1.0} const MAX_PIXEL_SIZE := 8 var volume := DEFAULT_VOLUME.duplicate() var move_dead_zone := 0.2 var aim_dead_zone := 0.3 var fullscreen := false var vsync := true ## Phones (MobilePort) default to no pixel look, at three quarters of the screen's resolution. var pixel_size := 1 if MobilePort.active() else 4 var resolution_scale := 0.75 if MobilePort.active() else 1.0 var show_tips := true ## Co-op: your name for the lobby and the name tag, and whether your microphone is sent. var player_name := "" var microphone := true ## Phones: the Interface size, 0.8-1.3 times the scale the screen calls for (MobilePort.ui_scale). var interface_size := 1.0 ## Phones: the sticks swapped (move on the left: TouchSticks, TouchButtons), the camera's distance ## (FollowCamera, 8-20 m), and a short buzz when the scarecrow is hit. Saved and applied by #41. var swap_sticks := false var camera_distance := FollowCamera.PHONE_DISTANCE var haptics := true static func load_from(path: String = PATH) -> GameSettings: var g := GameSettings.new() var f := ConfigFile.new() if f.load(path) != OK: return g for bus in BUSES: g.volume[bus] = _number(f.get_value("audio", bus.to_lower(), g.volume[bus]), 0.0, 1.0, g.volume[bus]) g.move_dead_zone = _number(f.get_value("controls", "move_dead_zone", g.move_dead_zone), 0.05, 0.9, g.move_dead_zone) g.aim_dead_zone = _number(f.get_value("controls", "aim_dead_zone", g.aim_dead_zone), 0.05, 0.9, g.aim_dead_zone) g.fullscreen = _flag(f.get_value("display", "fullscreen", g.fullscreen), g.fullscreen) g.vsync = _flag(f.get_value("display", "vsync", g.vsync), g.vsync) g.pixel_size = roundi(_number(f.get_value("display", "pixel_size", g.pixel_size), 1.0, MAX_PIXEL_SIZE, g.pixel_size)) g.resolution_scale = _number(f.get_value("display", "resolution_scale", g.resolution_scale), 0.5, 1.0, g.resolution_scale) g.show_tips = _flag(f.get_value("gameplay", "show_tips", g.show_tips), g.show_tips) var n: Variant = f.get_value("coop", "player_name", "") g.player_name = String(n).strip_edges().left(20) if n is String else "" g.microphone = _flag(f.get_value("coop", "microphone", g.microphone), g.microphone) g.interface_size = _number(f.get_value("touch", "interface_size", g.interface_size), 0.8, 1.3, g.interface_size) return g func save_to(path: String = PATH) -> Error: var f := ConfigFile.new() for bus in BUSES: f.set_value("audio", bus.to_lower(), volume.get(bus, DEFAULT_VOLUME[bus])) f.set_value("controls", "move_dead_zone", move_dead_zone) f.set_value("controls", "aim_dead_zone", aim_dead_zone) f.set_value("display", "fullscreen", fullscreen) f.set_value("display", "vsync", vsync) f.set_value("display", "pixel_size", pixel_size) f.set_value("display", "resolution_scale", resolution_scale) f.set_value("gameplay", "show_tips", show_tips) f.set_value("coop", "player_name", player_name) f.set_value("coop", "microphone", microphone) f.set_value("touch", "interface_size", interface_size) return f.save(path) ## Applies everything: bus volumes, dead zones (into config too, when given), the interface ## scale, and the display (skipped without a real display, e.g. headless). func apply(viewport: Viewport, config: GameConfig = null) -> void: for bus in BUSES: var i := AudioServer.get_bus_index(bus) if i < 0: continue var v: float = volume.get(bus, DEFAULT_VOLUME[bus]) AudioServer.set_bus_volume_db(i, linear_to_db(maxf(v, 0.0001))) AudioServer.set_bus_mute(i, v <= 0.001) for action in ["move_left", "move_right", "move_forward", "move_back"]: if InputMap.has_action(action): InputMap.action_set_deadzone(action, move_dead_zone) if config != null: config.aim_dead_zone = aim_dead_zone if viewport != null: apply_pixels(viewport) apply_scale(viewport) if DisplayServer.get_name() == "headless": return var mode := DisplayServer.window_get_mode() var want_full := fullscreen var is_full := mode == DisplayServer.WINDOW_MODE_FULLSCREEN or mode == DisplayServer.WINDOW_MODE_EXCLUSIVE_FULLSCREEN if want_full != is_full: DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN if want_full else DisplayServer.WINDOW_MODE_WINDOWED) DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_ENABLED if vsync else DisplayServer.VSYNC_DISABLED) ## The 3D view's size: pixel_size > 1 renders it that many times smaller with sharp (nearest) ## upscaling and no MSAA, so each art pixel is a clean block; 1 renders it at resolution_scale ## with the project's MSAA. func apply_pixels(viewport: Viewport) -> void: if pixel_size > 1: viewport.scaling_3d_mode = Viewport.SCALING_3D_MODE_NEAREST viewport.scaling_3d_scale = 1.0 / pixel_size viewport.msaa_3d = Viewport.MSAA_DISABLED else: viewport.scaling_3d_mode = Viewport.SCALING_3D_MODE_BILINEAR viewport.scaling_3d_scale = resolution_scale viewport.msaa_3d = ProjectSettings.get_setting("rendering/anti_aliasing/quality/msaa_3d", Viewport.MSAA_DISABLED) ## The interface scale (MobilePort.ui_scale: on a phone, the screen's size times ## interface_size; always 1 on desktop) as the window's content_scale_factor, which scales every ## CanvasLayer (the HUD, the menus, the sticks) and leaves the 3D view alone. func apply_scale(viewport: Viewport) -> void: var window := viewport.get_window() if window != null: window.content_scale_factor = MobilePort.ui_scale(interface_size) static func _number(v: Variant, lo: float, hi: float, fallback: float) -> float: if not (v is float or v is int): return fallback var x := float(v) return clampf(x, lo, hi) if is_finite(x) else fallback static func _flag(v: Variant, fallback: bool) -> bool: return bool(v) if v is bool else fallback