Saving a User made Input (Converting a string to a InputEvent)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lavaduder
:warning: Old Version Published before Godot 3 was released.

Okay so I have a input set up, where the player can choose the button configuration. However this gets reset when the game is closed, and the player will have to do it again.

I want to save the new input action created by the player. To eliminate this need.

However I’m having trouble, as
A. Saving stores it as a string, Not a InputEvent
B. Loading as a string, fails when converting to an InputMap

Which results in this error
Invalid type in function 'action_add_event' in base 'InputMap'. Cannot convert argument 2 from String to InputEvent.
Here’s the complete code for context:

func save_game(string_file = "user://data.sav"):
var file = File.new()
file.open(string_file,file.WRITE)
var data=[]
#Save Options
var options_data = {}
var op_input_data = {}
for i in InputMap.get_actions():
	var input_actions = InputMap.get_action_list(i)
	op_input_data[str(i)] = input_actions
options_data["Inputs"] = op_input_data
options_data["Level"] = Globals.get("v/current_level")
data.append(options_data)
#Save Game
var game_data = {}
data.append(game_data)
#print(data)
file.store_var(data)
file.close()
func load_game(string_file = "user://data.sav"):
print("Loading...")
var file = File.new()
file.open(string_file,file.READ)
#Load Options
var data = file.get_var()
var options_data = data[0]#To get the array index, 
if options_data.has("Inputs"):#Inputs
	for i in options_data["Inputs"]:
		InputMap.add_action(str(i))
		InputMap.action_add_event(i,i[0])
if options_data.has("Level"): #Current_level, Disable this if testing another level
	var sav_level = options_data["Level"]
	get_tree().change_scene(sav_level)
print(data)
#Load Game WIP
file.close()

I have no idea why enter code here has thrown the formatting off. Let me reassure you the formatting is correct in the Godot Editor, Else I wouldn’t be getting Invalid type in function 'action_add_event' in base 'InputMap'. Cannot convert argument 2 from String to InputEvent.

lavaduder | 2018-01-21 21:05

:bust_in_silhouette: Reply From: Aaron

There’s a demo project that demonstrates configurable persistent input mapping.
In Godot 3.0 under the Templates tab of the project manager you can find it as “Gui Input Mapping Demo”.

For key events, you use InputEventKey.scancode, OS.get_scancode_string, and OS. find_scancode_from_string to convert input events to strings and back.