Populate TArray with GetAxisMappings/GetActionMapping?

I am trying to create a keybinds system that automatically removes any input mappings that uses the same input as the new input. In order to do this, I need to create an array holding all keybindings that I can loop through and check for mappings that use the same input key as the mapping I am creating.

I have tried to use the GetActionMappings and GetAxisMappings, but I cant seem to populate my Arrays with them. My code compiles and runs without a hitch, but the Debug Messages only prints once, while each should print at least six times.

Am I doing this correctly? Or have I fundamentally misunderstood how the code is suppossed to be implemented?


TArray<FInputAxisKeyMapping> testArrayAxis;
    TArray<FInputActionKeyMapping> testArrayActions;

    TArray<FInputAxisKeyMapping> &GetAxisMappings(testArrayAxis);
    TArray<FInputActionKeyMapping> &GetActionMappings(testArrayActions);


    for(int i=0; i<=testArrayAxis.Num(); i++)
    {
        GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("Axis Array fills out"));
    }

    for (int i = 0; i <= testArrayActions.Num(); i++)
    {
        GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Action Array fills out"));
    }

Ugh, you have two serious problems with that code.

First, you’re not actually calling the GetAxisMappings()/GetActionMappings() functions. With your syntax you’re declaring local variables with those names, which happen to be references, and initializing them with an empty array.

Second, you’re iterating the arrays incorrectly (and dangerously). If you use an index, always end with < array.Num(), never with <=. If you try to index an array with the number of elements in it, the program will crash, as arrays are zero-based.

You probably want something like this (untested):


UInputSettings* inputSettings = UInputSettings::GetInputSettings();
TArray<FInputAxisKeyMapping> testArrayAxis = inputSettings->GetAxisMappings();
TArray<FInputActionKeyMapping> testArrayActions = inputSettings->GetActionMappings();

And then to range-based loop through the arrays:



for(auto Mapping : testArrayAxis)
{
    GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("Axis Array fills out"));
}

for (auto Mapping : testArrayActions)
{    
    GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Action Array fills out"));
}