UE C++: Matrices with generic data types

For creating a matrix, I made a UStruct as a wrapper containing only a TArray<> as its member. Putting that into another Array and I got a matrix I can loop over.

But in C++ I have to define the type of values the TArray<> member can hold which is currently FString. So far so good, but I want the array to be generic, such as in Blueprints where the type of Array data type is determined when you first plug in an element. But I have no idea how to set up a let’s say TArray as a UPROPERTY which can accept a generics.

Thanks in advance for any suggestions or ideas!

The short version: you can’t.

Longer version: To do what you’re asking requires that you write your struct using templates which would allow you to do something like TArray< FMyStruct< int > >. You can do this but with the limitation that it can’t be a UPROPERTY.
Unreal’s reflection support is very limited when it comes to templates. Not even the TOptional template provided by the engine is supported as a UPROPERTY. The containers (TMap, TSet, TArray) are the exceptions and have special support to enable them.
Even so, the support for them is limited and TArray< TArray< int > > isn’t supported as a UPROPERTY (which may have driven you to the struct wrapper, a common Unreal idiom to get around this restriction).

1 Like