Download

Is there a simple way to get the Action Name after an InputAction, default only passed the key

I wrote this to get the action name, as I base my character State on the action name. I used the key given by the InputAction Is there a more simple way ?


void URequestActionStateChange::BeginPlay()
{
    Super::BeginPlay();

    SetCurrentActionState(DefaultState);

    GetActionAndKeyArray();
}

//filling the provided TArray<FInputActionKeyMapping> with ActionName and Key value
void URequestActionStateChange::GetActionAndKeyArray()
{
    ActionAndKeyArray.Empty();

    UInputSettings* InputSettings = InputSettings->GetInputSettings();

    //empty array to be filled by iterator
    TArray<FName> ActionNames;

    //filling ActionNames array with action names
    InputSettings->GetActionNames(ActionNames);

    //filling ActionNameAndKey array with ActionName and Key value
    for (size_t i = 0; i < ActionNames.Num(); i++)
    {
        FName ActionName = ActionNames*;
        InputSettings->GetInputSettings()->GetActionMappingByName(ActionName, ActionAndKeyArray);
    }

    return;
}

FString URequestActionStateChange::GetActionNameFromKeyPressed(FKey KeyPressed)
{
    for (FInputActionKeyMapping Mappings : ActionAndKeyArray)
    {
        FString Action = Mappings.ActionName.ToString();
        FKey Key = Mappings.Key;

        if (Key == KeyPressed)
        {
            return Action;
        }
    }

    return FString("Error : Key Not Found in URequestActionStateChange::GetActionNameFromKeyPressed");
}

This?



FName UYourFunctionLibrary::GetActionBoundToKey(const FKey& BoundKey)
{
    const UInputSettings* InputSettings = UInputSettings::StaticClass()->GetDefaultObject<UInputSettings>();

    FName ResultAction = NAME_None;

    for (const FInputActionKeyMapping& ActionMapping : InputSettings->ActionMappings)
    {
        if (ActionMapping.Key == BoundKey)
        {
            ResultAction = ActionMapping.ActionName;
            break;
        }
    }

    if (ResultAction == NAME_None)
    {
        for (const FInputAxisKeyMapping& AxisMapping : InputSettings->AxisMappings)
        {
            if (AxisMapping.Key == BoundKey)
            {
                ResultAction = AxisMapping.AxisName;
                break;
            }
        }
    }

    return ResultAction;
}


Could probably use FindByPredicate instead.
Not sure which would be faster.
shrug