saving & loading keybinds! (-=

This commit is contained in:
Harmony Honey 2024-02-05 21:57:26 -05:00
parent 944f043581
commit eacecc0bb8
4 changed files with 41 additions and 5 deletions

View file

@ -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": ""
}

View file

@ -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)

4
src/class/savedict.gd Normal file
View file

@ -0,0 +1,4 @@
extends Resource
class_name SaveDict
export var dict : Dictionary = {}

View file

@ -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):