Skip to content

Commit cd72b05

Browse files
authored
Merge pull request #739 from voylin/FixGuiInputMappingPersistenceKeyMaps3.x
[3.x] GUI Input Mapping Demo Persistent Key Mapping- Fixes #629
2 parents 0f84837 + 69758f2 commit cd72b05

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

gui/input_mapping/ActionRemapButton.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ func _unhandled_key_input(event):
2525

2626

2727
func remap_action_to(event):
28+
# We first change the event in this game instance.
2829
InputMap.action_erase_events(action)
2930
InputMap.action_add_event(action, event)
31+
# And then save it to the keymaps file
32+
KeyPersistence.keymaps[action] = event
33+
KeyPersistence.save_keymap()
3034
text = "%s Key" % event.as_text()
3135

3236

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# This is an autoload (singleton) which will save
2+
# the key maps in a simple way through a dictionary.
3+
extends Node
4+
5+
const keymaps_path = "user://keymaps.dat"
6+
var keymaps: Dictionary
7+
8+
9+
func _ready() -> void:
10+
# First we create the keymap dictionary on startup with all
11+
# the keymap actions we have.
12+
for action in InputMap.get_actions():
13+
keymaps[action] = InputMap.get_action_list(action)[0]
14+
load_keymap()
15+
16+
17+
func load_keymap() -> void:
18+
var file := File.new()
19+
if not file.file_exists(keymaps_path):
20+
save_keymap() # There is no save file yet, so let's create one.
21+
return
22+
#warning-ignore:return_value_discarded
23+
file.open(keymaps_path, File.READ)
24+
var temp_keymap: Dictionary = file.get_var(true)
25+
file.close()
26+
# We don't just replace the keymaps dictionary, because if you
27+
# updated your game and removed/added keymaps, the data of this
28+
# save file may have invalid actions. So we check one by one to
29+
# make sure that the keymap dictionary really has all current actions.
30+
for action in keymaps.keys():
31+
if temp_keymap.has(action):
32+
keymaps[action] = temp_keymap[action]
33+
# Whilst setting the keymap dictionary, we also set the
34+
# correct InputMap event
35+
InputMap.action_erase_events(action)
36+
InputMap.action_add_event(action, keymaps[action])
37+
38+
39+
func save_keymap() -> void:
40+
# For saving the keymap, we just save the entire dictionary as a var.
41+
var file := File.new()
42+
#warning-ignore:return_value_discarded
43+
file.open(keymaps_path, File.WRITE)
44+
file.store_var(keymaps, true)
45+
file.close()

gui/input_mapping/project.godot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ config/description="A demo showing how to build an input key remapping screen.
1919
run/main_scene="res://InputRemapMenu.tscn"
2020
config/icon="res://icon.png"
2121

22+
[autoload]
23+
24+
KeyPersistence="*res://KeyPersistence.gd"
25+
2226
[display]
2327

2428
window/size/width=640

0 commit comments

Comments
 (0)