Change Key Mappings

I have the following function to change key bindings:

// just so you know:
APlayerController* MyController = Cast<APlayerController>(Controller);

bool AMyGameCharacter::SetKeyBinding(FName action, FKey key) {
	for (int32 i = 0; i < MyController->PlayerInput->ActionMappings.Num(); i++) {
		if (MyController->PlayerInput->ActionMappings[i].ActionName == action) {
			MyController->PlayerInput->ActionMappings[i].Key = key;
			return true;
		}
	}
	return false;
}

But it doesn’t seem to work.

You might have better luck using RemoveActionMapping() and AddActionMapping(). Theoretically, this should probably also be followed up by a call to SaveConfig() and ForceRebuildingKeyMaps(false), but in my experience so far, I’ve found that the action and axis inputs are not getting saved to the input.ini file in the saved directory. I did find DebugExecBindings getting saved, as part of Engine.PlayerInput, but nothing from AxisConfig or ActionMappings, from Engine.InputSettings.

 bool AMyGameCharacter::SetKeyBinding(FName action, FKey key) {
     for (int32 i = 0; i < MyController->PlayerInput->ActionMappings.Num(); i++) {
         if (MyController->PlayerInput->ActionMappings[i].ActionName == action) {
             FInputActionKeyMapping actionMap =
                 MyController->PlayerInput->ActionMappings[i];
             MyController->PlayerInput->RemoveActionMapping(actionMap);
             actionMap.Key = key;
             MyController->PlayerInput->AddActionMapping(actionMap);
             return true;
         }
     }
     return false;
 }