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.
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.