# How do I provide C++ with a class to spawn an object from the editor?

**URL:** <https://forums.unrealengine.com/t/how-do-i-provide-c-with-a-class-to-spawn-an-object-from-the-editor/337012>\
**Category:** Blueprint\
**Tags:** unreal-engine, editor, CPP, Blueprint, question\
**Created:** [October 30, 2015, 11:32pm UTC](https://forums.unrealengine.com/t/how-do-i-provide-c-with-a-class-to-spawn-an-object-from-the-editor/337012 "2015-10-30T23:32:52Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![bombmask](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bombmask](https://forums.unrealengine.com/u/bombmask)\
**Post date:** [October 30, 2015, 11:32pm UTC](https://forums.unrealengine.com/t/how-do-i-provide-c-with-a-class-to-spawn-an-object-from-the-editor/337012/1 "2015-10-30T23:32:52Z")

</div>

So to elaborate I want to provide the functionality to swap out the way an actor functions by specifying a certain class which overrides functionality though inheritance.

The basic question is can I do what blueprint variables can do in C++?

 ![64956-test.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/7/e/f7edd7b2fda3e8acd94a3cc57b2b46a99ac6205d.png)

Shown here is the ability to set a blueprint variable to a class which the blueprint could later use to spawn that type of actor. Though polymorphism it is shown that the editor is aware of compatible classes of the selected class.

 ![64957-test1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/e/5/fe51a2570f1cf508f30ed0c69d709e27f4aaf0f6.png)

![64958-testme.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/e/2/ae2201addf147ea2ed3b5e5f94f134b9afdef279.png)

My code at the moment attempted to use pointers to allow blueprint to specify the object and let my class get the object to call from but that required an already spawned object and since my class inherits from UObject and does not provide any visual or scene aspect to the engine there isn’t a way to spawn one that I know of.

```
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UTurtle * TurtleMesher; // Inherits from UObject

```

I would like to be able to select a base class and then select classes that inherit from that base class and show them in the drop down menu. I don’t know if C++ has that ability or if it was something that only blueprint can do.

#Edit  
I’ve tried to do

```
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mesh Building")
UTurtle * TurtleMesher;

```

this code to try and make it so I can give the C++ script an object but the object isn’t allowed to receive it unless it’s already made and I can’t spawn it. What I wanted to do was give the C++ script the ability to spawn it’s own object given through a variable and use polymorphism to store it using the base specifier of UTurtle and anything that inherits from UTurtle can be stored in that variable.  
I would like this menu below in the menu in the next picture. But I realize that can’t be done as long as it’s a pointer.

![64958-testme.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/e/2/ae2201addf147ea2ed3b5e5f94f134b9afdef279.png)

 ![64974-turtlemesh3.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/d/5/2d5b54381270aa86e5f01c3d32ba1176f2bf7ef7.png)

---

<div class="post-metadata">

**Author:** ![makai](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@makai](https://forums.unrealengine.com/u/makai)\
**Post date:** [October 31, 2015, 12:07am UTC](https://forums.unrealengine.com/t/how-do-i-provide-c-with-a-class-to-spawn-an-object-from-the-editor/337012/2 "2015-10-31T00:07:06Z")

</div>

hi,  
since 4.9 you can take use of “Construct Object From Class” node ([What's New | Unreal Engine 5.2 Documentation](https://docs.unrealengine.com/latest/INT/Support/Builds/ReleaseNotes/2015/4_9/index.html) search for construct)

otherwise in most cases you want to derive from actor and spawn or place inside a map even if you write a manager actor, you can easily spawn it in the map.  
im not sure what you are trying to do but from your screens and what you described i guess you are better by going the actor way as they have a UWorld context and you have much more functionality (please correct me if im wrong)

best wishes

---

<div class="post-metadata">

**Author:** ![bombmask](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bombmask](https://forums.unrealengine.com/u/bombmask)\
**Post date:** [October 31, 2015, 12:45am UTC](https://forums.unrealengine.com/t/how-do-i-provide-c-with-a-class-to-spawn-an-object-from-the-editor/337012/3 "2015-10-31T00:45:03Z")

</div>

That wasn’t exactly what I was trying to do. The UTurtle is a class that makes points on a line and I wanted to make it inherit from FRunnable later on. The idea was that I could change out the UTurtle in the owning C++ class and then change it’s functionality based on the given UTurtle since later on I could inherit from UTurtle and override functions that exist in the parent class that would change the output of UTurtle. The reason why I need to spawn UTurtle is so that I can do two things, Separate my classes so that it’s not one big mega class and two the turtle needs to have it’s own data per instance but provide a single function interface to that data. I don’t even know if C++ can do this and even if it can though templates I don’t know if UE4’s reflection system supports templates.

---

<div class="post-metadata">

**Author:** ![bombmask](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bombmask](https://forums.unrealengine.com/u/bombmask)\
**Post date:** [October 31, 2015, 1:09am UTC](https://forums.unrealengine.com/t/how-do-i-provide-c-with-a-class-to-spawn-an-object-from-the-editor/337012/4 "2015-10-31T01:09:12Z")

</div>

I did update the bottom of the post being more specific. Sorry I wasn’t very clear.

---

<div class="post-metadata">

**Author:** ![makai](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@makai](https://forums.unrealengine.com/u/makai)\
**Post date:** [October 31, 2015, 1:14am UTC](https://forums.unrealengine.com/t/how-do-i-provide-c-with-a-class-to-spawn-an-object-from-the-editor/337012/5 "2015-10-31T01:14:02Z")

</div>

Of course you can use all advantages of polymorphism to avoid mega classes, but as more you derive as more errors can get into your code if you don’t plan your classes extensively BEFORE you work it out.  
I still don’t know why you don’t derive from actor and then derive it logically into those parts you want it as your final object will be…actor!?

here is an old tutorial about templates in UE4 by rama

> **[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 that helps

---

<div class="post-metadata">

**Author:** ![bombmask](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bombmask](https://forums.unrealengine.com/u/bombmask)\
**Post date:** [October 31, 2015, 1:17am UTC](https://forums.unrealengine.com/t/how-do-i-provide-c-with-a-class-to-spawn-an-object-from-the-editor/337012/6 "2015-10-31T01:17:26Z")

</div>

I’m not deriving from actor because this is basicly only a utility class but I want it exposed to the editor as an option to select so I thought I had to derive from UObject, the turtle class is a utility to create points for a procedural mesh component so while I’m not technically putting this in an actor it does go into actors to be shown in the world but none of my objects need an actor component but rather go into actors.

---

<div class="post-metadata">

**Author:** ![makai](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@makai](https://forums.unrealengine.com/u/makai)\
**Post date:** [October 31, 2015, 1:23am UTC](https://forums.unrealengine.com/t/how-do-i-provide-c-with-a-class-to-spawn-an-object-from-the-editor/337012/7 "2015-10-31T01:23:58Z")

</div>

if i understand you right, you propably want to derive from UActorComponent ?

> **[Components](https://docs.unrealengine.com/5.2/en-US/components-in-unreal-engine/)**
>
> An overview of Components

---

<div class="post-metadata">

**Author:** ![bombmask](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bombmask](https://forums.unrealengine.com/u/bombmask)\
**Post date:** [October 31, 2015, 1:57am UTC](https://forums.unrealengine.com/t/how-do-i-provide-c-with-a-class-to-spawn-an-object-from-the-editor/337012/8 "2015-10-31T01:57:13Z")

</div>

I don’t think I’m communicating this right. There might be a better way to do this but I was trying to follow a model view controler type design.

 ![64977-programflow1.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/3/5/6353d4e2c3059796488b9befc42eec31d6481db6.jpeg)

The Classes with the asterisks are the ones that I’ve created. I just wanted the turtle to be in a separate class. [Turtle Graphics][2] that’s just what the turtle class is for.

[The Procedural Mesh Component From Unreal][3]

---

<div class="post-metadata">

**Author:** ![makai](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@makai](https://forums.unrealengine.com/u/makai)\
**Post date:** [October 31, 2015, 4:54am UTC](https://forums.unrealengine.com/t/how-do-i-provide-c-with-a-class-to-spawn-an-object-from-the-editor/337012/9 "2015-10-31T04:54:36Z")

</div>

ok i understand but i don’t know if you can go this way i guess not as it seems to go straight against the architecture how unreal works ([Actors in Unreal Engine | Unreal Engine 5.2 Documentation](https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Actors/index.html)).  
you would have to write your very own system and break the complete architecture how unreal works (correct me if i am wrong).

i would take a look into components… if you derive from aactor and instance your uturtle protected/public you can call those base functions in all childs even if you decide to create a component for the view part

---

<div class="post-metadata">

**Author:** ![bombmask](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bombmask](https://forums.unrealengine.com/u/bombmask)\
**Post date:** [October 31, 2015, 5:07am UTC](https://forums.unrealengine.com/t/how-do-i-provide-c-with-a-class-to-spawn-an-object-from-the-editor/337012/10 "2015-10-31T05:07:29Z")

</div>

Well I sorta made a hack for it using your first suggestion.

 ![64991-capturea.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/8/4/e84383728ebad4ce79b41ed28e0e2edaa4809d27.png)

I didn’t really want to use blueprint for that part but at the moment it seems like that’s what I need to do.

it sort of fixes my problem of wanting to change functionality using the editor, thanks for the pointer in the right direction towards construct object node.

---

<div class="post-metadata">

**Author:** ![bombmask](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bombmask](https://forums.unrealengine.com/u/bombmask)\
**Post date:** [November 22, 2015, 7:41am UTC](https://forums.unrealengine.com/t/how-do-i-provide-c-with-a-class-to-spawn-an-object-from-the-editor/337012/11 "2015-11-22T07:41:08Z")

</div>

Hello!!  
I’ve found what I was looking for. And it was really simple.  
[https://docs.unrealengine.com/latest/INT/API/Runtime/CoreUObject/UObject/TSubclassOf/index.html](https://docs.unrealengine.com/latest/INT/API/Runtime/CoreUObject/UObject/TSubclassOf/index.html)

Allows you to do everything I was looking for.

And adds the dropdown menu I was needing into the C++ so I don’t have to wrap spawning objects in blueprint anymore.

Cheers!
