save management

This commit is contained in:
2025-10-14 20:19:29 +02:00
parent 21a25c07a8
commit ea63279633
9 changed files with 74 additions and 10 deletions

View File

@@ -0,0 +1 @@
extends Node

View File

@@ -0,0 +1 @@
uid://b08q3lqbuoolb

50
scripts/autoloads/save.gd Normal file
View File

@@ -0,0 +1,50 @@
class_name SaveManager
extends Node
var file: ConfigFile = ConfigFile.new()
var save_path: String = "user://save.binary"
func get_section_name(res: Resource) -> String:
return res.resource_name if res.resource_name != "" else res.get_class()
func save(res: Resource) -> bool:
if not is_instance_valid(res):
push_error("cannot save: invalid resource")
return false
var section_name: String = get_section_name(res)
if FileAccess.file_exists(save_path) and file.load(save_path) != OK:
printerr("Save file corrupted!")
return false
file.set_value("meta", "savedate", Time.get_datetime_string_from_system())
for property in res.get_property_list():
if property.usage & PROPERTY_USAGE_STORAGE:
var v_name: String = property.name
file.set_value(section_name, v_name, res.get(v_name))
return file.save(save_path) == OK
func load(res: Resource) -> bool:
if not FileAccess.file_exists(save_path):
return true
if file.load(save_path) != OK:
printerr("Save file corrupted!")
return false
var section_name: String = get_section_name(res)
var props := []
for p in res.get_property_list():
if p.usage & PROPERTY_USAGE_STORAGE:
props.append(p.name)
for key in file.get_section_keys(section_name):
if key in props:
res.set(key, file.get_value(section_name, key))
return true
func delete() -> void:
file.clear()
file.save(save_path)

View File

@@ -0,0 +1 @@
uid://dfgqnmfb710g8

View File

@@ -1,10 +1,15 @@
extends Node
@onready var fullscreen: bool = ProjectSettings.get_setting("display/window/size/mode") == Window.MODE_EXCLUSIVE_FULLSCREEN | Window.MODE_FULLSCREEN
var config: Config = Config.new()
func _ready() -> void:
Save.load(config)
toggle_fullscreen()
func _process(_delta: float) -> void:
if Input.is_action_just_pressed("fullscreen"):
fullscreen = !fullscreen
config.fullscreen = !config.fullscreen
Save.save(config)
toggle_fullscreen()
func _notification(what: int) -> void:
@@ -18,7 +23,7 @@ func _unhandled_input(event: InputEvent) -> void:
Input.mouse_mode = Input.MOUSE_MODE_HIDDEN
func toggle_fullscreen() -> void:
if fullscreen:
if config.fullscreen:
if OS.get_name() == "Windows":
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_EXCLUSIVE_FULLSCREEN)
else: