gtav-ent/GTAV-EnhancedNativeTrainer

Vector subscript out of range

Closed this issue · 13 comments

I got an error using the SelectFromListMenuItem class when selecting the 51th value of the array but using an index between 0 and 50 work good.
assert

Where are you using it? Got a code example?

My code is really similar to the engine power multiplicator option, except I'm using a bigger array with over 50 values.

See lines 51 and 52 for the arrays: https://github.com/gtav-ent/GTAV-EnhancedNativeTrainer/blob/master/EnhancedNativeTrainer/src/features/vehicles.cpp#L52

Your captions array is probably 51 long, and the values 50 long.

Ok, I'll check for that in case I did that kind of little error.

I just checked and that's ain't the problem.

Post up the code.

I forgot an important fact, it's a float array.

I will post the code later since it's scattered and I'm simplifying a way to post it.

Doesn't matter. The indices are all ints.

If you post the code fragments where you initialize your vectors and iterate through them, it should be fairly simple to diagnose.

vehicles.cpp
{

const std::vector<std::string> VEH_ENG_TORQUE_CAPTIONS{ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100" };
const std::vector<float> VEH_ENG_TORQUE_VALUES{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100 };
int engTorqueMultIndex = 0;
bool torqueChanged = true;

void process_veh_menu()
{
    {...}

    listItem = new SelectFromListMenuItem(VEH_ENG_TORQUE_CAPTIONS, onchange_veh_eng_torque_index);
    listItem->wrap = false;
    listItem->caption = "Engine Torque Multiplier";
    listItem->value = engTorqueMultIndex;
    menuItems.push_back(listItem);

    {...}
}

void update_vehicle_features(BOOL bPlayerExists, Ped playerPed)
{
    {...}

    if (bPlayerExists && PED::IS_PED_IN_ANY_VEHICLE(playerPed, true) && (VEH_ENG_POW_VALUES[engTorqueMultIndex] != 1) || torqueChanged) 
    {
        VEHICLE::_SET_VEHICLE_ENGINE_TORQUE_MULTIPLIER(veh, VEH_ENG_TORQUE_VALUES[engTorqueMultIndex]);
        torqueChanged = false;
    }

    {...}
}

int get_current_veh_eng_torque_index()
{
    return engTorqueMultIndex;
}

void onchange_veh_eng_torque_index(int value, SelectFromListMenuItem* source) 
{
    engTorqueMultIndex = value;
    torqueChanged = true;
}

}

vehicles.h
{

int get_current_veh_eng_torque_index();

void onchange_veh_eng_torque_index(int value, SelectFromListMenuItem* source);

}

You have VEH_ENG_POW_VALUES in there.

Also, it's pretty horrible - e.g. generate the values with a loop!

Holy cow, the intellisense is killing me...

For the values, these are not the ones I use, it's look more like this: 1, 1.05f, 1.1f, 1.25f, 1.5f, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 500, 1000

I used 1,2,3... for the example because I needed a better range.

Thanks everyone for the help and sorry for my stupid error...

JFYI it work good...