I have attempted to program some code that spawns a bullet above my pawn. However , when I run it , nothing happens. The header file contains:
TSubclassOf <class APlayerPawn> MyBulletBlueprint;
And inside the .cpp file, in the constructer there is:
static ConstructorHelpers::FObjectFinder<UBlueprint> BulletBlueprint(TEXT("Blueprint'/Content/Blueprints/BulletBP.BulletBP'"));
if (BulletBlueprint.Object){
MyBulletBlueprint = (UClass*)BulletBlueprint.Object->GeneratedClass;
}
and the actual code that is meant to spawn the blueprint is:
UWorld* const World = GetWorld();
if (World)
{
FActorSpawnParameters SpawnParams;
SpawnParams.Instigator = this;
World->SpawnActor<APlayerPawn>(MyBulletBlueprint, APlayerPawn::GetActorLocation() + FVector(0.0f,0.0f,BulletGap), APlayerPawn::GetActorRotation(), SpawnParams);
}
What have I done wrong?
SaxonRah
(SaxonRah)
March 9, 2015, 1:11am
2
All thanks goto to Rama.
Paste this into a header file like your pre-compiled header. This will allow you to use it like below.
template <typename ASpawnBP>
FORCEINLINE ASpawnBP* SpawnBP(
UWorld* TheWorld,
UClass* TheBP,
const FVector& Loc,
const FRotator& Rot,
const bool bNoCollisionFail = true,
AActor* Owner = NULL,
APawn* Instigator = NULL
)
{
if (!TheWorld) return NULL;
if (!TheBP) return NULL;
//~~~~~~~~~~~
FActorSpawnParameters SpawnInfo;
SpawnInfo.bNoCollisionFail = bNoCollisionFail;
SpawnInfo.Owner = Owner;
SpawnInfo.Instigator = Instigator;
SpawnInfo.bDeferConstruction = false;
return TheWorld->SpawnActor<ASpawnBP>(TheBP, Loc, Rot, SpawnInfo);
}
This would go in some header like an aactor
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = SpawningBP)
UClass* BPSpawning;
And this would be how you use it
const FVector GenSpawnLoc(0.0f, 0.0f, 0.0f);
const FRotator GenSpawnRot(0.0f, 0.0f, 0.0f);
For instance an AStaticMeshActor
AStaticMeshActor* SpawnedBP = SpawnBP<AStaticMeshActor>(GetWorld(), BPSpawning, GenSpawnLoc, GenSpawnRot);
or an AActor
AActor* SpawnedBP = SpawnBP<AActor>(GetWorld(), BPSpawning, GenSpawnLoc, GenSpawnRot);
How to make it work from BlueprintFunctionLibrary (atleast in C++)?
Rama posted on wiki:
From a Static Library, in an actor class (for use of GetWorld()). SpawnLoc and SpawnRot are calculated by you based on your needs.
AActorBaseClass* NewActor = UFunctionLibrary::SpawnBP(GetWorld(), TheActorBluePrint, SpawnLoc, SpawnRot);
No compile errors, but having function definition in BPFL.h and calling above line of code in some AMyActor.cpp doesn’t spawn anything.