NB. This is for version 5.0.0. UE5 version tags don’t seem to be working.
I came across really strange behavior with animation notify states.
In notify tick, I instruct the function to call a getter in another class to retreive a weapon actor.
h
AHandheldWeaponBase* Weapon = nullptr;
bool bWeaponFound = false;
cpp
if (!bWeaponFound)
{
AAIBaseCharacter* AICharacter = Cast<AAIBaseCharacter>(MeshComp->GetOwner());
// crashes here
Weapon = AICharacter->GetWeapon();
}
if (Weapon)
{
Weapon->Swing(MeshComp);
bWeaponFound = true;
}
As the weapon spawns on BeginPlay, it doesn’t exist when I am setting the notify state in an animation BP, so the getter function retrieves a nullptr if the weapon has not spawned or a pointer to the weapon object if it has:
h file
AHandheldWeaponBase* AttachedWeapon = nullptr;
cpp
BeginPlay()
{
AttachedWeapon = GetWorld()->SpawnActor<AHandheldWeaponBase>(WeaponClass);
}
AHandheldWeaponBase* AAIBaseCharacter::GetWeapon()
{
if (!AttachedWeapon) {return nullptr;}
else {return AttachedWeapon;}
}
Firstly, it is bizarre that this function seems to be running in the animation preview. I have no idea why this would be the case. Secondly, I also have no idea why this would cause Unreal to crash. The getter either explicitly returns an initialised nullptr or the object itself.
There are ways I could work around this but I wonder if anyone can shed some light.
Cheers.