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

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++?

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.


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.

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.


since 4.9 you can take use of “Construct Object From Class” node (Unreal Engine 4.9 Release Notes | Unreal Engine Documentation 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

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.

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

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.

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

hope that helps

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

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.

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]

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

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.

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 | Unreal Engine Documentation).
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

I’ve found what I was looking for. And it was really simple.

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.