T4 / Text Templates

Hey,

I was just curious if anyone else familiar with T4 templates has had any luck in getting them to run within a unreal project in VS. Currently, I’m manually using another vs project flagged as C# then copying over the outputs of the text transformations but it would be handy if UHT or whatever had some means of being able to define specific objects / build tools?

Thanks

Isn’t Macro good enough for what you need?

C++ already has a powerful templating system on board, which is called… templates. It’s the system used to power generic container types such as TArray and TMap, string conversions with TStringConvert, and reflection with things like TProperty, TObjectIterator and TFieldRange, to name a few examples. Basically anything prefixed with a T in the UE4 source code. It may seem limited from these few examples, but if you dig deep enough you will find that C++ templates are a fully featured functional programming language inside the compiler that allows you to generate all sorts of complex code, based on pattern matching with C++ types.

C++ templates have a steep learning curve though and can be a nightmare to decipher and debug, so if you want to keep things simple I’d recommend sticking to what you are doing now. T4 templates were designed specifically for use with C# and .NET, so I wouldn’t expect native support for C++ projects anytime soon. In theory, you could take the C# code from a generated Runtime Text Template and add it to your .Build.cs file for use in a pre-build step, but I’m not sure how well that would work in practice.

Thanks for the tips and responses!

I’m well aware of macros and c++ templates but they are quite different than T4 which actually generates output. Also AFAIK UHT fails to expand macros and only supports specific template types (TArray/TSet/TSubclassOf etc) so using T4 for tediously large USTRUCT/UCLASS/UENUM etc is completly compatible. So just to be clear, I’m also a fan of c++ templates but basically because of UHT, they cant properly work in every scenario to reduce human error, and macros – same goes with UHT, but its also a little frustratin to populate all namespaces with defines and rather complicated ones can slow down compile times. So I was just curious if anyone else had any success with setting them up as a custom build step in a way that they retain their configuration when the VS solution is rebuilt/regenerated?

Thanks again for the feedback