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?