FActorSpawnParameters params;
params.bAllowDuringConstructionScript = true;
params.bDeferConstruction = false;
params.bNoFail = true;
params.Name = {};
params.ObjectFlags = EObjectFlags::RF_NoFlags;
params.OverrideLevel = nullptr;
params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
params.Template = nullptr;
TSubclassOf< class AStaticMeshActor > character = nullptr;
static ConstructorHelpers::FObjectFinder< UClass > found_something_object(TEXT("/Game/ThirdPersonBP/Blueprints/ThirdPersonCharacter.ThirdPersonCharacter_C"));
character = found_something_object.Object;
const FVector Location = { 0, 20, 0 };
const FRotator Rotation = FRotator(0, 0, 0);
auto something =
GetWorld()->SpawnActor<AStaticMeshActor>
(character
, Location
, Rotation
, params
);
This code does not work. What is wrong?
Is there an error output or just does nothing in engine? Also, for reference, where are you calling this?
I called this from “void AMyActor::BeginPlay()”.
MYActor is located on the viewport in the UE editor.
But this crash on line 12.
there is no erroe message.
even could not to debug mode.
First issue is that you can only use ConstructorHelpers::FObjectFinder in a classes constructor see here:
Have a look through the links on that answer and if you need any more help let me know.
I want to use dynamic BluePrint loading and spawn it.
This is a new code.
But, It returned null at “character = bp->GeneratedClass;”.
What is wrong?
auto *clazz = StaticLoadObject(UObject::StaticClass(), nullptr, TEXT("/Game/ThirdPersonBP/Blueprints/ThirdPersonCharacter"));
if (clazz != NULL) {
GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Green, TEXT("class exists!!!"));
UBlueprint * bp = Cast<UBlueprint>(clazz);
if (bp) {
GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Green, TEXT("bp exists!!!"));
auto character = bp->GeneratedClass;
if (character != NULL) {
FActorSpawnParameters params;
params.bAllowDuringConstructionScript = true;
params.bDeferConstruction = false;
params.Name = { "test char" };
params.ObjectFlags = EObjectFlags::RF_NoFlags;
params.OverrideLevel = nullptr;
params.Owner = this;
params.Template = nullptr;
const FVector Location = { 0, 20, 0 };
const FRotator Rotation = FRotator(0, 0, 0);
auto something =
GetWorld()->SpawnActor<AStaticMeshActor>
(character
, Location
, Rotation
, params
);
if (something) {
GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Green, TEXT("spawn succeed!!!"));
}
else {
GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Red, TEXT("spawn failed!!!"));
}
}
else {
GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Red, TEXT("characer is NULL!!!"));
}
}
else {
GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Green, TEXT("bp does not exist!!!"));
}
}
else {
GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Red, TEXT("cls2 is NULL!!!"));
}
It works.
Succeeded by using “LoadClass” instead of “StaticLoadObject”.