Save data custom UDataAsset which is no UPROPERTY

I want to save data in a custom UDataAsset (which is not supposed to be edited in editor) and if I observed correctly only vars which have the UPROPERTY macro will be saved in such an asset.
For example if I have an custom UDataAsset like following:

UCLASS()
class MY_API USizeTDataAsset: public UDataAsset
{
	GENERATED_BODY()
	
public:
	size_t m_Size;

    UPROPERTY()
    int m_SizeInt;
};

Create and set it with following:

// get asset tool
IAssetTools& AssetTools = FModuleManager::GetModuleChecked<FAssetToolsModule>("AssetTools").Get();

// create factory
UDataAssetFactory* Factory = NewObject<UDataAssetFactory>();

// create asset
USizeTDataAsset* Asset = Cast<USizeTDataAsset>(AssetTools.CreateAsset(fileName, *outputPath, USizeTDataAsset::StaticClass(), Factory));

Asset->m_Size = 11;
Asset->m_SizeInt = 11;

And load it like so:

USizeTDataAsset* DataAsset = LoadObject<USizeTDataAsset>(nullptr, TEXT("PATH_TO_ASSET"), nullptr);

DataAsset->m_Size will be 0 (unset) and DataAsset->m_SizeInt will be 11.

I know unreal has a lot of data types which are supported by UPROPERTY and converting a size_t to int would be pretty easy, but I am have a lot more unsupported datatypes (like FXxHash128, std::vector, custom structs and more). Converting them all to supported types would be annoying and unefficient.

TLDR:

  • Is there a way to store data types which are not supported by the UPROPERTY macro in a DataAsset?
  • Or is there a way to add support for classes with the UPROPERTY macro?
  • Or is there a better solution for saving data which isn’t supposed to be edited in editor (and is automatically packaged by unreal without the need to add a extra Non-Asset packaging directory)?

int may not have consistent size on all platforms, it is suggested to use int32 for better platform interoperability.
instead of std::vector consider using TArray<T> where you do get Saving, and UPROPERTY() protections
Custom Structs can be made UPROPERTY() as long as they are designated as

USTRUCT(BlueprintType)
struct FMyStructType
{
    GENERATED_USTRUCT_BODY()
public:
    // data members (including "UPROPERTY()" members for nesting) here
};

UCLASS(BlueprintType)
class [ProjectName]_API AMyCharacter : public ACharacter
{
    GENERATED_BODY()
private:
    UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = true))
    FMyStructType MyStruct;
};

for the most part the rule is:

  • only types marked with UCLASS, USTRUCT, UINTERFACE, or UENUM can be UPROPERTY with data saving and visibility/edit-ability in the Editor.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.