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.new()
	fileX.open(myPath,File.WRITE)
	for event in InputMap.get_action_list(ActionX.text):
		if event is InputEventKey:
			var toStore ={
				"type":"InputEventKey",
				"device":event.device,
				"alt":event.alt,
				"command":event.command,
				"control":event.control,
				"meta":event.meta,
				"shift":event.shift,
				"echo":event.echo,
				"pressed":event.pressed,
				"scancode":event.scancode,
				"unicode":event.unicode
			}
			fileX.store_line(to_json(toStore))
	fileX.close()

func load_from_disk():
	var fileX = File.new()
	if not fileX.file_exists(myPath):
		return
	InputMap.action_erase_events(ActionX.text)
	fileX.open(myPath,File.READ)
	while fileX.get_position() < fileX.get_len():
		var toSet = parse_json(fileX.get_line())
		if toSet["type"] == "InputEventKey":
			var event = InputEventKey.new()
			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"]
			InputMap.action_add_event(ActionX.text,event)
			KeyChangeButton.text = OS.get_scancode_string(event.scancode)
	fileX.close()
: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 ControlsManager.gd 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:
  load_controls()

func save_controls() -> void:
  var actions := InputMap.get_actions()
  var data := ControlsData.new()
  for action in actions:
    if action.begins_with("editor_") or action.begins_with("ui_"):
      continue
    data.controls[action] = InputMap.action_get_events(action)
  var error := ResourceSaver.save(data, 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)
    return

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

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

  changed.emit()

This is a neat solution, but how did you implement replace_action_mapping? In my case I support keyboard as well as joypad, so I would need to make sure that the InputEvents that are loaded from disk only replace the events as appropriate.

Rather than do that, I’m just clearing out all events associated with the action, and then reloading them completely from whatever is on disk.

Edit: and for the record, this approach (I’ll put my for loop here) seems to work fine

	for action: String in data.controls.keys():
		InputMap.action_erase_events(action)
		for event: InputEvent in data.controls[action]:
			InputMap.action_add_event(action, event)