Use TUnion with UPROPERTY?

Can TUnion be used with UPROPERTY?

I expected TUnion works in a similar way with TArray, but I got a compile error “Unrecognized type ‘TUnion’ - type must be a UCLASS, USTRUCT or UENUM”.

Here is a test code, please point out if I made any mistakes.

#pragma once

#include "CoreMinimal.h"
#include "Containers/Union.h"
#include "Teststructure.generated.h"

USTRUCT()
struct FTeststructure
{
	GENERATED_BODY()
public:
	UPROPERTY()
		TUnion<float, int32> theUnionData;
};

If that’s the error that you’re getting then it’s not supported. There are only a few of the T structures/classes that work with the property system. Off the top of my head I know that TOptional also does not work with the property system as I’ve had to work around that in the past.

TSet, TArray and TMap are the only template/container types supported by reflection.

You can leverage the USTRUCT() traits system to partially expose other types (bot to Blueprint of course), but it’s rarely needed unless you want to serialize something or reference UObjects’ in a non-exposed container.

2 Likes