I’m encountering a strange error and I’m having trouble finding anyone else that’s experienced it.
I’m trying to access data stored in UPROPERTY TArrays on my Actors. But some of the TArrays are throwing out of bound errors, and it seems to only be the arrays with the fewer elements.
Here’s the piece of the code I’ve isolated:
for (int i = 0; i < Unit->Rank1Composition.Num(); i++)
{
switch (i)
{
case 1:
if (Unit->Rank1Composition.IsValidIndex(1))
{
TestUnitBackup0.Rank1Composition.Add(TestModel1Backup0);
//BackupTyranidModel(Cast<ATyranidModel>(Unit->Rank1Composition[1]), TestModel1Backup0);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Unit lacks Index[%i]: %s"), i, *Unit->GetActorNameOrLabel());
}
case 2:
if (Unit->Rank1Composition.IsValidIndex(2))
{
TestUnitBackup0.Rank1Composition.Add(TestModel2Backup0);
//BackupTyranidModel(Cast<ATyranidModel>(Unit->Rank1Composition[2]), TestModel2Backup0);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Unit lacks Index[%i]: %s"), i, *Unit->GetActorNameOrLabel());
}
case 3:
if (Unit->Rank1Composition.IsValidIndex(3))
{
TestUnitBackup0.Rank1Composition.Add(TestModel3Backup0);
//BackupTyranidModel(Cast<ATyranidModel>(Unit->Rank1Composition[3]), TestModel3Backup0);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Unit lacks Index[%i]: %s"), i, *Unit->GetActorNameOrLabel());
}
case 4:
TestUnitBackup0.Rank1Composition.Add(TestModel4Backup0);
//BackupTyranidModel(Cast<ATyranidModel>(Unit->Rank1Composition[4]), TestModel4Backup0);
case 5:
TestUnitBackup0.Rank1Composition.Add(TestModel5Backup0);
//BackupTyranidModel(Cast<ATyranidModel>(Unit->Rank1Composition[5]), TestModel5Backup0);
default:
if (Unit->Rank1Composition.IsValidIndex(0))
{
TestUnitBackup0.Rank1Composition.Add(TestModel0Backup0);
//BackupTyranidModel(Cast<ATyranidModel>(Unit->Rank1Composition[0]), TestModel0Backup0);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Unit lacks Index[%i]: %s"), i, *Unit->GetActorNameOrLabel());
}
}
}
As you can see from the code, it shouldn’t trigger if there is no index 0, and then it should only call the switch for index’s less than Rank1Composition.Num(). You can also see that in trying to debug this, I’ve setup UE_LOGs to output the number of elements in the TArray, and warn if it can’t find an index. In the screenshot, you can see these warnings as well as the referenced Actor. They clearly show there are 3 elements in the Array, but also output that there is no index 1 or 2.
And as I said above, this only appears to be for Actors with a small number of elements in their TArray. If there’s 1 element, it’s fine and I don’t get an error. If there’s 5 elements, it’s fine and I don’t get an error. So far It’s only if there are exactly 2 or 3 elements.
Any help would be greatly appreciated!