I have a function which returns a random UAnimMontage from an array. The code is scuffed, but I’m not sure how else to implement it.
if (AttackDirection == EWeaponAttackDirection::AD_InAir)
{
const uint8 RandomInt = FMath::RandRange(0, InAirAnimations.Num());
return InAirAnimations[RandomInt];
}
if (AttackDirection == EWeaponAttackDirection::AD_Down)
{
const uint8 RandomInt = FMath::RandRange(0, DownAnimations.Num());
return DownAnimations[RandomInt];
}
else if (AttackDirection == EWeaponAttackDirection::AD_Up) //Up animations and thrust animations are one and the same
{
const uint8 RandomInt = FMath::RandRange(0, ThrustAnimations.Num());
return ThrustAnimations[RandomInt];
}
else if (AttackDirection == EWeaponAttackDirection::AD_Left)
{
const uint8 RandomInt = FMath::RandRange(0, LeftAnimations.Num());
return LeftAnimations[RandomInt];
}
else if (AttackDirection == EWeaponAttackDirection::AD_UpLeft)
{
const uint8 RandomInt = FMath::RandRange(0, TopLeftAnimations.Num());
return TopLeftAnimations[RandomInt];
}
else if (AttackDirection == EWeaponAttackDirection::AD_BottomLeft)
{
const uint8 RandomInt = FMath::RandRange(0, BottomLeftAnimations.Num());
return BottomLeftAnimations[RandomInt];
}
else if (AttackDirection == EWeaponAttackDirection::AD_Right)
{
const uint8 RandomInt = FMath::RandRange(0, RightAnimations.Num());
return RightAnimations[RandomInt];
}
else if (AttackDirection == EWeaponAttackDirection::AD_BottomRight)
{
const uint8 RandomInt = FMath::RandRange(0, BottomRightAnimations.Num());
return BottomRightAnimations[RandomInt];
}
else if (AttackDirection == EWeaponAttackDirection::AD_UpRight)
{
const uint8 RandomInt = FMath::RandRange(0, TopRightAnimations.Num());
return TopRightAnimations[RandomInt];
}
return ThrustAnimations[0];
This is called by another function:
if ((ComboCount >= 1) && (CurrentAttackDirection == PreviousAttackDirection))
{
//Do spin animations here, currently it is still regular animations
if (UAnimMontage* SpinAnimation = GetRandomMontage(CurrentAttackDirection))
{
return SpinAnimation;
}
}
if (UAnimMontage* RegularAnimation = GetRandomMontage(CurrentAttackDirection))
{
return RegularAnimation;
}
return ThrustAnimations[0];
The latter function is executed by the character when they melee, and I play the anim montage in blueprints (Specifically within a GAS Ability). In the latter function, if I delete the if statements and only return ThrustAnimations[0], there is no crash and it successfully plays the animation. The array it throws is “appError called: Assertion failed: (Index >= 0) & (Index < ArrayNum) [File:F:\Unreal Engine\Installs\UE_5.1\Engine\Source\Runtime\Core\Public\Containers\Array.h] [Line: 763]
Array index out of bounds: 1 from an array of size 1”. I thought that it may be that the array index is of size one, meaning that index 1 would be invalid, and it should instead be index 0. So, I decided to subtract 1 from the random integer mentioned in the function which is at the top of this page. Then it throws the same error, but instead it is at index -1 of array of size 1.