Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

How do you instantiate a new UObject?

Based on the awesome advice in https://forums.unrealengine.com/showthread.php?66464-UDataAsset-vs-Blueprintable-UObject I'm using a child of UDataAsset to store each kind of quest objective in my system:

 class UQuestObjective : public UDataAsset {
     virtual bool IsComplete() {
         return true;

This compiles cleanly, but when I attempt to declare it like a struct, unreal crashes:

 UQuestObjective  testObjective;

I assume you can't declare classes like structs, so I'm wondering how do you safely initialize and destroy one? I've always used CreateDefaultSubObject/SpawnActor, neither of which is appropriate for a UDataAsset.

Product Version: UE 4.10
more ▼

asked Mar 14 '16 at 10:51 PM in C++ Programming

avatar image

251 95 66 83

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

You can in with normal C++ class, but not UObjects, they exclusivly can be only hold as pointer and you can use "new", "delete" or statically declare them (which you did) you need to use UE4 APIs to control "there existance", since they haeavyly memoery managed. To create instance of UObject do this:

 UQuestObjective*  testObjective;
 testObjective = NewObject<UQuestObjective>();

Also note, you can't delete UObjects on demend, you also need to hold pointer to it in atleast 1 UPROPERTY() varable, the way UObjects work is that they automatically deleted by garbage collection when they are not referenced anywhere, you should also not circular refrence UObject without control because that might produce memory leak, always break the circle when objects are uneeded.

AActors are also UObjects but oyu use SpawnActor() insted of NewObject() also they can be deleted on demend

more ▼

answered Mar 14 '16 at 11:19 PM

avatar image

36.6k 931 169 1111

avatar image HInoue Mar 14 '16 at 11:42 PM

Ooh that clears up a lot, thank you!! So to be clear, I'm safe using a data asset as a kind of "Use and forget" data holder like I've described, and trusting Unreal to correctly GC it when it's no longer being referenced anywhere? :)

avatar image Sir Rollalot Mar 31 '19 at 02:54 PM

Thank you for sharing your knowledge =) Epic regards

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question