1

In Unreal Engine, I've created a series of structs in C++ that let me generate a replicated version of the TMap struct. It relies on a number of engine resources like Fast Array Serialization, and the structs needs to be recognized by the engine like a normal USTRUCT.

For example, here's there starting piece of one of the structs:

USTRUCT(BlueprintType)
struct FReplicatedTMap_GuidToGuid_Array : public FFastArraySerializer
{
    GENERATED_BODY()

public:
    UPROPERTY(BlueprintReadOnly, NotReplicated, VisibleAnywhere)
        TMap<FGuid, FGuid> ReplicatedMap;

Long story short, my implementation works. However, due to UHT (Unreal Header Tool) constraints, I cannot use templates

USTRUCT(BlueprintType)
template<>

because it isn't supported by the engine, and I can't use macros

#define \
USTRUCT(BlueprintType) \

because the UHT pre-processor doesn't expand macros before looking for its own macros like USTRUCT(), and this prevents pre-processor generated code from being evaluated by the engine.

In the first code example, you can see that each set of struct/function definitions only apply to one key type - value type pair. The code itself is only ~150 lines or so, but I'd like to not have to change multiple dozens of definitions every time I need to tweak these struct templates, let alone create additional copies whenever I need new types.

Is there a solution within Unreal, or using C++? What's the best way to autogenerate the code I need?

Kyton
  • 355
  • 3
  • 12
  • 1
    Doing your own preprocessing or code-generation on the side, before unreal even gets to see the code? – Jeffrey Jul 07 '20 at 20:28
  • Is there a solution that can be triggered by a build within Visual Studio? There are plenty of solutions as far using external tools to replace code and generate files, but UHT hijacks most compilers' preprocessors as far as I know, and the question is more of "is there a way to do this I don't know about?" rather than "does a solution to autogenerate code outside an editor exist?". – Kyton Jul 07 '20 at 20:35
  • The short answer is you can't without adding another build step. I would probably shift to `TArray>` you might lose out on one or two functions but you can write free templated helper functions if you need them. You may want to profile this solution. Though anecdotally, the team developing the chaos engine switched to arrays after finding a performance bottleneck with sets (`TMap`'s underlying type). I'm unsure on the specifics but they can be found online. Anyway doing your own profiling is usually best. – George Jul 08 '20 at 08:21
  • Yep, another build step was required. I'll plan on profiling the TPair solution if I end up hitting bottlenecks with this particular module, thanks for the advice. – Kyton Jul 14 '20 at 19:57

1 Answers1

0

In case anyone stumbles across this post, the best solution I found is using Visual Studio's T4 Text Templating engine, despite it not being included in C++ project files. In the case of UE4, you can use the UProject file to prebuild a Text Template.

Important Resources:

T4 in C++ UE4 PreBuild

My UProject Code addition to run the T4 engine with VS2019:

"PreBuildSteps":
{
    "Win64": [
        "\"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Common7\\IDE\\TextTransform.exe\"
        $(ProjectDir)\\Source\\<YOUR_FILE_LOCATION>\\<YOUR_TEMPLATE_FILE>.tt","echo T4 Executions Complete"
    ]
}
Kyton
  • 355
  • 3
  • 12