Hi everyone,
So I am designing a relatively (heh) simple input remapping menu - using C++ as the base for the widgets. I am doing this in Unreal 5.1
My goal is to:
- Change the Key Value
- To keep the FEnhancedActionKeyMapping the same (e.g., modifiers, PlayerMappableOptions, bIsPlayerMappable)
- To have these changes persist across game instances*
- I realize this is a limitation of the C++ version, as noted in another post/bug report. (Though interestingly I could get it to work in blueprint).
Basically, I want to retain the entire FEnhancedActionKeyMapping of the old input (minus the Fkey) while chaning the FKey out for the new input.
Here is where I am stuck:
- If I use KeyMap I lose all the data.
- If I use my work around, I can only change one input value once per instance of the menu being opened
- If I use my work around, I cannot revert my inputs to their default state (using a second Input Mapping Context)
Here are some more
Now, in blueprint I could get this working:
Copying the old FEnhancedActionKeyMapping into the new, and using the InputChord to set the FKey in the new Mapping. I am able to do this in C++ too. (note I am using UInputKeySelector to get the InpurtChord).
void UIndividualKeyMap::SetPrimaryInput(FInputChord InputChord)
{
// bunch of related code (literally a few checks to make sure that the user is not replacing the FKey with the same value, and some other stuff related to checking if it is the primary or secondary input).
NewPrimaryMap = CurrentPrimaryMap;
NewPrimaryMap.Key = InputChord.Key;
}
Then in the blueprint version I unmap the old map (“CurrentPrimaryMap”) and instead of using MapKey I use AddUnique().
Now in the C++ version, UInputMappingContext::GetMappings() returns a const ref, meaning I cannot access the AddUnique() method.
If I use MapKey, I lose the modifiers (negate, swizel, etc.) and the mappable options - all of which I need.
My alternative approach, was to find the mapping and just change the FKey to the new value, which kinda of work:
void UIndividualKeyMap::SetPrimaryInput(FInputChord InputChord)
{
NewPrimaryMap.Key = InputChord.Key;
size_t i = TargetContext->GetMappings().Find(CurrentPrimaryMap);
TargetContext->GetMapping(i).Key = NewPrimaryMap.Key;
UEnhancedInputLibrary::RequestRebuildControlMappingsUsingContext(TargetContext)
}
This had very limited success. In that I could update the binding of a particular value only once per instance of the menu being opened. If I clicked the Input Key Selector again, I would be able to type in a new value but it would not take.
Likewise, when I try to revert the changes (I am using two InputContextMaps - one that is the default and stays default, and the other that can be edited).
for (FEnhancedActionKeyMapping CMap : Context.MappingContext->GetMappings())
{
for (FEnhancedActionKeyMapping OMap : Context.BackupContext->GetMappings())
{
CMap.Key = OMap.Key;
}
UEnhancedInputLibrary::RequestRebuildControlMappingsUsingContext(Context.MappingContext)
}
Hopefully I’ve given enough detail and someone knows how to change (and revert) context maps while keeping all details minus the FKey.
Best wishes!
(And I hope that you have a wonderful day!)