# Spawn Actor/Object from code

**URL:** <https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739>\
**Category:** C++\
**Tags:** spawn, spawn-actor-from-cla, unreal-engine, Blueprint, question, CPP\
**Created:** [June 25, 2014, 1:35pm UTC](https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739 "2014-06-25T13:35:58Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_2c530438](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2c530438](https://forums.unrealengine.com/u/anonymous_user_2c530438)\
**Post date:** [June 25, 2014, 1:35pm UTC](https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739/1 "2014-06-25T13:35:58Z")

</div>

Hi!

I switched from Unity3D to UE4 and I have the following problem:

In Unity3D I created prefab with some objects inside. And in other script I Instantiated the prefab into world.  
In UE4 I created blueprint, because here I can build objects together using visual editor, but I have my game code in C++, so I need to spawn the blueprint from C++ (and interact with it in C++).

Is there any way how to this?  
I read the Blueprint will be available only from editor, so is there any other solution?  
Thanks.

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [June 25, 2014, 1:56pm UTC](https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739/2 "2014-06-25T13:56:46Z")

</div>

_"Is there any way how to this? I read the Blueprint will be available only from editor, so is there any other solution? "_

You absolutely can interact with Blueprints from c++ !

I do it all day!

To answer your question, yes you can spawn Blueprint Actors with all your custom setups straight in the C++.

**To answer all your other unasked questions:**

You can do anything in c++! UE4 C++ is amazingly powerful!

Rama

---

<div class="post-metadata">

**Author:** ![ivanesses](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ivanesses](https://forums.unrealengine.com/u/ivanesses)\
**Post date:** [June 25, 2014, 2:24pm UTC](https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739/3 "2014-06-25T14:24:44Z")

</div>

And can you make separate actors via Construction Scripts using c++?  
This question arises every time  
[https://answers.unrealengine.com/questions/60041/videobphow-to-make-separate-blueprints-via-constru.html](https://answers.unrealengine.com/questions/60041/videobphow-to-make-separate-blueprints-via-constru.html)  
Similar unsolved problem to Rama specially

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2c530438](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2c530438](https://forums.unrealengine.com/u/anonymous_user_2c530438)\
**Post date:** [June 25, 2014, 2:33pm UTC](https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739/4 "2014-06-25T14:33:55Z")

</div>

Thank you, can you please write a little example how to do this please?

---

<div class="post-metadata">

**Author:** ![Kochab](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kochab](https://forums.unrealengine.com/u/Kochab)\
**Post date:** [November 14, 2014, 8:40am UTC](https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739/5 "2014-11-14T08:40:49Z")

</div>

Here is how I spawn a blueprint via C++. Note that the blueprint I spawn has a base class that was created in C++ also.

**.h**

```
 TSubclassOf<YourClass> BlueprintVar; // YourClass is the base class that your blueprint uses

```

**.cpp**

```
 ClassThatWillSpawnTheBlueprint::ClassThatWillSpawnTheBlueprint(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
 {
      static ConstructorHelpers::FObjectFinder<UBlueprint> PutNameHere(TEXT("Blueprint'/Path/To/Your/Blueprint/BP.BP'"));
      if (PutNameHere.Object) {
           BlueprintVar = (UClass*)PutNameHere.Object->GeneratedClass;
      }
 }

```

`PutNameHere` is just an arbitrary name you give to the constructor helper. The path to your blueprint is found by finding your blueprint in the content browser, right clicking it, and choosing `Copy Reference`. Then, just paste that in between the quotes.

Now, you’re ready to spawn the blueprint. You can do it in BeginPlay() or wherever, just not in the constructor.

```
 UWorld* const World = GetWorld(); // get a reference to the world
 if (World) { 
      // if world exists
      YourClass* YC = World->SpawnActor<YourClass>(BlueprintVar, SpawnLocation, SpawnRotation);
 }

```

If you don’t know your SpawnLocation or SpawnRotation you can just throw in FVector(0,0,0) and FRotator(0,0,0) instead.

So, since your blueprint base class was also created in C++ this makes it easy to interact with your blueprint from code. It’s as simple as `YC->SomeVariable = SomeValue`. Hope that helps.

* * *

# EDIT: A more in depth explanation how this works.

## Summary

Communicating with a blueprint via C++ requires some initial setup with the blueprint you need to communicate with. It is not possible to talk to a blueprint variable that was _created inside of blueprints_ via C++. It has to be a C++ variable that the blueprint simply uses. You do this by creating your own base class in C++ for the blueprint, declaring all of the variables that your blueprint will need inside that class, and then setting the parent class of your blueprint to your base C++ class.

## Creating the base class

First, you need to create a base class for your blueprint, probably extending from AActor. Then declare your variables in the `.h` like so:

```
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Projectile)
float ProjectileSpeed; // just an example

```

Only `UPROPERTY` variables will work. `BlueprintReadWrite` means this variable can both be read and written to inside of blueprints. `EditDefaultsOnly` is optional but it will allow you to edit that variable within the blueprint defaults if you need to. For example, you could tweak the speed without having to recompile everything.

## Parenting the blueprint

Once you have your base class written and compiled, you will need to parent your blueprint to that class.

1. open blueprint
2. click graph
3. click blueprint properties
4. in the details panel, scroll down until you see Parent Class and use the drop down to choose your C++ class.

You can also set the base class at the time of blueprint creation. As long as your class is compiled it will show up as an option.

## Accessing inherited variables inside of blueprints

You will now be able to access the variables you created in your C++ class from inside your blueprint by clicking the “show inherited variables” tick box. Since we set the category to “Projectile” above, that is where they will be listed. And since we made them `BlueprintReadWrite`, we can use them.

You will have to replace any variables you’ve already created in the blueprint with these new inherited variables from the base class. For example if you had created a variable in blueprints called `ProjectileSpeed` and used that in 5 different places, you would have to remove all of them, delete that variable and replace them with the inherited `ProjectileSpeed` instead. This can get confusing and is a pain, especially if your graph is big. To make this easier you could call the inherited variable something different like `ParentProjectileSpeed` or something, that way neither you or the editor will get confused about which is which.

## Spawning the blueprint via code and communicating with it

After this is done, you can then spawn and communicate with this blueprint via C++. If your base class is called `AMyProjectile` then you would write:

```
void AClassThatWillSpawnTheBlueprint::BeginPlay()
{
	UWorld* const World = GetWorld(); // get a reference to the world
	if (World) {
		// if world exists

		// we are now creating a new instance of this blueprint
		AMyProjectile* Missile = World->SpawnActor<AMyProjectile>(BlueprintVar, SpawnLocation, SpawnRotation);
	}

	// setting a blueprint instance's variable
	Missile->ProjectileSpeed = 100;
}

```

Keep in mind your `BlueprintVar` will need to use this base class as well.

```
TSubclassOf<AMyProjectile> BlueprintVar;

```

---

<div class="post-metadata">

**Author:** ![kspr\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kspr\_1](https://forums.unrealengine.com/u/kspr_1)\
**Post date:** [February 13, 2015, 8:40am UTC](https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739/6 "2015-02-13T08:40:03Z")

</div>

I am also switching from Unity3D and need help understanding the different paradigms, could I contact you somehow for help?

---

<div class="post-metadata">

**Author:** ![S4intFl0w](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@S4intFl0w](https://forums.unrealengine.com/u/S4intFl0w)\
**Post date:** [February 27, 2015, 10:01pm UTC](https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739/7 "2015-02-27T22:01:53Z")

</div>

As I cannot create a topic, I will answer here hoping that someone might give it a read.

Spawning a Blueprint in c++ with 4.7 error C2027: use of undefined type ‘UBlueprint’

Just trying to spawn a Blueprint Projectile instead of the c++ class projectile within the Twin Stick Shooter Template.

Read through all the other topics and tried to do it like it has been described here: [Spawn Actor/Object from code - C++ - Unreal Engine Forums](https://answers.unrealengine.com/questions/60897/spawn-actorobject-from-code.html)

Sadly, I just cannot seem to get it to work, even after many hours of trying out different things. Given that I am very new to c++ and used to Java, I might be lacking the proper skills, but I still would love to know where I made a mistake in this simple process. here is a screenshot of the errorlog and code: [Dropbox - Error](https://www.dropbox.com/s/2jdeq0rskay6wme/Screenshot%202015-02-27%2022.50.39.png?dl=0)

Any help would be greatly appreciated as I just cannot get around the Ublueprint error.

Thanks so much!

---

<div class="post-metadata">

**Author:** ![kamrann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kamrann](https://forums.unrealengine.com/u/kamrann)\
**Post date:** [February 27, 2015, 10:38pm UTC](https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739/8 "2015-02-27T22:38:35Z")

</div>

Add `#include "Engine/Blueprint.h"` to the list of includes at the top of your cpp file.

---

<div class="post-metadata">

**Author:** ![S4intFl0w](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@S4intFl0w](https://forums.unrealengine.com/u/S4intFl0w)\
**Post date:** [February 27, 2015, 10:55pm UTC](https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739/9 "2015-02-27T22:55:53Z")

</div>

Thank you for your quick reply! I thought there had to be an include for that. sadly, #include “Blue…” searching and google searches on that did not point me to that one.

Now the Compiler shows me a bunch of new errors in all 3 files (2 of which i didnt touch at all). [http://i.imgur.com/56pFSpg.png](http://i.imgur.com/56pFSpg.png)

Any more help would be even more greatly appreciated! Sorry for being a newb.

---

<div class="post-metadata">

**Author:** ![Kochab](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kochab](https://forums.unrealengine.com/u/Kochab)\
**Post date:** [February 28, 2015, 12:06am UTC](https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739/10 "2015-02-28T00:06:27Z")

</div>

What engine version are you using? I didn’t have to include Engine/Blueprint.h for it to work, but I was using 4.5.1. Maybe something changed.

---

<div class="post-metadata">

**Author:** ![S4intFl0w](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@S4intFl0w](https://forums.unrealengine.com/u/S4intFl0w)\
**Post date:** [February 28, 2015, 12:36am UTC](https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739/11 "2015-02-28T00:36:45Z")

</div>

Using 4.7, new to C++ and rather new to UE4. Watching the C++ tutorials from Epic did not really prepare me for the 3 hours of trying to get a super simple thing to work sadly. 😕

---

<div class="post-metadata">

**Author:** ![eXi](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/exi/32/906583_2.png) [@eXi](https://forums.unrealengine.com/u/eXi)\
**Post date:** [February 28, 2015, 12:40am UTC](https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739/12 "2015-02-28T00:40:53Z")

</div>

Can you show me the full code of that class?

Pack it into [http://pastebin.com/](http://pastebin.com/) and post the link here please.

PS: If i don’t answer right away, it’s due to 01:44 o’clock in germany and i need some sleep 😃

---

<div class="post-metadata">

**Author:** ![S4intFl0w](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@S4intFl0w](https://forums.unrealengine.com/u/S4intFl0w)\
**Post date:** [February 28, 2015, 12:54am UTC](https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739/13 "2015-02-28T00:54:44Z")

</div>

EDIT: Okay, after some more time I got it to work by creating a new project with the template and just adding new code line by line and compiling after every line. What caused the old code you see below to crash was most likely the spawn call. it needed to be

> World-\>SpawnActor \< AActor \>  
> (I needed to put the spaces because otherwise the text wouldnt wanna show up in the comment)  
> Now I have the next problem. I have no idea how I should set all the variables I need to set on spawn (because I have no idea how to tell the c++ code about which variables the blueprint class has and needs) and I slowly get the impression that it’s just a bad idea to wanna access such a blueprint within c++ code?

Old Post:  
01:52, Germany here as well 😉 Thanks for your help. Here are the classes:  
h: [TwinStickCPawn.h - Pastebin.com](http://pastebin.com/7pMWVJmh)  
cpp: [TwinStickCPawn.cpp - Pastebin.com](http://pastebin.com/tdT8Nssk)

Note that I tried using  
TSubclassOf BlueprintVar;  
and  
UClass\* BlueprintVar;  
in the h file, neither worked.

Thanks so much for trying to help everyone!

---

<div class="post-metadata">

**Author:** ![Kochab](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kochab](https://forums.unrealengine.com/u/Kochab)\
**Post date:** [February 28, 2015, 8:52am UTC](https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739/14 "2015-02-28T08:52:52Z")

</div>

I noticed one problem in the files. Your BlueprintVar is not being set at line 35 in the .cpp. It should be:

```
BlueprintVar = (UClass*)ASD.Object->GeneratedClass;

```

Also, in the .h you shouldn’t need to specify the `class` keyword:

```
TSubclassOf<class AActor> BlueprintVar;

```

Do it like this instead:

```
TSubclassOf<AActor> BlueprintVar;

```

I’m not sure if that will make any difference but that’s how I’ve done it.

Also, you should not be using AActor, but rather your own class that extends AActor, because you won’t be able to declare the variables you need for your blueprint inside of AActor. I’m working on a more in depth explanation how this all works. I’ll update my original answer when it’s done.

---

<div class="post-metadata">

**Author:** ![Kochab](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kochab](https://forums.unrealengine.com/u/Kochab)\
**Post date:** [February 28, 2015, 10:03am UTC](https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739/15 "2015-02-28T10:03:07Z")

</div>

I’ve updated my answer and hopefully it will help you a bit more. If you have any other questions let me know.

---

<div class="post-metadata">

**Author:** ![S4intFl0w](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@S4intFl0w](https://forums.unrealengine.com/u/S4intFl0w)\
**Post date:** [February 28, 2015, 4:07pm UTC](https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739/16 "2015-02-28T16:07:41Z")

</div>

Thank you very much for taking the time to write such an indepth answer about this topic Sir Kochab. I think it helped me greatly understanding the concept and possible connections between c++ classes and blueprints and I will spend the next hours trying to put that new knowledge to good use!

Give that man Upvotes!

---

<div class="post-metadata">

**Author:** ![SadMonsterParty](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sadmonsterparty/32/88708_2.png) [@SadMonsterParty](https://forums.unrealengine.com/u/SadMonsterParty)\
**Post date:** [December 2, 2021, 2:48am UTC](https://forums.unrealengine.com/t/spawn-actor-object-from-code/286739/17 "2021-12-02T02:48:46Z")

</div>

I’m the 3rd person here moving from Unity to Unreal; hit me up if y’all wanna chat.
