# SpawnActorDeferred not working as intended?

**URL:** <https://forums.unrealengine.com/t/spawnactordeferred-not-working-as-intended/403332>\
**Category:** Blueprint\
**Tags:** spawn, spawnactor, unreal-engine, UE4, CPP, question\
**Created:** [August 31, 2017, 1:52am UTC](https://forums.unrealengine.com/t/spawnactordeferred-not-working-as-intended/403332 "2017-08-31T01:52:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_7e85bfbf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_7e85bfbf](https://forums.unrealengine.com/u/anonymous_user_7e85bfbf)\
**Post date:** [August 31, 2017, 1:52am UTC](https://forums.unrealengine.com/t/spawnactordeferred-not-working-as-intended/403332/1 "2017-08-31T01:52:10Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Crefossus](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Crefossus](https://forums.unrealengine.com/u/Crefossus)\
**Post date:** [August 31, 2017, 3:22am UTC](https://forums.unrealengine.com/t/spawnactordeferred-not-working-as-intended/403332/2 "2017-08-31T03:22:31Z")

</div>

I think it’s working as intended (although tricky to understand at first).  
“[SpawnActorDeferred](https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Engine/UWorld/SpawnActorDeferred/1/index.html): WILL NOT run **Construction Script of Blueprints** to give caller an opportunity to set parameters beforehand.”

It says blueprints, but you should be able to override OnConstruction in C++ to do what you want. In case of deferred spawning, OnConstruction will not run until you call FinishSpawningActor (follow UGameplayStatics::FinishSpawningActor to see what happens when it’s called).

Also see:

[https://forums.unrealengine.com/development-discussion/c-gameplay-programming/12970-beginplay-vs-constructor?27461-BeginPlay-VS-Constructor=](https://forums.unrealengine.com/development-discussion/c-gameplay-programming/12970-beginplay-vs-constructor?27461-BeginPlay-VS-Constructor=)

> **[What is CDO?](https://forums.unrealengine.com/questions/191353/what-is-cdo.html)**
>
> The whole studio, for many months, is trying to figure out what is a purpose of Class Default Object (CDO)? In the documentation I can see: This structure initializes the properties from an archetype or class default object (CDO) after the ...

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_7e85bfbf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_7e85bfbf](https://forums.unrealengine.com/u/anonymous_user_7e85bfbf)\
**Post date:** [August 31, 2017, 3:50am UTC](https://forums.unrealengine.com/t/spawnactordeferred-not-working-as-intended/403332/3 "2017-08-31T03:50:48Z")

</div>

Thanks for the clarification!  
I will try to move everything in constructor to OnConstruction and see what happens.
