Changing the controls in-game

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

So I’ve been fiddling with this, but I just cannot figure it out.
I want to do the typical options menu for changing controls: select an action from the list (for example ‘JUMP’), a ‘please press the key’ pop-up appears (or not, doesn’t matter) and the next key you press gets saved as the input for that action. Could someone provide me with a snippet of example code or just explain thoroughly how to do this (as in just catching the input event part)?

Also, I’d like to save this configuration to a file and be able to load it later on. Again, I’m not quite sure what to save here, especially that I’d like this to also support gamepads.

:bust_in_silhouette: Reply From: Gold Card

Godot’s inputs usually use the input map. there are other ways to use inputs but the input map is the best. here is a tutorial on the input map:

Yes, my point was how to change the input map in-code, as in read the raw InputEvent and set it as the event which triggers InputMap action.

I kinda figured it out, though I do have some other related issues I’m working on. Might post a tutorial on the forum when I’m done.

Elkondo | 2020-01-25 09:48

:bust_in_silhouette: Reply From: johnlambe

For anyone else, this is done using the InputMap class.
Here’s an example in C# (you can do the same in GDScript):

  // create the "jump" action:

  // map the 'A' key to the "jump" action:
  var e = new InputEventKey();
  e.Set("scancode", KeyList.A);
  InputMap.ActionAddEvent("jump", e);