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:
This compiles cleanly, but when I attempt to declare it like a struct, unreal crashes:
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.
asked Mar 14 '16 at 10:51 PM in C++ Programming
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:
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
Follow this question
Once you sign in you will be able to subscribe for any updates here