This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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

in Engine by (695 points)

2 Answers

0 votes

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)
by (50 points)
0 votes

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)

by (40 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.