Can't spawn BP Actor instance from C++

I have a blueprint function “Fire” coded in C++, that takes some class derived from the BaseProjectile C++ class, and some vectors and is supposed to the spawn an instance of that derived class at the specified location and return the pointer to that instance for further manipulation. The function is like this:
ThirdPersonCharacter.cpp

ABaseProjectile* AThirdPersonCharacter::Fire(TSubclassOf<class ABaseProjectile> Projectile, const FVector Muzzle, bool Hitscan = false, const FVector OverrideVelocity = FVector::ZeroVector){	
	FVector Velocity, CameraLoc;
	FRotator CameraRot, MuzzleRot;
	GetActorEyesViewPoint(CameraLoc, CameraRot);
	if (OverrideVelocity == FVector::ZeroVector && Hitscan == false) { //if custom velocity vector was not passed and projectile isn't hitscan
		MuzzleRot = CameraRot;
		MuzzleRot.Pitch += 10.0f;
		Velocity = MuzzleRot.Vector(); //Set velocity to align with camera
	}
	else {
		Velocity = OverrideVelocity; //else override
	}
	UWorld* const World = GetWorld();
	if (World) { //if witcher 3 not out yet
		FActorSpawnParameters SpawnParams;
		SpawnParams.Owner = this;
		SpawnParams.Instigator = Instigator;
		if (Projectile) {	
	
			GEngine->AddOnScreenDebugMessage(1, 5.0f, FColor::Blue, typeid(World->SpawnActor<ABaseProjectile>(Projectile->GetClass(), Muzzle, MuzzleRot, SpawnParams)).name());

			
			ABaseProjectile* const SpawnedProjectile = World->SpawnActor<ABaseProjectile>(Projectile->GetClass(), Muzzle, MuzzleRot, SpawnParams);
			if (SpawnedProjectile) {				 
				SpawnedProjectile->InitVelocity(Velocity);
				return SpawnedProjectile;
			}			
		}
		return nullptr;
	}
	else {
		return nullptr;
	}
}

In blueprints the function looks like this:

Playing doesn’t spawn any projectiles and attacking debug text to e.g. display the name of the return value as string says “None”.
You’ll notice the debug string in the code there. What’s interesting is it does output a proper pointer. Not NULL, like I would expect it to if the spawn would have failed:

What is the reason nothing is spawning?

Hi Megakoresh,

Upon first glance, I don’t know exactly why this isn’t working but I’ll be looking into this issue. In the meantime, you can have a look at this tutorial and maybe it can shed some light on the issue: A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums

I’ll get back to you as soon as I have an idea of what the issue may be.

P.S. I like the Witcher 3 comment :slight_smile:

The most effective way to fix something is by telling someone of it - naturally I found a solution to this issue (that I have been trying to fix for 2 days) right after I post it. And it’s not hard - I simply removed the GetClass() from the spawn call so it looks like this:

ABaseProjectile* const SpawnedProjectile = World->SpawnActor<ABaseProjectile>(Projectile, Muzzle, MuzzleRot, SpawnParams);

From a logical point of view I don’t understand why it didn’t work before - SpawnActor requires a pointer to class as first parameter, and Projectile is a pointer of a class type. So I would expect to have to call GetClass() on it to find it’s class to pass to SpawnActor… Especially considering that I tried this particular approach in the past and it didn’t work (however I can attribute that one to this bug). But it works now and it seems to reference the correct BP class too, not the base one.

Wow, tearing through these things isn’t easy. I am off for a fun ride, aren’t I?