How to serialize input map

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JulioYagami

How could I save/load input map to/from the disk to enable player to setup his own controls?

:bust_in_silhouette: Reply From: hazlin

Hey there, this is the first thing that comes up when I googled this topic, so here is a reply. Basically, you need to save all the important properties, for each event associated with an action, then when loading, create the object, set the properties, then add it back to the action map… if this was python I’d just pickle the object, but it isn’t, so here we are.

#--- Persistence
# example myPath = "user://settings_control_action_"+ActionX.text+".json"
func save_to_disk():
	var fileX =,File.WRITE)
	for event in InputMap.get_action_list(ActionX.text):
		if event is InputEventKey:
			var toStore ={

func load_from_disk():
	var fileX =
	if not fileX.file_exists(myPath):
	while fileX.get_position() < fileX.get_len():
		var toSet = parse_json(fileX.get_line())
		if toSet["type"] == "InputEventKey":
			var event =
			event.device = toSet["device"]
			event.alt = toSet["alt"]
			event.command = toSet["command"]
			event.control = toSet["control"]
			event.meta = toSet["meta"]
			event.shift = toSet["shift"]
			event.echo = toSet["echo"]
			event.pressed = toSet["pressed"]
			event.scancode = toSet["scancode"]
			event.unicode = toSet["unicode"]
			KeyChangeButton.text = OS.get_scancode_string(event.scancode)
:bust_in_silhouette: Reply From: LeMilonkh

I know it’s been a while since this has been asked but here is my solution:

Create a custom resource to save the control data in. This allows you to easily serialize it using ResourceSaver.

class_name ControlsData extends Resource
@export var controls: Dictionary = {}

Then put the following code in a file called and add it as an autoload singleton in the project settings:

extends Node

signal changed

const CONTROLS_SAVE_PATH := "user://controls.tres"

func _ready() -> void:

func save_controls() -> void:
  var actions := InputMap.get_actions()
  var data :=
  for action in actions:
    if action.begins_with("editor_") or action.begins_with("ui_"):
    data.controls[action] = InputMap.action_get_events(action)
  var error :=, CONTROLS_SAVE_PATH)
  if error != OK:
    printerr("Failed to save controls! Error: ", error_string(error))

func load_controls() -> void:
  if not ResourceLoader.exists(CONTROLS_SAVE_PATH, &"ControlsData"):
    printerr("No saved controls data in ", CONTROLS_SAVE_PATH)

  var data: ControlsData = ResourceLoader.load(CONTROLS_SAVE_PATH, &"ControlsData")
  if not is_instance_valid(data):
    printerr("Failed to load controls!")

  for action in data.controls.keys():
    for event in data.controls[action]:
      replace_action_mapping(action, event)