From eacecc0bb8cdce4525786d252e0608e0695cf009 Mon Sep 17 00:00:00 2001 From: Harmony Honey Date: Mon, 5 Feb 2024 21:57:26 -0500 Subject: [PATCH] saving & loading keybinds! (-= --- project.godot | 6 ++++++ src/autoload/Shared.gd | 23 +++++++++++++++++++++++ src/class/savedict.gd | 4 ++++ src/menu/options/KeyMenu.gd | 13 ++++++++----- 4 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 src/class/savedict.gd diff --git a/project.godot b/project.godot index 5ad3526..2ff6fb2 100644 --- a/project.godot +++ b/project.godot @@ -64,6 +64,11 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://src/actor/Player.gd" }, { +"base": "Resource", +"class": "SaveDict", +"language": "GDScript", +"path": "res://src/class/savedict.gd" +}, { "base": "Actor", "class": "Switch", "language": "GDScript", @@ -86,6 +91,7 @@ _global_script_class_icons={ "Menu": "", "Passthrough": "", "Player": "", +"SaveDict": "", "Switch": "", "SwitchBlock": "" } diff --git a/src/autoload/Shared.gd b/src/autoload/Shared.gd index 093ed2f..9dc9260 100644 --- a/src/autoload/Shared.gd +++ b/src/autoload/Shared.gd @@ -29,6 +29,7 @@ var last_slot = -1 var save_maps := {} var save_path := "user://save/" var save_filename := "box.save" +var keys_path := "keys.tres" var scene_dict := {} export var is_scene_dict_refresh := false setget set_is_scene_dict_refresh var replays := [{}, {}, {}] @@ -98,6 +99,8 @@ func _ready(): dir.make_dir(s) load_slots() + KeyMenu.default_keys() + load_keys() Wipe.connect("finish", self, "wipe_finish") @@ -262,6 +265,25 @@ func save(): save_file(save_path + str(save_slot) + "/" + save_filename, JSON.print(data, "\t")) +func save_keys(path := keys_path): + var s_keys = SaveDict.new() + for a in InputMap.get_actions(): + s_keys.dict[a] = InputMap.get_action_list(a) + + ResourceSaver.save(save_path + path, s_keys) + +func load_keys(path := keys_path): + if !ResourceLoader.exists(save_path + path): return + var r = load(save_path + path) + + if r is SaveDict: + for a in r.dict.keys(): + if InputMap.has_action(a): + InputMap.action_erase_events(a) + + for e in r.dict[a]: + InputMap.action_add_event(a, e) + func load_slots(): for i in 3: load_save(i) @@ -281,6 +303,7 @@ func delete_slot(_slot := save_slot): func save_replays(arg := replay_map, _slot := save_slot): save_file(save_path + str(_slot) + "/" + arg + ".save", JSON.print(replays[save_slot][arg], "\t")) + func load_save(_slot = save_slot, is_reload := false): save_slot = clamp(_slot, 0, 2) diff --git a/src/class/savedict.gd b/src/class/savedict.gd new file mode 100644 index 0000000..7583cc9 --- /dev/null +++ b/src/class/savedict.gd @@ -0,0 +1,4 @@ +extends Resource +class_name SaveDict + +export var dict : Dictionary = {} diff --git a/src/menu/options/KeyMenu.gd b/src/menu/options/KeyMenu.gd index 922890c..f0237e6 100644 --- a/src/menu/options/KeyMenu.gd +++ b/src/menu/options/KeyMenu.gd @@ -1,6 +1,6 @@ extends Menu -onready var default_keys := {} +var default_keys := {} export var is_gamepad := false @@ -46,10 +46,6 @@ func _ready(): popup.visible = false - # get default key binds - for i in InputMap.get_actions(): - default_keys[i] = InputMap.get_action_list(i) - # setup list list = [] for i in keys_action.keys(): @@ -124,6 +120,8 @@ func on_open(): header_node.text = "gamepad" if is_gamepad else "keyboard" for i in actions.size(): fill_row(list[i], actions[i]) + else: + Shared.save_keys() func fill_row(row, action): var a = get_action_list_is_type(action) @@ -174,6 +172,11 @@ func clear_action(_cursor := cursor): fill_row(list[_cursor], actions[_cursor]) +func default_keys(): + # get default key binds + for i in InputMap.get_actions(): + default_keys[i] = InputMap.get_action_list(i) + func reset_to_defaults(): for action in InputMap.get_actions(): for event in InputMap.get_action_list(action):