Create A Simple Class C++

I have historically not done much of my UE4 work in C++ and have been able to get away with creating classes with Actor parents. I am starting a new project that I would like to be more structured and would like to make use of objects more efficiently.

All I want to do is to create an object that can hold variables and call functions to modify those variables or use them to return data. A struct will not cut it because it cannot contain functions.

This is very simple by using Actor as a parent but it just seems wrong. I tried creating a class with UObject as a parent and I couldn’t construct the object in blueprints or create a blueprint class based on it.

What parent class am I looking for? Thanks a ton in advance!

Hi tehhax
You are looking for UDataAsset, it is a UObject based class just to store data and call functions, there is a whole live stream to create state machine, and they are using this, check here:

link text

Also the most proper way to create something really custom in your case is using a UObject, and then your will need and extra module just for editor, create a UFactory for your object and then you can start by adding functionality, but I think UDataAsset is enough just for data and functions!

Structs can contain Functions in C++ ;P. But you where on the right track you can use UObject as parent class. You can create UObjects in BP via the “Construct Object from Class” Node. To create Blueprints from your UObject make sure to add Blueprintable to your UCLASS Macro.

https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Reference/Classes/Specifiers/Blueprintable/index.html

Thats it. And a little gotcha you might look out for if the created Object is not refferenced in a UPROPERTY anywhere it will get Garbage Collected automaticly.

UDataAsset has no Function Graphs (I assume he want to add something inside BP too), No UFactory needed for UObjects (see my Answer). Everything else is on point =)

Oh wow it was as simple as adding Blueprintable to the class. You’re a rockstar! I really need to get more familiar with Unreal’s macros. One step at a time, I guess.

Follow-up question: In the blueprint node “Construct Object”, what should I set the ‘Outer’ pin to? I set it to some random mesh and it worked fine but it feels sloppy.

Also: Structs can contain functions?! Knowledge bomb.

The outer refers to the Owner of this Object. Actors for example are owned by the Level and Components on a Actor are owned by the Actor (or Level if not attached to a Actor). You can use the Actor that is responsible for the Lifetime of the Object (usually the one who creates it, “self”). You should not run into any trouble with that in most cases.

And yes structs can contain Function and you can call them via BP too. Structs however don´t have any Function Graphs inside the editor so you can´t add more functionality. The rule of thumb is to use them as plain Data or limit the functionality to operations involving the struct Data at least.

That makes perfect sense for the outer param I’ll usually use self. Thank you so much for the answers and explanations!