5

I'd like to implement something like UPROPERTY() macro in my project, but I cannot find any references of what it actually is. I mean, there're are tutorials on how this macro works, but these are just use cases.

How does the compiler know that UPROPERTY() references the variable under it?

example:

UPROPERTY(EditAnywhere)
int x = 0;

I know that unreal is open source, but its codebase is huge.

Edziju
  • 399
  • 11
  • Do you have any references to learning macros? I got some base knowledge (functions, classes, I've also implemented generic compile time type descriptor class with macro based on https://philippegroarke.com/posts/2022/data_driven_descriptor_based_generative_cpp/), so I need something more advanced. When I search for something more advanced I always get the #define, #if, #ifdef simple examples which I already know, though. – Edziju Oct 17 '22 at 21:16

1 Answers1

7

UPROPERTY is not a real C++ macro. It looks like one, so the compiler accepts it, but it actually is used by the Unreal Header Tool (UHT) to create code that will then be added in the GENERATED_BODY code of your class. The UHT is a parser that just takes all of your code and searches for all those UCLASS, UPROPERTY, UFUNCTION, etc. declarations and then generates the proper code for it that will then be put in the "XXX.generated.h" and "XXX.generated.cpp" files that you include. For example, if you want your variable to be readable via blueprints, a getter logic is created to allow the blueprint logic to call it.

So the compiler does not actually know anything about what a UPROPERTY is, because the pre processor removes it entirely due to it not having an actual body.

If you want to know how the UHT works in depth, you should look into its code. There you could also modify it to suit your needs, including custom code generation.


A side note regarding macros in Unreal: The Slate framework in Unreal actually uses macros to generate a lot of boilerplate code without the Unreal Header Tool. Maybe looking into it will give you some insights. But they are not identical to the power of the code generation done by the UHT.

Max Play
  • 3,717
  • 1
  • 22
  • 39
  • Thank you for the profound explanation. I'll definitely take a look into the code you linked. – Edziju Oct 21 '22 at 19:32