Hey folks, I’m trying to assign a data asset I’ve defined in the editor to a variable in code. I’ve never really worked with data assets in code, and I’m mostly experimenting so I’m not even sure this is a recommended way to do things.
I’ve created a UPrimaryDataAsset class called ItemPrimaryDataAsset. In the editor I created and defined a data asset derived from that class. In code I’ve declared a pointer like this:
This leaves you with a hard-coded path which might not be the best on some cases but if the reason that you are “specifically trying to set it in code” is just to avoid hard reference to the asset, you can also expose FSoftObjectPath on the editor (or just use TSoftObjectPtr).
Excellent! This put me on the right track. I had to make a couple of changes since the asset in the editor was derived from a primary data asset but was of type data asset. This is definitely a bad way to do this. Thanks for the help!
if(!EquippedDefaultItem)
{
const FSoftObjectPath DefaultItemPath(TEXT("/Game/Data/DA_Defaults/DA_DefaultItem"));
EquippedDefaultItem = Cast<UDataAsset>(DefaultItemPath.TryLoad());
};
// Then later in a Switch Case
EquippedItem = Cast<UItemPrimaryDataAsset>(EquippedDefaultItem);