Download

How do you programmatically instantiate a new UDataAsset?

Per Rama’s excellent advice in https://forums.unrealengine.com/showthread.php?66464-UDataAsset-vs-Blueprintable-UObject , I’m creating a data holder for my quest system by extending UDataAsset:



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

UCLASS(BlueprintType)
class UQuestKillObjective : public UQuestObjective {
	GENERATED_BODY()
public:

		UPROPERTY()
		int32 enemiesKilled;

		UPROPERTY()
		int32 killsRequired;

		UFUNCTION()
		bool IsComplete() override{
			if (enemiesKilled >= killsRequired) {
			return true;
			}
			else {
				return false;
			}
		}
};

As you can see, my logic explicitly relies on my ability to override IsComplete() and store a quest’s entire string of objectives as an array of UQuestObjective*, if it weren’t for that I would just do this with structs.

That having been said, how do I safely declare an instance of my class when I need to create a new object? If this were a struct I could just write “UQuestKillObjective newObjective”, and if it extended UActor I could use GetWorld()->SpawnActor, but I’ve never had the need to create and destroy UObjects that don’t inherit from U Actor before, so I have no idea what the safe procedure is.