I am a bit confused on how to check what is wrong with a spawned actor before using it. I believe I have spawned the actor correctly I don’t know what to do other than to check if its NULL. As you can see I’m trying to spawn a new version of the Weapon but it crashes on AttachRootComponent. Anyone got any ideas to help me out?
As a side note: Not sure how people handle these situations since UE4 doesn’t have try and catch blocks nor return codes. How are blueprints catching errors?
void AVRPawn::OnCollisionHand(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult, EHand Hand)
{
AWeapon* Weapon = Cast<AWeapon>(OtherActor);
if (Weapon)
{
// Get Current Weapon and Motion controller for the correct hand
AWeapon* CurrentWeapon = Hand == EHand::Left ? CurrentWeaponLeft : CurrentWeaponRight;
UMotionControllerComponent* MotionController = (Hand == EHand::Left) ? MotionControllerLeft : MotionControllerRight;
// Setup Spawn Parameters
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;
// Spawn another weapon of that type
AWeapon* Spawner = GetWorld()->SpawnActor<AWeapon>(Weapon->GetClass(), SpawnParams);
// Option A: Works Fine but isn't what I want
Weapon->AttachRootComponentTo(MotionController);
if (Spawner && MotionController)
{
// Option B: Crashes
Spawner->AttachRootComponentTo(MotionController);
}
}
}