How do you instantiate a new UObject?

Based on the awesome advice in UDataAsset vs Blueprintable UObject - C++ - Unreal Engine Forums I’m using a child of UDataAsset to store each kind of quest objective in my system:

UCLASS(BlueprintType)
class UQuestObjective : public UDataAsset {
	GENERATED_BODY()
public:	
	UFUNCTION()
	virtual bool IsComplete() {
		return true;
	}
};

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

UQuestObjective  testObjective;
testObjective.IsComplete();

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.

1 Like

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

11 Likes

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? :slight_smile:

Thank you for sharing your knowledge =) Epic regards

So, do I get it right, that you cannot actually create a static instance of a class that inherits UObject?

Say I have a class USomething, which inherits UObject. Than I have a class called say, MyClass, which has a USomething attribute like so:

class MyClass{
    private:
        USomething myAtribute;
    public:
        MyClass(){}
    //other stuff...
}

So I cannot actually do this, right? It has to be:

UPROPERTY()
USomething* myAttribute;

…and allocated as:

myAttribute= NewObject<USomething>();

Or did I get it all wrong?

1 Like