fixed saving & added first basic save creation menu
This commit is contained in:
@@ -4,16 +4,11 @@ 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:
|
||||
func save(res: SaveFile) -> 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
|
||||
@@ -23,26 +18,25 @@ func save(res: Resource) -> bool:
|
||||
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))
|
||||
file.set_value(res.save_name, v_name, res.get(v_name))
|
||||
|
||||
return file.save(save_path) == OK
|
||||
|
||||
func load(res: Resource) -> bool:
|
||||
func load(res: SaveFile) -> 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):
|
||||
for key in file.get_section_keys(res.save_name):
|
||||
if key in props:
|
||||
res.set(key, file.get_value(section_name, key))
|
||||
res.set(key, file.get_value(res.save_name, key))
|
||||
return true
|
||||
|
||||
func delete() -> void:
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
extends Node
|
||||
|
||||
var config: Config = Config.new()
|
||||
var config: Config = Config.new("config")
|
||||
|
||||
func _ready() -> void:
|
||||
Save.load(config)
|
||||
config.load()
|
||||
toggle_fullscreen()
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
if Input.is_action_just_pressed("fullscreen"):
|
||||
config.fullscreen = !config.fullscreen
|
||||
Save.save(config)
|
||||
config.save()
|
||||
toggle_fullscreen()
|
||||
|
||||
func _notification(what: int) -> void:
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
extends Control
|
||||
|
||||
|
||||
@onready var tab_container: TabContainer = $TabContainer
|
||||
|
||||
func _on_start_button_pressed() -> void:
|
||||
pass # Replace with function body.
|
||||
tab_container.current_tab = 1
|
||||
|
||||
func _on_profile_creator_exit() -> void:
|
||||
tab_container.current_tab = 0
|
||||
|
||||
@@ -1 +1,45 @@
|
||||
extends Control
|
||||
|
||||
signal exit
|
||||
|
||||
var profile: Profile = Profile.new("profile")
|
||||
@onready var next: Button = $VBoxContainer/HBoxContainer/Next
|
||||
@onready var line_edit: LineEdit = $VBoxContainer/LineEdit
|
||||
|
||||
@onready var max_index: int = profile.named_things.keys().size() - 1
|
||||
var index: int = 0
|
||||
|
||||
func _ready() -> void:
|
||||
next.disabled = index == max_index
|
||||
|
||||
func _on_back_button_pressed() -> void:
|
||||
if index == 0:
|
||||
exit.emit()
|
||||
else:
|
||||
index -= 1
|
||||
update_name()
|
||||
|
||||
func _on_next_pressed() -> void:
|
||||
index += 1
|
||||
update_name()
|
||||
|
||||
func update_name() -> void:
|
||||
if index == max_index:
|
||||
next.text = "Finish"
|
||||
elif index > max_index:
|
||||
profile.save()
|
||||
index = max_index
|
||||
return
|
||||
else:
|
||||
next.text = "Next"
|
||||
|
||||
var labels: Dictionary = {
|
||||
"player_name": "Name him",
|
||||
"villain_friend": "Name him too",
|
||||
"stupid_friend": "And also name him",
|
||||
"favourite_candy": "Favourite candy?"
|
||||
}
|
||||
|
||||
var label: String = labels[profile.named_things.keys()[index]]
|
||||
line_edit.placeholder_text = label
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class_name Config
|
||||
extends Resource
|
||||
extends SaveFile
|
||||
|
||||
@export var fullscreen: bool = ProjectSettings.get_setting("display/window/size/mode") == Window.MODE_EXCLUSIVE_FULLSCREEN | Window.MODE_FULLSCREEN
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
class_name Profile
|
||||
extends Resource
|
||||
extends SaveFile
|
||||
|
||||
@export var your_name: String = "Joel"
|
||||
@export var villain_friend_name: String = "Josh"
|
||||
@export var stupid_friend_name: String = "Kevin"
|
||||
@export var named_things: Dictionary[StringName, String] = {
|
||||
"player_name": "Joel",
|
||||
"villain_friend": "Josh",
|
||||
"stupid_friend": "Kevin",
|
||||
"favourite_candy": "Chocolate"
|
||||
}
|
||||
|
||||
14
scripts/resources/save_file.gd
Normal file
14
scripts/resources/save_file.gd
Normal file
@@ -0,0 +1,14 @@
|
||||
class_name SaveFile
|
||||
extends Resource
|
||||
|
||||
@export var save_name = "Save"
|
||||
|
||||
func _init(p_save_name = "Save") -> void:
|
||||
save_name = p_save_name
|
||||
|
||||
func save() -> void: Save.save(self)
|
||||
func load() -> void: Save.load(self)
|
||||
|
||||
func reset() -> void:
|
||||
Save.file.erase_section(save_name)
|
||||
Save.file.save(Save.save_path)
|
||||
1
scripts/resources/save_file.gd.uid
Normal file
1
scripts/resources/save_file.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dru7lnuaadb87
|
||||
Reference in New Issue
Block a user