UHT USTRUCT() Substitution Issue

Hello there,

I am having an issue trying to get some code to compile because of some kind of hangup with the evaluation of the macro at compile-time.

#define GAME_STRUCT(StructName)       USTRUCT() struct StructName

And I’m using it this way:

GAME_STRUCT(FMyStruct)
{
    GENERATED_BODY()
    // ... Stuff goes here
}

When I write something like above, I get this error:

GameDefinitions.h(107): warning : The identifier 'GENERATED_BODY' was detected in a block being skipped. Was this intentional?

But if I use the code:

USTRUCT()
struct FMyStruct
{
    GENERATED_BODY()
    // ...
}

It works fine.

And it’s leaving my head scratching because, as I understand it, macros are just simple text substitution. Ergo, shouldn’t the Unreal Header Tool pick this up just fine?

Or does UHT run FIRST before the compiler evaluates the macros? I can see that causing issues. If that’s the case, is there a workaround?

What’s especially frustrating is that if I code this:

#define GAME_ENUM(EnumName)        UENUM() enum class EnumName : uint8

It compiles just fine!

Why on earth does the latter work for UHT, but not the former? Is there some special syntax I need to add in order to get UHT to recognize this properly?

Thank you.

This is not supported.

The enum appears to work because there’s nothing to detect and throw a compiler error, but it doesn’t really work, your macro does nothing. It will not be a real UENUM so it won’t benefit from reflection system and will not be available in blueprints/editor.

UHT runs before compiler and there’s no workaround, unless maybe you set up your own code generation system to run even before UHT, but that sounds like a lot of work.

1 Like