In one word SpawnActorDeferred is not deferring the constructor of actors.
In the constructor of AMyActor, one section of code determines which static mesh MyActor should load according to class member (say) int MeshStyle, for example
AMyActor::AMyActor()
{
// Other stuff
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshAsset0(TEXT("Reference0")
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshAsset1(TEXT("Reference1")
// I omitted checking whether they Succeeded() nor not here
if (MeshStyle == 0)
{
StaticMeshComponent->SetStaticMesh(StaticMeshAsset0.Object)
}
else
{
StaticMeshComponent->SetStaticMesh(StaticMeshAsset1.Object)
}
// Other stuff
In the handler class , I have:
AMyActor* AHandler::SpawnMyActor(FTransform Transform, int MeshStyle = 0)
{
AMyActor* MyActor = GetWorld()->SpawnActorDeferred<AMyActor>(AMyActor::StaticClass(), Transform, this, nullptr, ESpawnActorCollisionHandlingMethod::AlwaysSpawn);
if(MyActor)
{
MyActor->MeshStyle = MeshStyle;
UGameplayStatics::FinishSpawningActor(MyActor, Transform);
}
return MyActor;
But no matter what MeshStyle I pass into this function, I always get the static mesh corresponding to MeshStyle = 0.
I did some UE_LOG debugging and found out that MyActor::MyActor is called before FinishSpawningActor, and this is not working as advertised(SpawnActorDeferred should postpone the constructor of the Actor).
I don’t know why or what to do!