Spawn Actor crashing

Hi there,

I am trying to spawn an actor using C++ but get an error thrown in the statement spawn actor.

AbilityWeapon is TSubClassof ARA_Weapon.

ARA_Weapon* CastedWeapon1 = Cast<ARA_Weapon>(AbilityWeapon->GetDefaultObject());
FTransform SpawnTransform;
const FVector Origin = FVector::ZeroVector;
SpawnTransform.SetLocation(Origin);
const FRotator Rot = FRotator::ZeroRotator;
SpawnTransform.SetRotation(Rot.Quaternion());
ARA_Weapon* SpawnedActor1 = GetWorld()->SpawnActorDeferred<ARA_Weapon>(CastedWeapon1->GetClass(), SpawnTransform, GetOwningActorFromActorInfo(), Cast(GetOwningActorFromActorInfo())); //Error here

Hi,

May you be great!

Cast is template function, so you must give a type when using it, which meas the right syntax is Cast<APawn>(GetOwningActorFromActorInfo()), you also can avoid the first Cast, simple add your AbilityWeapon in your SpawnActorDeferred directly once it is a TSubClassof.

If I can give you an advice, before use any Pointer, always ensure if it it valid, it’s a good practice, for instance:

if (AbilityWeapon)
{
	const FTransform SpawnTransform = FTransform(FRotator::ZeroRotator, FVector::ZeroVector);

	if (APawn* Instigator = Cast<APawn>(GetOwningActorFromActorInfo()))
	{
		ARA_Weapon* SpawnedActor = GetWorld()->SpawnActorDeferred<ARA_Weapon>
		(
			AbilityWeapon,
			SpawnTransform,
			GetOwningActorFromActorInfo(),
			Instigator,
			ESpawnActorCollisionHandlingMethod::AlwaysSpawn
		);

		if (SpawnedActor)
		{
			// Set your properties ... 

			// Finally
			UGameplayStatics::FinishSpawningActor(SpawnedActor, SpawnTransform);
		}
	}
}

May that help you, Bye!