# \[c++\]How to actually spawn actor after async load is complete

**URL:** <https://forums.unrealengine.com/t/c-how-to-actually-spawn-actor-after-async-load-is-complete/404469>\
**Category:** C++\
**Tags:** asynchronous-asset-l, unreal-engine, question, CPP\
**Created:** [September 13, 2017, 11:34am UTC](https://forums.unrealengine.com/t/c-how-to-actually-spawn-actor-after-async-load-is-complete/404469 "2017-09-13T11:34:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noowai](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Noowai](https://forums.unrealengine.com/u/Noowai)\
**Post date:** [September 13, 2017, 11:34am UTC](https://forums.unrealengine.com/t/c-how-to-actually-spawn-actor-after-async-load-is-complete/404469/1 "2017-09-13T11:34:19Z")

</div>

The question is kinda silly, but I fail to figure it out by myself.

I’m using [this tutorial][1].  
Everything seems fine, but I don’t understand where to go from a point when I call method ResolveObject() on FStringAssetReference.  
From this point, in tutorial parts of skeletal mesh are being attached one to other, but what I’m need to do is to load blueprints derived from c++ class AMyGameCharacter. And I don’t get what to do with this Resolved Object.

Before I was spawning blueprints preloaded with ConstructorHelpers::FClassFinder with GetWorld()-\>SpawnActor(UClass\*, Location, Rotation, SpawinInfo), but now, when I try it returns either NULL, or memory access violation.  
I’m guessing it is because whenever I try to get a Blueprint from Resolved Object I get some generic class and not the blueprint object I’ve loaded.

Anyway, simple question is this: how do I spawn object, that I have loaded from DataAsset? I don’ really get what to do, please help!

[1]: [[TUTORIAL] C++ - Runtime Async Load Modular Character (Intermediate) - Community & Industry Discussion - Unreal Engine Forums](https://forums.unrealengine.com/community/community-content-tools-and-tutorials/4924-tutorial-c-runtime-async-load-modular-character-intermediate?5309-TUTORIAL-C-Runtime-Async-Load-Modular-Character-)(Intermediate=

---

<div class="post-metadata">

**Author:** ![Noowai](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Noowai](https://forums.unrealengine.com/u/Noowai)\
**Post date:** [September 14, 2017, 9:53am UTC](https://forums.unrealengine.com/t/c-how-to-actually-spawn-actor-after-async-load-is-complete/404469/2 "2017-09-14T09:53:35Z")

</div>

Ok, I’ve posted this answer in tutorial thread, but just in case copying it here:

After Resolving Object I had to do the following:  
Code:  
( UClass\*)(Cast(BlueprintAssetReference.ResolveObject()))-\>GeneratedClass;  
This gives me desired class, that I then successfully spawn via WorldClass::SpawnActor() method

---

<div class="post-metadata">

**Author:** ![M0xim](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@M0xim](https://forums.unrealengine.com/u/M0xim)\
**Post date:** [December 3, 2017, 10:09pm UTC](https://forums.unrealengine.com/t/c-how-to-actually-spawn-actor-after-async-load-is-complete/404469/3 "2017-12-03T22:09:01Z")

</div>

hey! I am trying exactly the same! But this doesnt work on packaged/standalone! Only in editor  
Any idea?

---

<div class="post-metadata">

**Author:** ![Noowai](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Noowai](https://forums.unrealengine.com/u/Noowai)\
**Post date:** [December 4, 2017, 7:03am UTC](https://forums.unrealengine.com/t/c-how-to-actually-spawn-actor-after-async-load-is-complete/404469/4 "2017-12-04T07:03:27Z")

</div>

No idea yet, sorry mate! I know about this issue, trying to follow few threads here on answerhub:

> **[Event Driven Loading broken / Async Loading Thread broken](https://forums.unrealengine.com/questions/676511/event-driven-loading-broken-async-loading-thread-b.html)**
>
> Greetings. So far I haven't been able to reproduce it in a new project. In 4.16 the game would run with Event Driven Loading enabled, but eventually crash on server travel, in 4.17 it won't even start the game. I've done more testing in relation...

> **[Fatal Crash from async loading and phys materials](https://forums.unrealengine.com/questions/605017/fatal-crash-from-async-loading.html)**
>
> This crash seems to possibly be caused by async loading. It never happend before 4.16 but ingame crashes and fatal errors are now very frequent! This screenshot is from a crashed shipping release build (built from project launcher), in a...

But since it’s not the first priority issue to me, I just put that on hold.  
If you figure that out and have time, please point me in right direction.  
Thanks!

---

<div class="post-metadata">

**Author:** ![Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://forums.unrealengine.com/u/Anonymous_e5d5c0bf7d300728867097ff79906dbe)\
**Post date:** [February 3, 2018, 12:50am UTC](https://forums.unrealengine.com/t/c-how-to-actually-spawn-actor-after-async-load-is-complete/404469/5 "2018-02-03T00:50:19Z")

</div>

Any news? ?

---

<div class="post-metadata">

**Author:** ![Noowai](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Noowai](https://forums.unrealengine.com/u/Noowai)\
**Post date:** [February 3, 2018, 12:17pm UTC](https://forums.unrealengine.com/t/c-how-to-actually-spawn-actor-after-async-load-is-complete/404469/6 "2018-02-03T12:17:44Z")

</div>

No luck yet. I’ve tried couple of things, even StaticLoad not working on packaged builds. ConstructorHelpers::FObjectFinder works, though. But it needs to be in constructor and I don’t like the idea of loading all assets on gamemode creation.  
So I’m thingking of spawning some intermediate Actor, that would use FConstructorHelper in it’s constructor, pulling out string refenrence from gamemode, loading needed blueprint and returning loaded reference to gamemode, so gamemode would spawn it the end. Not sure that it would work though.

---

<div class="post-metadata">

**Author:** ![Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://forums.unrealengine.com/u/Anonymous_e5d5c0bf7d300728867097ff79906dbe)\
**Post date:** [February 3, 2018, 12:24pm UTC](https://forums.unrealengine.com/t/c-how-to-actually-spawn-actor-after-async-load-is-complete/404469/7 "2018-02-03T12:24:39Z")

</div>

I think I’ve managed to do it in the end.  
Will post a code in couple of hours.

---

<div class="post-metadata">

**Author:** ![Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://forums.unrealengine.com/u/Anonymous_e5d5c0bf7d300728867097ff79906dbe)\
**Post date:** [February 3, 2018, 1:59pm UTC](https://forums.unrealengine.com/t/c-how-to-actually-spawn-actor-after-async-load-is-complete/404469/8 "2018-02-03T13:59:37Z")

</div>

So this is how I do it:

I am using 4.16.3 but I think it shall work the same in newer versions besides the fact that “TAssetSubclassOf” was renamed to “TSoftClassPtr” in 4.18.

Since 4.17 is the version marked in question - I will use “TAssetSubclassOf” version in the example below:  
Please refer to [A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums](https://wiki.unrealengine.com/TAssetPtr_and_Asynchronous_Asset_Loading)

TAssetSubclassOf allows us to reference a BlueprintAsset (that inherits some base class) so here is my H file:

```
FStreamableManager AssetLoader;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = AsyncLoad)
TAssetSubclassOf<ABaseBodyPartsActor> m_BodyPartsPtr;

UFUNCTION(BlueprintCallable, Category = "SpawnAsyncLoadedParts")
ABaseBodyPartsActor* SpawnParts(FTransform transform);

UFUNCTION()
void LoadedAsstes();

```

and here is the CPP part 🙂

//BeginPlay = start async load:

```
{

	Super::BeginPlay();

	if (m_BodyPartsPtr != nullptr)
	{
		FStringAssetReference AssetToLoad;
		AssetToLoad = m_BodyPartsPtr.ToStringReference();

		AssetLoader.RequestAsyncLoad(AssetToLoad, FStreamableDelegate::CreateUObject(this, &ACBaseUnitCharacter::LoadedAsstes), 1, true);
	}
...
}

    //Loaded assets callback - for me it is only 1 item so I left it as an empty implementation
    void ACBaseUnitCharacter::LoadedAsstes()
    {
    	if (m_BodyPartsPtr != nullptr)
    	{
    		if (m_BodyPartsPtr.IsValid())
    		{
    		}
    	}
    }
    
    //The spawn itself!
    ABaseBodyPartsActor* ACBaseUnitCharacter::SpawnParts(FTransform transform)
    {
    	if (m_BodyPartsPtr.IsValid() && GetWorld())
    	{
    		return GetWorld()->SpawnActor<ABaseBodyPartsActor>(m_BodyPartsPtr.Get(), transform);
    	}
    	else
    	{
    		return nullptr;
    	}
    }

```

Tested this code on both macOS and Windows10 within PIE and Shipment build - both works fine,

---

<div class="post-metadata">

**Author:** ![Noowai](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Noowai](https://forums.unrealengine.com/u/Noowai)\
**Post date:** [February 4, 2018, 8:25am UTC](https://forums.unrealengine.com/t/c-how-to-actually-spawn-actor-after-async-load-is-complete/404469/9 "2018-02-04T08:25:32Z")

</div>

It worked! Thank you.

---

<div class="post-metadata">

**Author:** ![Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://forums.unrealengine.com/u/Anonymous_e5d5c0bf7d300728867097ff79906dbe)\
**Post date:** [February 4, 2018, 8:23pm UTC](https://forums.unrealengine.com/t/c-how-to-actually-spawn-actor-after-async-load-is-complete/404469/10 "2018-02-04T20:23:58Z")

</div>

My pleasure!

---

<div class="post-metadata">

**Author:** ![Two-faced](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Two-faced](https://forums.unrealengine.com/u/Two-faced)\
**Post date:** [July 4, 2019, 4:12pm UTC](https://forums.unrealengine.com/t/c-how-to-actually-spawn-actor-after-async-load-is-complete/404469/11 "2019-07-04T16:12:31Z")

</div>

nice code btw 😃
