How to properly spawn an actor with the class of a pointer?

I have a simple pointer to an actor in my character. I wish to spawn an actor of the same class as this variable, which is called PrimaryWeapon. Here is my attempt:

GetWorld()->SpawnActorGetClass()->StaticClass()>
(PrimaryWeapon->GetClass(), FVector::ZeroVector, FRotator::ZeroRotator);

Unfortunately, this returns the following errors:
‘UWorld::SpawnActor’: no matching overloaded function found.
‘UWorld::SpawnActor’: invalid template argument for ‘T’, type expected.

In light of this, what is the correct way write the code?

	UClass* ClassToSpawn = PtrToActor->GetClass();
	FVector Location = FVector::ZeroVector;
	FRotator Rotation = FRotator::ZeroRotator;
	GetWorld()->SpawnActor(ClassToSpawn, &Location, &Rotation);

Hope it helps.

1 Like