# Spawn derived class at runtime (blueprint derived from C++)

**URL:** <https://forums.unrealengine.com/t/spawn-derived-class-at-runtime-blueprint-derived-from-c/340127>\
**Category:** Blueprint\
**Tags:** spawn, unreal-engine, UE4, CPP, Blueprint, question\
**Created:** [November 30, 2015, 2:46pm UTC](https://forums.unrealengine.com/t/spawn-derived-class-at-runtime-blueprint-derived-from-c/340127 "2015-11-30T14:46:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![NegInfinity](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@NegInfinity](https://forums.unrealengine.com/u/NegInfinity)\
**Post date:** [November 30, 2015, 2:46pm UTC](https://forums.unrealengine.com/t/spawn-derived-class-at-runtime-blueprint-derived-from-c/340127/1 "2015-11-30T14:46:52Z")

</div>

Let’s say I have a property in my actor class:

```
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Magic)
TSubclassOf<ASpellProjectile> testProjectileClass;

```

I want to set this property in editor (by dragging Blueprint derived from ASpellProjectile into property), and then spawn the class at runtime.

Problems:

First, for some reason this doesn’t work:

```
ASpellProjectile *tmp = Cast<ASpellProjectile>(testProjectileClass);

```

in this example tmp will always be null.

Second, attempting to spawn actor being referenced:

```
ASpellProjectile *projectile = 
	world->SpawnActor<ASpellProjectile>(testProjectileClass->GetClass(), transform, spawnParams);

```

Results in message:

```
 SpawnActor failed because BlueprintGeneratedClass is not an actor class

```

So, how do I actually spawn an actor from “template”/“preset”?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_41845f4a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_41845f4a](https://forums.unrealengine.com/u/anonymous_user_41845f4a)\
**Post date:** [November 30, 2015, 7:38pm UTC](https://forums.unrealengine.com/t/spawn-derived-class-at-runtime-blueprint-derived-from-c/340127/2 "2015-11-30T19:38:08Z")

</div>

Hello NegInfinity,

You should be able to fix this by making one change to the lines of code you provided. In this line:

```
ASpellProjectile *projectile = world->SpawnActor<ASpellProjectile>(testProjectileClass->GetClass(), transform, spawnParams);

```

Remove the “-\>GetClass()” part and just leave the variable name “testProjectileClass”. This should let the function know that you intend to spawn that class. You can find more information about this kind of setup from the following video.

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — https://unrealcommunity.wiki/! You will be able to find content from...

Hope this helps!

---

<div class="post-metadata">

**Author:** ![NegInfinity](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@NegInfinity](https://forums.unrealengine.com/u/NegInfinity)\
**Post date:** [December 1, 2015, 11:45am UTC](https://forums.unrealengine.com/t/spawn-derived-class-at-runtime-blueprint-derived-from-c/340127/3 "2015-12-01T11:45:52Z")

</div>

Thanks for the response. I was having trouble because I thought that testProjectileClass points at object instance to clone (like unity prefabs), not at object class. Someone mentiond on the forums that it points at class, not instance, and that cleared things up.
