Understanding Input.ini and DefaultInput.ini (saving and reverting default input settings)

Hi there, I’m trying to implement an input remapping system.

So far I was able to easily just do:

// Code omitted here: Detect which action the user wants to remap, and which key they want to remap to
// ...

// Apply the new mapping
auto InputSettings = UInputSettings::GetInputSettings();
InputSettings->RemoveActionMapping(OldMapping, false);
InputSettings->AddActionMapping(NewMapping, false);
InputSettings->SaveKeyMappings();
InputSettings->ForceRebuildKeymaps();

This works fine, and it does change the button mapping. However it seems to also alter the editor input settings found via ProjectSettings/Input. At first I thought this was override the default input settings… so I was really confused as to how I can implement a “Reset to Defaults” button in the game, which would reset the user mappings to the default ones.

Upon further research, it seems that Unreal reads the input settings in a hierarchical fashion. First reads DefaultInput.ini, then reads Input.ini. So when the game saves input settings via SaveKeyMappings(); its writing to Input.ini and not the default config file. Is this correct?

So with that in mind, simply deleting all mappings from the current input settings, and reloading the config file should apply the defaults, because it will just load DefaultInput.ini and find nothing in Input.ini. Is this correct?

And so this is the code to reset input settings to default (it seems to work):


void UUIOptions::ResetInputDefaults()
{
    // Erase mappings in the current user mapping settings
    {
        auto InputSettings = UInputSettings::GetInputSettings();
        auto AxisMappings = InputSettings->GetAxisMappings();
        for (auto Mapping : AxisMappings)
        {
            InputSettings->RemoveAxisMapping(Mapping, false);
        }

        auto ActionMappings = InputSettings->GetActionMappings();
        for (auto Mapping : ActionMappings)
        {
            InputSettings->RemoveActionMapping(Mapping, false);
        }
        InputSettings->SaveKeyMappings();
    }

    // Reload Input.ini settings which will cause DefaultInput.ini to be applied since its higher up in the config stack
    {
        FConfigCacheIni::LoadGlobalIniFile(GInputIni, TEXT("Input"), nullptr, true, true);
        auto InputSettings = UInputSettings::GetInputSettings();
        InputSettings->ReloadConfig(InputSettings->GetClass(), *GInputIni, UE4::LCPF_PropagateToInstances);
        InputSettings->ForceRebuildKeymaps();
        InputSettings->SaveKeyMappings();
    }
}

Am I understanding everything correctly or am I missing something?

Cheers!
Ali

shout out to this random post, works great, can’t believe this isn’t document/implemented by default…